Bharanikumar
Bharanikumar

Reputation: 25733

I can't post to Twitter using cURL and PHP

I am unable to tweet continuously in twitter - every three tweets I get:

Error posting to Twitter. Retry

How do I fix this?

My code is as follows:

$host = "http://twitter.com/statuses/update.xmlstatus=".urlencode(stripslashes(urldecode($message)));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
$result = curl_exec($ch);
// Look at the returned header
$resultArray = curl_getinfo($ch);
//print_r($resultArray);
if($resultArray['http_code'] == "200"){
     $twitter_status='Your message has been Tweeted';
     update_tweet_extra($result_id,"1");
} else {
 $twitter_status="Error posting to Twitter. Retry";
 // update_tweet_extra($result_id,"0");
}
echo  $twitter_status;

Upvotes: 0

Views: 3294

Answers (5)

Metablocks Corp
Metablocks Corp

Reputation: 1645

You can find a list of PHP libraries that support OAUTH and you can use to write a tweet function in PHP and the 1.1 version of the Twitter API here: https://dev.twitter.com/docs/twitter-libraries

tmhOAuth is probably my favorite.

Upvotes: 0

Sahil Singh
Sahil Singh

Reputation: 3787

to tweet using twitter you will need a post_authenticity_token along with your username and password. this token can be obtained from your profile page by fetching it using curl (after you login with curl). i experimented with curl and was able to tweet using curl. you can find my code at (though it is in bash script, it can be ported to php easily coz they both use curl) http://pastebin.com/a5eBcEeP .

Upvotes: 0

Pons
Pons

Reputation: 1776

You can't use API with basic authentication since 31 august 2010. You can use a small spider function with curl that login in, fetch your home, and tweet. I made it: http://www.barattalo.it/2010/09/09/how-to-change-twitter-status-with-php-and-curl-without-oauth/ If you don't want to use oAuth authentication model you have to do this way.

Upvotes: 2

Martin Bean
Martin Bean

Reputation: 39389

I don't think this is a problem with your code, but more the fact that Twitter is phasing out basic authentication support and moving to OAuth.

There's more information at http://apiwiki.twitter.com/Authentication.

Upvotes: 2

Dominic Rodger
Dominic Rodger

Reputation: 99751

This seems very odd, since status updates via the Twitter API are not rate-limited (see here), unless you're updating more than 1000 times a day.

You might work out more of what happened if you uncomment that helpful looking line:

//print_r($resultArray);

by removing the // at the beginning of line.

Upvotes: 0

Related Questions