I\'ve got the following working code, the code simply posts a status and an image om my twitter page.
require_once(\'TwitterAPIExchange.php\');
$
The accepted answer uses a depreciated API endpoint https://dev.twitter.com/rest/reference/post/statuses/update_with_media
Here is a working solution:
// send image to Twitter first
$url = 'https://upload.twitter.com/1.1/media/upload.json';
$requestMethod = 'POST';
$image = 'full/path/to/image.jpg';
$postfields = array(
'media_data' => base64_encode(file_get_contents($image))
);
$response = $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();
// get the media_id from the API return
$media_id = json_decode($response)->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' => 'My amazing tweet'
'media_ids' => $media_id,
);
$response = $twitter->buildOauth($url, $requestMethod)
->setPostfields($postfields)
->performRequest();