Passing media id to POST statuses/update


#1

Hey,

I am using the twitter api by j7mbo ( http://github.com/j7mbo/twitter-api-php ) because I am making a small personal wordpress site, and just wanted everything minimal and in my experience with namespaces etc. wordpress doesnt really like playing with them

ANYWAY, I upload an image to https://upload.twitter.com/1.1/media/upload.json and it returns fine, with a media object. However when i try update a new statusm with the media ids attached, the tweet never displays an image?

heres some sample code

$img = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID),'large'); // get the image object of what I want to upload

if($img)
{
	$url = 'https://upload.twitter.com/1.1/media/upload.json';
	$requestMethod = 'POST';
	
	$postfields = array(
		'media' => file_get_contents($img[0])
	);
	
	$twitter = new TwitterAPIExchange($settings);
	$result = $twitter->buildOauth($url, $requestMethod)
					 ->setPostfields($postfields)
					 ->performRequest(true);
					 
	$json = json_decode($result);
}

$url = 'https://api.twitter.com/1.1/statuses/update.json';
$requestMethod = 'POST';

$postfields = array(
	'status' => titleSend($post->post_title,$post->ID)
);

if($json) // if there is json from the media upload add it as another parameter
{
	$postfields['media_ids'] = $json->media_id_string;	
}

$twitter = new TwitterAPIExchange($settings);

$results = $twitter->buildOauth($url, $requestMethod)
		 ->setPostfields($postfields)
		 ->performRequest();

Any help on the subject would be greatly appreciated,
Do the media ids have to be formatted in a specific way? (anything to do with the fact it can have up to 4 media entities?)

Thanks
Jonny


Media_ids parameter ignored with status update
#2

Make sure that on your second request, the one to the statuses/update endpoint, your body is encoded with www-form-encoding (application/x-www-form-urlencoded) and not multipart/formdata.


#3

Hey thanks for getting back, I’m out at the moment so I cant check this, but that sounds like it will sort it out

Will update when I have tried it, thanks again!


#4

Hello, how did you resolve the issues? I’ve been banging my head for over 2 days now! Thanks!


#5

I had the same issue, and eventually I switched libraries, here is a post I made on another thread:

Hi, I was having this problem as well, and earlier I saw another person post this who also had the same problem and was using the same library.

When I saw your post I realised it might be a problem with the library, so I tried tmhOauth instead which worked fine for me. I can’t say why it doesn’t work for J7mbo/twitter-api-php, maybe something to do with the http headers.

I used the example script here for uploading media with tmhOauth and then creating a tweet with the attached media.

Hope that helps

Joe