无法使用 Twitter API、PHP 和 curl 发布图像和文本?媒体/上传和状态/更新

Posted

技术标签:

【中文标题】无法使用 Twitter API、PHP 和 curl 发布图像和文本?媒体/上传和状态/更新【英文标题】:Cannot post image and text with Twitter API, PHP and curl ? media/upload and statuses/update 【发布时间】:2021-11-27 07:41:23 【问题描述】:

我可以得到一个 image_id 但不能用图片更新状态...

我向“media/upload.json”发出第一个请求并获得了 media_id,然后我向“statuses/update.json”发出了一个请求,其中包含状态消息和之前收到的 media_id。只显示文字,不显示图片。

我的php代码:

<?php
$oauth_access_token = "XXXX";
$oauth_access_token_secret = "XXXX";
$consumer_key = "XXXX";
$consumer_secret = "XXXX";

//twitter api urls
$URLS = array(
    "image" => "https://upload.twitter.com/1.1/media/upload.json",
    "status" => "https://api.twitter.com/1.1/statuses/update.json"
);

function buildBaseString($baseURI, $method, $params)

    $r = array();
    ksort($params);
    foreach ($params as $key => $value) 
        $r[] = "$key=" . rawurlencode($value);
    
    return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));


function buildAuthorizationHeader($oauth)

    $r = 'Authorization: OAuth ';
    $values = array();
    foreach ($oauth as $key => $value)
        $values[] = "$key=\"" . rawurlencode($value) . "\"";
    $r .= implode(', ', $values);
    return $r;


function makeRequest($postfields, $url)

    global $consumer_key;
    global $consumer_secret;
    global $oauth_access_token;
    global $oauth_access_token_secret;
    $oauth = array(
        'oauth_consumer_key' => $consumer_key,
        'oauth_nonce' => time(),
        'oauth_signature_method' => 'HMAC-SHA1',
        'oauth_token' => $oauth_access_token,
        'oauth_timestamp' => time(),
        'oauth_version' => '1.0'
    );
    $base_info = buildBaseString($url, 'POST', $oauth);
    $composite_key = rawurlencode($consumer_secret) . '&' . rawurlencode($oauth_access_token_secret);
    $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
    $oauth['oauth_signature'] = $oauth_signature;
    $header = array(buildAuthorizationHeader($oauth), 'Content-Type: multipart/form-data;');
    $options = array(
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_POSTFIELDS => $postfields,
        CURLOPT_HEADER => false,
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false
    );
    $feed = curl_init();
    curl_setopt_array($feed, $options);
    $json = curl_exec($feed);
    curl_close($feed);
    return $json;



//Upload image to twitter and get media_id
$file = file_get_contents("C:\wamp64\apps\annonces_insolites\auto\annonces_insolites\img\mercedes.jpg");
$postfields = array("media_data" => base64_encode($file));
$result = makeRequest($postfields, $URLS['image']);
$imageresult = json_decode($result);
$imageid = $imageresult->media_id_string;
var_dump($imageresult);

// update status with status and media_id
$postfields = array(
    "media_ids" => $imageid,
    "status" => "test messsage with image"
);
var_dump($postfields);
$result = makeRequest($postfields, $URLS['status']);
$statusresult = json_decode($result);
var_dump($statusresult);

twitter api 响应: 在对我的第一个请求的响应中,我有 media_id。 使用 media_id 但更新状态时不起作用...

C:\wamp64\apps\annonces_insolites\auto\annonces_insolites\reseaux\twitter\twitter_fct2.php:82:
object(stdClass)[1]
  public 'media_id' => int 1446017066218164228
  public 'media_id_string' => string '1446017066218164228' (length=19)
  public 'size' => int 161329
  public 'expires_after_secs' => int 86400
  public 'image' => 
    object(stdClass)[2]
      public 'image_type' => string 'image/jpeg' (length=10)
      public 'w' => int 1200
      public 'h' => int 900
C:\wamp64\apps\annonces_insolites\auto\annonces_insolites\reseaux\twitter\twitter_fct2.php:91:
array (size=2)
  'media_ids' => string '1446017066218164228' (length=19)
  'status' => string 'test messsage with image' (length=24)
C:\wamp64\apps\annonces_insolites\auto\annonces_insolites\reseaux\twitter\twitter_fct2.php:96:
object(stdClass)[3]
  public 'created_at' => string 'Thu Oct 07 07:38:29 +0000 2021' (length=30)
  public 'id' => int 1446017068470611973
  public 'id_str' => string '1446017068470611973' (length=19)
  public 'text' => string 'test messsage with image' (length=24)
  public 'truncated' => boolean false
  public 'entities' => 
    object(stdClass)[4]
      public 'hashtags' => 
        array (size=0)
          empty
      public 'symbols' => 
        array (size=0)
          empty
      public 'user_mentions' => 
        array (size=0)
          empty
      public 'urls' => 
        array (size=0)
          empty
  public 'source' => string '<a href="XXXX" rel="nofollow">XXXX</a>' (length=83)
  public 'in_reply_to_status_id' => null
  public 'in_reply_to_status_id_str' => null
  public 'in_reply_to_user_id' => null
  public 'in_reply_to_user_id_str' => null
  public 'in_reply_to_screen_name' => null
  public 'user' => 
    object(stdClass)[5]
      public 'id' => int XXXX
      public 'id_str' => string 'XXXX' (length=19)
      public 'name' => string 'XXXX' (length=23)
      public 'screen_name' => string 'XXXX' (length=6)
      public 'location' => string 'XXXX' (length=19)
      public 'description' => string 'XXXX' (length=59)
      public 'url' => null
      public 'entities' => 
        object(stdClass)[7]
          public 'description' => 
            object(stdClass)[6]
              ...
      public 'protected' => boolean false
      public 'followers_count' => int 54
      public 'friends_count' => int 47
      public 'listed_count' => int 0
      public 'created_at' => string 'Sun Jan 20 11:36:11 +0000 2019' (length=30)
      public 'favourites_count' => int 2163
      public 'utc_offset' => null
      public 'time_zone' => null
      public 'geo_enabled' => boolean false
      public 'verified' => boolean false
      public 'statuses_count' => int 172
      public 'lang' => null
      public 'contributors_enabled' => boolean false
      public 'is_translator' => boolean false
      public 'is_translation_enabled' => boolean false
      public 'profile_background_color' => string 'F5F8FA' (length=6)
      public 'profile_background_image_url' => null
      public 'profile_background_image_url_https' => null
      public 'profile_background_tile' => boolean false
      public 'profile_image_url' => string 'XXXX' (length=75)
      public 'profile_image_url_https' => string 'XXXX' (length=76)
      public 'profile_banner_url' => string 'XXXX' (length=68)
      public 'profile_link_color' => string '1DA1F2' (length=6)
      public 'profile_sidebar_border_color' => string 'C0DEED' (length=6)
      public 'profile_sidebar_fill_color' => string 'DDEEF6' (length=6)
      public 'profile_text_color' => string '333333' (length=6)
      public 'profile_use_background_image' => boolean true
      public 'has_extended_profile' => boolean true
      public 'default_profile' => boolean true
      public 'default_profile_image' => boolean false
      public 'following' => boolean false
      public 'follow_request_sent' => boolean false
      public 'notifications' => boolean false
      public 'translator_type' => string 'none' (length=4)
      public 'withheld_in_countries' => 
        array (size=0)
          empty
  public 'geo' => null
  public 'coordinates' => null
  public 'place' => null
  public 'contributors' => null
  public 'is_quote_status' => boolean false
  public 'retweet_count' => int 0
  public 'favorite_count' => int 0
  public 'favorited' => boolean false
  public 'retweeted' => boolean false
  public 'lang' => string 'en' (length=2)

【问题讨论】:

【参考方案1】:

我遇到了完全相同的问题。在几乎放弃了几次之后,我终于破解了它。

一些你需要知道/做的事情。

    确保将您的帖子参数附加到您传递给 buildBaseString 的 OAuth 数组中,然后在对它们进行 URL 编码之前执行 ksort

    如果您将 post 参数作为数组传递给 curl,那么它会将 content-type 标头更改为您不想要的 multipart/form-data,因此请务必先执行以下操作:$post = http_build_query($data, "", "&amp;"); before传递给 curl。

    OAuth 规范说当且仅当编码为 x-www-form-urlencoded 时,主体应包含在基本字符串中以进行签名。

    至于为什么 Twitter 接受所有禁止 media_ids 参数(如果您作为 multipart/form-data 发送)的原因我不知道,但似乎是这样。

    不要将 post 参数包含到您传递给 buildAuthorisationHeader func 的 OAuth 数组中:)

我希望这对您有所帮助,因为它让我发疯了好几个小时,然后我才能让它工作。我现在对 OAuth 的了解比我想知道的还要多,哈哈。

PS - 成功后,您应该会在 JSON 响应中收到一大块扩展属性,详细说明推文中包含的图像/视频。

【讨论】:

感谢您的回复。【参考方案2】:

我创建了这段代码,它运行良好: 如果有人需要帮助或想在没有任何库的情况下将图像和状态上传到 twitter ;)

<?php

function post_media_status($url, $method, $media_ids, $status, $oauthconsumerkey, $oauthtoken, $clientsecret, $tokensecret)


    $nonce = time();
    $timenow = time();
    $signature = build_signature($url, $method, $media_ids, $status, $nonce, $timenow, $oauthconsumerkey, $oauthtoken, $clientsecret, $tokensecret);
    //print($signature);

    $oauth = array(
        'oauth_consumer_key' => $oauthconsumerkey,
        'oauth_nonce' => $nonce,
        'oauth_signature_method' => 'HMAC-SHA1',
        'oauth_token' => $oauthtoken,
        'oauth_timestamp' => $timenow
    );
    $oauth['oauth_signature'] = $signature;

    $header = array(buildAuthorizationHeader($oauth));
    var_dump($header);

    curl_post_status_media($url, $header, $media_ids, $status);


function build_signature($url, $method, $media_ids, $status, $nonce, $timenow, $oauthconsumerkey, $oauthtoken, $clientsecret, $tokensecret)


    $key = rawurlencode($clientsecret) . "&" . rawurlencode($tokensecret);

    $paramstring = "media_ids=" . $media_ids . "&oauth_consumer_key=" . $oauthconsumerkey . "&oauth_nonce=" . $nonce . "&oauth_signature_method=HMAC-SHA1" . "&oauth_timestamp=" . $timenow . "&oauth_token=" . $oauthtoken . "&status=" . $status;
    $encodeurl = $method . "&" . rawurlencode($url) . "&" . rawurlencode($paramstring);
    $signature = hash_hmac('sha1', $encodeurl, $key, TRUE);
    $signature = base64_encode($signature);
    return $signature;


function curl_post_status_media($url, $header, $media_ids, $status)

    $options = array(
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_POSTFIELDS => "status=" . $status . "&media_ids=" . $media_ids,
        CURLOPT_HEADER => false,
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false
    );

    $request = curl_init();
    curl_setopt_array($request, $options);
    $response = curl_exec($request);
    $decoded = json_decode($response);
    var_dump($decoded);
    curl_close($request);
    return $response;


function buildAuthorizationHeader($oauth)

    $r = 'Authorization: OAuth ';
    $values = array();
    foreach ($oauth as $key => $value)
        $values[] = "$key=\"" . rawurlencode($value) . "\"";
    $r .= implode(', ', $values);
    return $r;



function upload_media($postfields, $url, $oauthconsumerkey, $oauthtoken, $clientsecret, $tokensecret)

    $oauth = array(
        'oauth_consumer_key' => $oauthconsumerkey,
        'oauth_nonce' => time(),
        'oauth_signature_method' => 'HMAC-SHA1',
        'oauth_token' => $oauthtoken,
        'oauth_timestamp' => time(),
        'oauth_version' => '1.0'
    );
    $base_info = buildBaseString($url, 'POST', $oauth);
    $composite_key = rawurlencode($clientsecret) . '&' . rawurlencode($tokensecret);
    $oauth_signature = base64_encode(hash_hmac('sha1', $base_info, $composite_key, true));
    $oauth['oauth_signature'] = $oauth_signature;
    $header = array(buildAuthorizationHeader($oauth));
    $options = array(
        CURLOPT_HTTPHEADER => $header,
        CURLOPT_POSTFIELDS => $postfields,
        CURLOPT_HEADER => false,
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER => false
    );
    $feed = curl_init();
    curl_setopt_array($feed, $options);
    $json = curl_exec($feed);
    //var_dump($json);
    curl_close($feed);
    return $json;


function buildBaseString($baseURI, $method, $params)

    $r = array();
    ksort($params);
    foreach ($params as $key => $value) 
        $r[] = "$key=" . rawurlencode($value);
    
    return $method . "&" . rawurlencode($baseURI) . '&' . rawurlencode(implode('&', $r));



function post_media_with_status($file_location, $status, $oauthconsumerkey, $clientsecret, $tokensecret, $oauthtoken)

    $file = file_get_contents($file_location);
    $postfields = array("media_data" => base64_encode($file));
    $result = upload_media($postfields, "https://upload.twitter.com/1.1/media/upload.json", $oauthconsumerkey, $oauthtoken, $clientsecret, $tokensecret);
    $imageresult = json_decode($result);
    var_dump($imageresult);
    $imageid = $imageresult->media_id_string;
    $result = post_media_status("https://api.twitter.com/1.1/statuses/update.json", "POST", $imageid, $status, $oauthconsumerkey, $oauthtoken, $clientsecret, $tokensecret);
    return $result;


post_media_with_status("IMAGE_URL", "Test", TWITTER_API_KEY, TWITTER_API_SECRET_KEY, "TOKEN_SECRET", "TOKEN");

【讨论】:

以上是关于无法使用 Twitter API、PHP 和 curl 发布图像和文本?媒体/上传和状态/更新的主要内容,如果未能解决你的问题,请参考以下文章

当有人使用twitter api和php发布推文时如何获得通知

如何使用 Twitter API 获取用户访问令牌和访问密钥

PHP 使用PHP从Twitter搜索API到MySQL数据库保存推文

PHP - Twitter API - 使用空间时请求损坏

PHP+JSON+Twitter API

使用 PHP Laravel 使用 Twitter API 获取授权用户详细信息