douyanpeng0748 2018-09-02 08:54
浏览 63

TwitterAPIExchange:推特状态有效,但不适用于图像

I have been struggling with this for days (months actually, but the last few days almost non-stop). It seems that many have also struggled with it and it would be nice to have a definitive answer that others can draw on.

I am using TwitterAPIExchange and originally wrote a few wrapper classes, but when they were not working (same results - tweets the message, but not the image), I have written a self-contained, minimal example (see below).

This will tweet the status, but never the image. Why?

Any ideas/suggestions?

UPDATE: I have also tried a "chunking" upload (the suggested method for uploading images) with the same result. The start of the response from the upload is:

string(2462) "{"created_at":"Mon Sep 03 10:27:39 +0000 2018","id":1036561354113437696,"id_str":"1036561354113437696","text":"Test Tweet - Delete me","truncated":false,"entities":{"hashtags":[],"symbols":[],"user_mentions ............"

Note that the media_id (1036561350065971201) does not appear in the response. Is that right? Does this suggest that the call to tweet is ignoring the 'media-ids' parameter? Just to note, the tweet still appears, so that is working.

require_once($_SERVER['DOCUMENT_ROOT'] . '/php/classes/' . 'TwitterAPIExchange.php');

$settings = array(
'oauth_access_token' => "XXX",
'oauth_access_token_secret' => "XXX",
'consumer_key' => "XXX",
'consumer_secret' => "XXX"
);

// Upload the image
$url = 'https://upload.twitter.com/1.1/media/upload.json';
$requestMethod = 'POST';

$image = $_SERVER['DOCUMENT_ROOT'] . '/soundbites/images/gxk.jpg';
display($image);

$postfields = array('media_data' => base64_encode(file_get_contents($image)));

var_dump($postfields);

$twitter = new TwitterAPIExchange($settings);

$response = $twitter->buildOauth($url, $requestMethod)
  ->setPostfields($postfields)
  ->performRequest();

var_dump($response);

// get the media_id from the API return
$media_id = json_decode($response)->media_id;
display("Media_id: " . $media_id);



// .... then send the Tweet along with the media ID
$url = 'https://api.twitter.com/1.1/statuses/update.json';

$requestMethod = 'POST';

$postfields = array(
  'status' => 'Test Wteet - Delete me',
  'media_ids' => $media_id,
);

var_dump($postfields);


$response = $twitter->buildOauth($url, $requestMethod)
  ->setPostfields($postfields)
  ->performRequest();

var_dump($response);


function display ($msg = "No custom message", $fn = "No fn defined"){
    echo $msg . "<br />";
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘