簡體   English   中英

update_with_media 使用 abraham 的 twitteroauth

[英]update_with_media using abraham's twitteroauth

我正在嘗試使用 Abraham 的 twitteroauth 庫(TwitterOAuth v0.2.0-beta2)實現來自 ajax 的 upload_with_media 請求。 我對基本帖子沒有任何問題,但是當我嘗試包含媒體時,我得到以下回復:

"{"request":"\/1\/statuses\/update_with_media.json","error":"Error creating status."}"

我發布媒體的代碼如下所示:

   $image = $_FILES["media"]["tmp_name"];

    $parameters = array(
        'media[]'  => "@{$image};type=image/jpeg;filename={$image}",
        'status'   => $status
      );

    if(isset($reply_id)) {
        $parameters['in_reply_to_status_id'] = $reply_id;
    }
    $post = $twitteroauth->post('https://upload.twitter.com/1/statuses/update_with_media.json', $parameters);
    echo json_encode($post);

我已經驗證所有數據都正確地發送到這個腳本,甚至設法使用上面的相同數據和 tmhOAuth 庫使 update_with_media 帖子工作,但由於我的小部件的其余部分使用 twitteroauth,我更願意保持統一。 我也嘗試過在結尾加上和不帶 .json 的情況下,沒有發現任何區別。 誰能給我看一個使用 twitteroauth 成功實現 update_with_media 的例子? 我似乎無法弄清楚我做錯了什么。

在幾個小時內使用 twitterauth 庫處理 UPDATE_WITH_MEDIA 的解決方案后,我發現以下解決方案可以正常工作:

  • 首先:此處從 Twitter Dev 鏈接的 PHP 原始庫不起作用。

不適用於 UPDATE_WITH_MEDIA

  • 經過搜索和搜索,我找到了相同的庫,但有一個修復程序。 你可以在這里找到它: https ://github.com/tomi-heiskanen/twitteroauth/tree/77795ff40e4ec914bab4604e7063fa70a27077d4/twitteroauth

基本的區別是原版有函數“post”,沒有“$multipart”參數。 此參數允許發送 Twiiter 在文檔中要求的內容:多部分 enctype 帖子。 所以最后基本代碼如下:

$image_path="folder/image.jpg";

$handle = fopen($image_path,'rb');
$image  = fread($handle,filesize($image_path));
fclose($handle);

$params = array(
  'media[]' => "{$image};type=image/jpeg;filename={$image_path}",
  'status'  => "Put your message here, must be less than 117 characters!"
);
$post = $connection->post('statuses/update_with_media', $params, true);

重要的! 如果您使用原始庫嘗試此代碼,您會發現錯誤。 您必須從上面的鏈接下載並替換項目中的兩個文件(OAuth.php 和 twitteroauth.php)。

嘗試使用 codebird-php https://github.com/mynetx/codebird-php

事實證明,盡管它在 Twitter 建議的 php 庫列表中排在最后,但它確實起到了作用。 只需從 git repo 中獲取 codebird.php 和 cacert.pem 即可。

    include_once('codebird.php');
    \Codebird\Codebird::setConsumerKey($consumer_key, $consumer_secret);
    $cb = \Codebird\Codebird::getInstance();
    $cb->setToken($token, $token_secret);
    $status = 'Gamo, I just tweeted with an image!';
    $filename = '/home/asdf/test.png';
    $cb->statuses_updateWithMedia(array('status' => $status, 'media[]' => $filename));

原始庫尚不包含上傳媒體功能。 您可以查看https://github.com/natefanaro/twitteroauth

我建議您使用 Fiddler2 或一些類似的工具來檢查和比較通過 twitteroauth 和 tmhOAuth 發出的消息。 你會看到不同之處。

根據我的經驗,這就是使用 update_with_media.{xml,json} 向 Twitter 發送 HTTP POST 的樣子。 我相信,您使用的后綴只會影響響應。 (您的應用必須以特定於您的應用的方式設置 Authorization 標頭。)

您想讓 twitteroauth 發布類似以下內容

POST https://upload.twitter.com/1/statuses/update_with_media.xml HTTP/1.1
Authorization: OAuth oauth_callback="oob", oauth_consumer_key="xxxxxxxxxxxx", oauth_nonce="7774328k", oauth_signature="pUYjRnccmrBYiO1j9cliETsw%2B5s%3D", oauth_signature_method="HMAC-SHA1", oauth_timestamp="1318300521", oauth_token="59152613-vrlZ2edX56PudQtBmpAWd3SPDt9cPyAhibO7ysl6W", oauth_version="1.0"
Content-Type: multipart/form-data; boundary=======c49479438c600bf59345e======
Host: upload.twitter.com
Content-Length: 7320
Connection: Keep-Alive

--======c49479438c600bf59345e======
Content-Disposition: form-data; name="status"

working on a Tweet tool that uses the OAuth Manager library.
--======c49479438c600bf59345e======
Content-Disposition: file; name="media[]"; filename="ThisIsAPicture.png"
Content-Type: image/png

  ...binary png data here...

--======c49479438c600bf59345e======--

我想發送帶有狀態參數的 url 鏈接

喜歡:把你的消息放在這里

對於 2022 年閱讀此書的任何人,我在任何地方都找不到答案,但我設法通過首先上傳媒體而不將 api 版本設置為 2 來解決它。但在檢索圖像后立即設置 api 版本。

似乎版本 2 的端點還不完全支持媒體上傳,不知道為什么。 但這對我來說是完美的,希望它對某人有用:

$connection = new TwitterOAuth(env('TWITTER_CONSUMER_KEY'), env('TWITTER_CONSUMER_SECRET'), env('TWITTER_ACCESS_TOKEN'), env('TWITTER_ACCESS_TOKEN_SECRET'));

    $result = $connection->upload('media/upload', [
        'media' => '/img/path.png',
    ], true);

    // check here to see if img has uploaded
    var_dump($result);

    $connection->setApiVersion('2');

    $response = $connection->post('tweets', [
        'text' => 'Test',
        'media' => [
            'media_ids' => [$result->media_id_string],
        ]
    ], true);

    var_dump($response);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM