This self-contained, PHP, TwitterAPIExchange code attempts to upload a file file to Twitter using Chunks. It all seems to work except the STATUS part (you have to do INIT, APPEND, FINALIZE and can check its STATUS) returns
string(64) "{"errors":[{"code":32,"message":"Could not authenticate you."}]}" Response after check STATUS: end
This does not seen to affect the upload, but I do not understand why one of the four commands does not work? Any ideas/suggestions?
At the end of this post, I have included the output of this code snippet (note: APPEND does not return a response - this is correct accordinging to the API documentation, which shows the various responses and var_dump's of each of the commands.
As an aside the final tweet works but does not display the image, but this is an ongoing issue that I have sought help on elsewhere, but any insights to that would also be welcome.
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");
$twitter = new TwitterAPIExchange($settings);
display('<br />============================= Image Details =========================================================<br />');
$image = $_SERVER['DOCUMENT_ROOT'] . '/soundbites/images/gxk.jpg';
$fileSize = filesize($image);
display($image . ': Size is ' . $fileSize . ' bytes');
display('<br />=============================== INIT =======================================================<br />');
$url = 'https://upload.twitter.com/1.1/media/upload.json';
$requestMethod = 'POST';
$postfields = array('command' => 'INIT', 'total_bytes' => $fileSize, 'media_type' => 'image/jpeg');
display('<br />postfields after INIT: start<br />');
var_dump($postfields);
display('<br />postfields after INIT: end<br />');
$response = $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
display('<br />Response after INIT: start<br />');
var_dump($response);
display('<br />Response after INIT: end<br />');
$media_id = json_decode($response)->media_id;
display("Media_id: " . $media_id);
display('<br />================================ APPEND ======================================================<br />');
$url = 'https://upload.twitter.com/1.1/media/upload.json';
$requestMethod = 'POST';
$postfields = array('command' => 'APPEND',
'media_id' => $media_id,
'media_data' => base64_encode(file_get_contents($image)),
//media' => $image,
'segment_index' => 0);
display('<br />postfields after APPEND: start<br />');
//var_dump($postfields); // Too large to display here, but outputs the file
display('<br />postfields after APPEND: end<br />');
$response = $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
display('Response after APPEND: start');
var_dump($response);
display('Response after APPEND: end');
display('<br />========================== STATUS ============================================================<br />');
$url = 'https://upload.twitter.com/1.1/media/upload.json';
$requestMethod = 'GET';
$postfields = array('command' => 'STATUS', 'media_id' => $media_id);
display('<br />postfields after check STATUS: start<br />');
var_dump($postfields);
display('<br />postfields after check STATUS: end<br />');
$response = $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
display('Response after check STATUS: start');
var_dump($response);
display('Response after check STATUS: end');
display('<br />============================== FINALIZE ========================================================<br />');
$url = 'https://upload.twitter.com/1.1/media/upload.json';
$requestMethod = 'POST';
$postfields = array('command' => 'FINALIZE',
'media_id' => $media_id);
display('<br />postfields after FINALIZE: start<br />');
var_dump($postfields);
display('<br />postfields after FINALIZE: end<br />');
$response = $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
display('Response after FINALIZE: start');
var_dump($response);
display('Response after FINALIZE: end');
display('<br />=============================== TWEET =======================================================<br />');
$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod = 'POST';
$postfields = array(
'status' => 'Test Tweet - Delete me',
'media_id_string' => $media_id);
display('<br />postfields after TWEET: start<br />');
var_dump($postfields);
display('<br />postfields after TWEET: end<br />');
$response = $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
display('Response after TWEET: start');
var_dump($response);
display('Response after TWEET: end');
function display ($msg = "No custom message", $fn = "No fn defined"){
echo $msg . "<br />";
}
=== OUTPUT ===
============================= Image Details =========================================================
/home2/sentralg/public_html/soundbites/images/gxk.jpg: Size is 42665 bytes
=============================== INIT =======================================================
postfields after INIT: start
array(3) { ["command"]=> string(4) "INIT" ["total_bytes"]=> int(42665) ["media_type"]=> string(10) "image/jpeg" }
postfields after INIT: end
Response after INIT: start
string(99) "{"media_id":1036759321785163777,"media_id_string":"1036759321785163777","expires_after_secs":86400}"
Response after INIT: end
Media_id: 1036759321785163777
================================ APPEND ======================================================
postfields after APPEND: start
postfields after APPEND: end
Response after APPEND: start
string(0) "" Response after APPEND: end
========================== STATUS ============================================================
postfields after check STATUS: start
array(2) { ["command"]=> string(6) "STATUS" ["media_id"]=> int(1036759321785163777) }
postfields after check STATUS: end
Response after check STATUS: start
string(64) "{"errors":[{"code":32,"message":"Could not authenticate you."}]}" Response after check STATUS: end
============================== FINALIZE ========================================================
postfields after FINALIZE: start
array(2) { ["command"]=> string(8) "FINALIZE" ["media_id"]=> int(1036759321785163777) }
postfields after FINALIZE: end
Response after FINALIZE: start
string(165) "{"media_id":1036759321785163777,"media_id_string":"1036759321785163777","size":42665,"expires_after_secs":86400,"image":{"image_type":"image\/jpeg","w":200,"h":147}}" Response after FINALIZE: end
=============================== TWEET =======================================================
postfields after TWEET: start
array(2) { ["status"]=> string(22) "Test Tweet - Delete me" ["media_id_string"]=> int(1036759321785163777) }
postfields after TWEET: end
Response after TWEET: start
string(2462) "{"created_at":"Mon Sep 03 23:34:20 +0000 2018","id":1036759326759636992,"id_str":"1036759326759636992","text":"Test Tweet - Delete me","truncated":false," ... [remainder deleted]
Response after TWEET: end