Updating user's status - 401 Error


I’m trying to update a user’s status in php using the following code but each time i am getting a 401 error

$twitter_url = 'https://api.twitter.com/1/statuses/update.json';
$tweet = "test tweet";


$ci = curl_init();
curl_setopt($ci, CURLOPT_HTTPHEADER, array('Expect:'));
curl_setopt($ci, CURLOPT_TIMEOUT, 10);
curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ci, CURLOPT_HEADER, FALSE);
curl_setopt($ci, CURLINFO_HEADER_OUT, TRUE);
curl_setopt($ci, CURLOPT_VERBOSE, true);

curl_setopt($ci, CURLOPT_POST, TRUE);
curl_setopt($ci, CURLOPT_POSTFIELDS, $post_parameters);

curl_setopt($ci, CURLOPT_URL, $twitter_url);
$result = curl_exec($ci);
curl_close ($ci);

return $response_info;

I am 100% sure that all the parameters set in $post_parameters are correct because when i use them for verifying credentials, i receive an HTTP 200 header. The problem is in my opinion coming from the curl section.

Can someone please help?


The difference between generating a signature for a POST and GET to account/verify_credentials is pretty great – how are you generating your signature base string in this context? Have you tried using the OAuth tool built into this site to generate golden examples to code against?


Hello Taylor,
I use Abraham’s php library to generate the signature like this :

$consumer_key = XXXXX; $consumer_secret = XXXXX; $oauth_access_token = XXXXX; $oauth_access_token_secret = XXXXX;

$twitter_url = ‘https://api.twitter.com/1/statuses/update.json’;

$consumer = new OAuthConsumer($consumer_key, $consumer_secret);
$sha1_method = new OAuthSignatureMethod_HMAC_SHA1();
$token = new OAuthConsumer($oauth_access_token, $oauth_access_token_secret);

$request = OAuthRequest::from_consumer_and_token($consumer, $token, ‘POST’, $twitter_url, array());
$request->sign_request($sha1_method, $consumer, $token);
$oauth_signature = urlencode($request->get_parameter(‘oauth_signature’));


In this code, you’re signing the request ($request->sign_request) before you’ve set a status parameter that you’re sending along with the request.


Thanks Taylor…problem fixed! :smiley:
Just had to set a status parameter to the request as you mentioned

$request = OAuthRequest::from_consumer_and_token($consumer, $token, 'POST', $twitter_url, array('status'=>$tweet));