PHP application using Twitter API works on some accounts and not others
I have this PHP script I wrote to automatically track users who post with certain conditions. It works 100% of the time on a bunch of test accounts, but then fails on the account I would like to use it with.
I checked the account API rate limit and it is well within the bounds. I have also verified that the username and password are correct. If I change nothing but the username and password to a different account, it works, but when it is changed (correctly) to the master account, nothing happens. I am completely confused. Has anyone ever encountered this?
I am including two files used below. If there is any other information that would be helpful, please let me know and I will provide it if I can. Thank!
Index.php
<?php
$url = "http://search.twitter.com/search.atom?q=SEARCHTERM&show_user=true&rpp=100";
$search = file_get_contents($url);
$regex_name = '/\<name\>(.+?) \(/';
preg_match_all($regex_name,$search,$user);
for($i=0;$user[1][$i];$i++)
{
$follow = $user[1][$i];
include("follow.php");
}
?>
Follow.php
<?php
define('TWITTER_CREDENTIALS', 'username:password');
$url = "http://twitter.com/friendships/create/".$follow.".xml";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_USERPWD, TWITTER_CREDENTIALS);
$result= curl_exec ($ch);
curl_close ($ch);
?>
Quick update on this: Turns out the problem was at the end of Twitter - the account was, for some reason, tighter than the usual API restrictions. I am not marking any answers as an answer as this was a rather unusual instance.
source to share
To debug your cURL request everything was said, but perhaps you could use Services_Twitter , which is a package wrapped around a Twitter -API. It provides fairly robust handling of responses.
Search and friendship are currently supported.
If you don't want to do this, then just for the Twitter-Search API, I would use JSON and do something like this:
<?php
$url = "http://search.twitter.com/search.json?q=SEARCHTERM&show_user=true&rpp=100";
$search = file_get_contents($url);
if ($search === false) {
die('Error occurred.');
}
$hits = json_decode($search);
var_dump($hits);
?>
It's less parsing, and ext / json is available in most PHP5 installations.
source to share