tags:

views:

1861

answers:

3

I have this PHP script that I wrote to automatically follow users that post messages with certain terms. It works 100% of the time on a bunch of test accounts but then does not work on the account I'd like to use it with.

I've checked the account's API rate limit and it's well within the boundaries. I've also verified that the username and password are correct. If I change nothing else but the username and password to another account it will work, but when changed back (correctly) to the main account nothing happens. I am totally baffled. Has anyone ever come across this?

I'm including the two files used below. If there's any other info that would be helpful, let me know and I'll provide it if I can. Thanks!

Index.php

<?php
$url = "http://search.twitter.com/search.atom?q=SEARCHTERM&amp;show_user=true&amp;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 on Twitter's end--the account in question had tighter than normal API limits imposed for some reason. I'm not marking any responses as the answer since it was a rather idiosyncratic instance.

A: 

Could it be the characters in your username or password? Maybe you have some single quotes or something like that? Do you get any kind of error responses from curl or php?

D-Rock
A: 

Try using:

curl_setopt($ch,     CURLOPT_VERBOSE, 1);

to and see if it helps identifying the problem.

+2  A: 

To debug your cURL request everything has been said, but maybe you could use Services_Twitter, which is a package wrapped around the Twitter-API. It provides pretty robust response handling.

Currently supports search and friendships also.

If you don't want to do that, then just for the Twitter-Search API, I'd use JSON and do something like this:

<?php
$url    = "http://search.twitter.com/search.json?q=SEARCHTERM&amp;show_user=true&amp;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.

Till
Till, thanks for showing me the json_decode function! This is way easier to get up and running than the XML parsing libraries I've tried.
Evan