Posting Tweet with Media

media
media-upload
php

#1

Hello
With this codes, i can get media id and post tweet but my photo not seem on tweet. Could you help me what i have to chabge?
Thanks

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);   
//////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();

#2

What error are you seeing with this code? Do you get back a Tweet ID from the statuses/update call successfully?


#4

I don’t see any error. It post a tweet but without image so yes i get tweet id. I cant solve pls help me


#5

Can you provide any media ID that is returned by this code, or any Tweet ID that is returned by this code?


#7

Media ID : 910262587987656704
Tweet ID: 910263606482259968


#8

Please help me :slight_smile:


#9

Hi, so did he helped you ? I’m having the same problem with direct message right now