Reputation: 87
With this code, I can upload image and print Media ID but I can't post a tweet with image. Could you help me?
Thanks
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
set_include_path('/home/.....');
require_once('TwitterAPIExchange.php');
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => "...",
'oauth_access_token_secret' => "...",
'consumer_key' => "...",
'consumer_secret' => "..."
);
$twitter = new TwitterAPIExchange($settings);
$file = file_get_contents('aaa.jpg');
$data = base64_encode($file);
// Upload image to twitter
$url = "https://upload.twitter.com/1.1/media/upload.json";
$method = "POST";
$params = array(
"media_data" => $data,
);
$json = $twitter
->buildOauth($url, $method)
->setPostfields($params)
->performRequest();
// Result is a json string
$res = json_decode($json);
// Extract media id
$id = $res->media_id_string;
print_r($id);
////// Code is working to this line and can print media id like 213213214123123
$url = "https://api.twitter.com/1.1/statuses/update.json";
$twitter = new TwitterAPIExchange($settings);
$requestMethod = 'POST';
$response = $twitter->setPostfields(
array('status' => '', 'media_ids' => $id)
)
->buildOauth($url, $requestMethod)
->performRequest();
Upvotes: 0
Views: 662
Reputation: 3875
The media_id
value can be used to create a Tweet with an attached photo using the statuses/update
endpoint. Use your media_id as a parameter in the Statuses/update
Upvotes: 1