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?