Problem uploading a photo to Twitter API using update_with_media


#1

I am brand new to the Twitter API, and am having quite a bit of trouble making my script work. Basically, I have a wall of dynamically generated photos, and I want people to be able to post them to their twitter feed. For each photo, I am generating a Twitter icon button, which when click, loads a javascript function, “postToTwitter”. That function contains an AJAX call, which calls a PHP script: “postToTwitter.php”.

Right now, with my PHP script, I am just trying to post a static graphic to my wall, before I start moving on to dynamic image URL’s. Here is my code:

Javascript function: “postToTwitter”;

function postToTwitter() {
	var msg = "Hi there!";
	$.ajax({
		url: 'postToTwitter.php',
		type: 'POST',
		data: ({msg:msg}),
		success: function(data) {
			alert(data);
		}
	});
}

postToTwitter.php

<?php
	require 'library/tmhOAuth.php';
    	require 'library/tmhUtilities.php';
	
	$tmhOAuth = new tmhOAuth(array(
      'consumer_key'    => '--consumer-key--',
      'consumer_secret' => '--consumer-secret--',
      'user_token'      => '--user-token--',
      'user_secret'     => '--user-secret--',
    	));
    
	$image = 'images/cancel.png';

	if (isset($_POST['msg'])) {
	    $tweetmsg = $_POST['msg'];
	    $response = $tmhOAuth->request('POST', 'https://upload.twitter.com/1/statuses/update_with_media.json', array('media[]'  => "@{$image}",'status'   => "This is a status"), true, true);
		
		if($tmhOAuth) {
			echo $response;
		} else {
			echo "Your message has not been sent to Twitter.";
		}
		
	} else {
		echo "Your message has not been sent to Twitter.";
	}
?>

When I execute this code, my AJAX script alerts “0”, and the tweet is not posted. Any idea what I am missing, or doing wrong?


#2

Have you tried isolating the interaction with the API to not involve your AJAX-executed code? Have you tried examining the direct error messages you might be getting back when making the API request?


#3

I went in and removed the if((isset($_POST[‘msg’]), which effectively removes the need for the script to run based off of the AJAX call, allowing me to access the file directly, which I did. I also replaced the $_POST[‘msg’] with a static string. New code looks like this:

require 'library/tmhOAuth.php'; require 'library/tmhUtilities.php';
$tmhOAuth = new tmhOAuth(array(
	'consumer_key' => 'fSNOwKkG8KgU8pGNgt6w',
	'consumer_secret' => 'xxxx',
	'user_token' => '235311523-hn0wg0Qa3gv61dd0TuThSc5dsLTYNiUNHdfmjCkB',
	'user_secret' => 'xxxx'
));

//echo "Authorization Message:  $tmhOAuth<br/>";
$image = 'images/cancel.png';

$tweetmsg = "This is a test tweet";
$response = $tmhOAuth->request('POST', 'https://upload.twitter.com/1/statuses/update_with_media.json', array('media[]'  => "@{$image}",'status'   => "This is a status"), true, true);

echo "The response is: $response<br/><br/>";
if($tmhOAuth) {
	echo $response;
} else {
	echo "Your message has not been sent to Twitter.";
}

When i visit that page, I’m still getting a returned value of 0. I looked at the error code definitions, and there isn’t even a 0 listed. Can’t quite figure out where to go from here.

I saw something that said that these scripts aren’t necessarily meant to be accessed by a webpage, and need to be run by a server. If that is the case, how do I do that?


#4

HTTP code 0 usually means the request isn’t leaving your machine at all – perhaps due to incomplete SSL settings in your PHP or perhaps due to some kind of firewall issue. Do you have a PHP error log file accessible to you? It might have more information within.

To run a script like this from a non-web context, open up a terminal window to get to a command line. Change to the directory where you have the script. And use the command like:

php filename.php

You may want to go a little more complicated in your debugging in the file. Instead of just echoing $response, I would recommend doing an object dump on it instead – perhaps you’ll then see more about the error disposition that way.


#5

I did not find any working example for upload image on twitter via api. If anyone can give me a php script then it will be very helpful for me.

Thanks in advance.


#6

Very Bad docs and support for Twitter APIs


#7

Hi @stevegartner9 were you able to solve the problems uploading a picture with update_with_media?


#8

I’m also in search of some information for this type of functionality. There are a few discussions on stackoverflow.com using cURL to perform but nothing that I have found to be promising. It would be easier if an intent tweet could perform the uploading. I might be looking to have things uploaded to twitpic first before it is shared as that provides the example in-twitter that I desire.


#9

Does anyone have a working version for API 1.1?


#10

We have listed several librairies that work with 1.1 at https://dev.twitter.com/docs/twitter-libraries.


#11

if you want to tweet images using twitter api you can check our blog to see all the steps and download the example:


#12

if you want to tweet images using twitter api you can check our blog to see all the steps and download the example:


#13

Can you please share code of image uploading on Twitter using Javascript and JQuery? Thanks in advance.


#14

if it is giving status 0,

then put cacert.pem file in the same folder where tmhOAuth.php exixts


#15

I don’t know why nobody posted the latest code update here, but I just tested this CODE snippet using ‘tmhOAuth library for PHP’…

include ‘/var/www/apps/Twitter/tmhOAuth.php’;

$tmhOAuth = new tmhOAuth(array(
‘consumer_key’ => ‘x’,
‘consumer_secret’=> ‘x’,
‘token’ => ‘x’,
‘secret’ => ‘X’
));

$image =’/var/www/images/SpinHistoryRoulette.png’;
$status = “Picture posting test bitcoin-roulette.com”;

$tmhOAuth->request(‘POST’, ‘https://api.twitter.com/1.1/statuses/update_with_media.json’, array( ‘media[]’ => “@{$image}”, ‘status’ => $status), true, true );

The last line of code is what I have seen written incorrectly everywhere.

  • make sure the $image is a reference to a local filename (NOT URL)
  • check the parameters to request() are like above

#16

I could solve it by using only using base64_encoded string. No need to use any other meta information.


#17

I know it is late but anyone coming next may find this useful.
I successfully shared image using this code:

$temp_file = $_POST['url']; // this is path of local file $temp_type = $_POST['type']; // jpg $temp_name = $_POST['name']; // image.jpg

$tmhOAuth = new tmhOAuth( array(
‘consumer_key’ => $_POST[‘consumer_key’],
‘consumer_secret’ => $_POST[‘consumer_secret’],
‘user_token’ => $_POST[‘user_token’],
‘user_secret’ => $_POST[‘user_secret’],
));

$params = array(
‘media[]’ => “@{$temp_file};type={$temp_type};filename={$temp_name}”,
‘status’ => $_POST[‘status’]
);

$code = $tmhOAuth->request( ‘POST’, ‘https://api.twitter.com/1.1/statuses/update_with_media.json’,
$params,
true, // use auth
true // multipart
);

if ( $code == 200 ) {
echo “success”;
}else{
echo $code;
}


#18

Well!


#19

that worked great, thank you!


#20

Thanks Bilal. Quick question… where are you pulling values for URL and Type? Can you help me a little.