I've tried using Matt Harris' Twitter OAUTH library (https://github.com/themattharris/tmhOAuth) replacing default data with my keys and tokens, but for some reason I can't get a valid response code.
The url I'm testing with ends with a port (8888), but I'm not sure if that is to do with it. I'm tailing the PHP log and there are no errors.
$tweet_text = 'Hello world!';
print "Posting...\n";
$result = post_tweet($tweet_text);
print "Response code: " . $result . "\n";
function post_tweet($tweet_text) {
require_once('tmhOAuth.php');
$connection = new tmhOAuth(array(
'consumer_key' => '(hidden)',
'consumer_secret' => '(hidden)',
'user_token' => '(hidden)',
'user_secret' => '(hidden)',
));
$connection->request('POST',
$connection->url('1/statuses/update'),
array('status' => $tweet_text));
return $connection->response['code'];
}
Does anybody have any ideas?
Many thanks.
If you compare with https://github.com/themattharris/tmhOAuth/blob/master/examples/tweet.php your code might need to change to look like:
$code=$connection->request('POST', ...);
return $code;
However, looking at the source code, reveals two things. First your code should be just as good as that, because $this->response['code']
is set to the value that gets returned. Second that that function (actually curlit()
) can also return void. When it does that response['code']
is undefined. (This was looking like a promising twitter library until I saw that design mistake.)
Probing even further, it would only return void
when $this->config['prevent_request']
exists and is true. You're not doing that, and we've gone full circle to not being able to explain the behaviour you see.
So, my next troubleshooting step would be to put error_reporting(E_ALL|E_NOTICE)
at the top, and then check the error logs for more clues. Also do a print_r($connection->response)
after your call to request()
to see what else you have in there.
I had this problem and it's due to this recent change in December 2011:
"defaulted CURLOPT_SSL_VERIFYPEER to true"
If you dump $connection->response as Darren advises, you may see an error such as this one which I had:
string(165) "error setting certificate verify locations:"
Either make sure that the certificate file cacert.pem is in place, or disable SSL_VERIFYPEER.
I'm willing to bet that this will fix your issue (although it may not be the most secure solution):
$connection = new tmhOAuth(array(
'consumer_key' => '(hidden)',
'consumer_secret' => '(hidden)',
'user_token' => '(hidden)',
'user_secret' => '(hidden)',
'curl_ssl_verifypeer' => false
));
Update February 2015
Looking back on this answer today, I realise that advising people to set curl_ssl_verifypeer to false is not a very good answer (at this point you can no longer be sure you're talking to Twitter, so in fact it's a terrible answer). Instead, take the other advice I gave and ensure the appropriate Root CA Certificate file (cacert.pem) is in place.
Looks like you need to make sure you have a current cert.
According to TMH's git repo:
Version 0.60 hardened the security of the library and defaulted curl_ssl_verifypeer to true. As some hosting providers do not provide the most current certificate root file it is now included in TMH's repository. If the version is out of date OR you prefer to download the certificate roots yourself, you can get them from: http://curl.haxx.se/ca/cacert.pem
Before upgrading the version of tmhOAuth that you use, be sure to verify the SSL handling works on your server by running the examples/verify_ssl.php script.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With