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 />";
}