I have a text file with 5,000 Twitter Users :-
JRJSHEARD
KMM_1979
ELMOCHLOE
ANNIEMMERSON
PATLOCKLEY
LISSYNUMBER
CAL32INCHSCREEN
PRINGLEDUDE
CORESMUSIC
I have found this API http://api.twitter.com/1/users/show.xml?screen_name=JRJSHEARD which开发者_如何学Python is really useful and just what I need.
How would I write a php function to loop through the user names in a text file and append their bio found between these tags (<description> </description>)
.
Is this possible? Any help would be gratefully received.
If you want to harvest user info in bulk from Twitter use users/lookup rather than users/show. The users/lookup API call returns 100 user objects at a time, and you can either pass the user IDs or the screen names when you make the call, however you will need to authenticate using OAuth in order to use it.
I recommend using JSON since it is a much more lightweight document format than XML. You will typically transfer only about 1/3 to 1/2 as much data over the wire, and I find that (in my experience) Twitter times-out less often when serving JSON.
http://api.twitter.com/1/users/lookup.json?screen_name=JRJSHEARD,KMM_1979,ELMOCHLOE
That's the direct API call, but if you're just starting out, what I would recommend is using a Twitter service implementation rather than try to do all the heavy lifting yourself. I'm not a PHP person, but my PHP-using Twitter buddies recommend Zend - http://framework.zend.com/manual/en/zend.service.twitter.html
$api = "http://api.twitter.com/1/users/show.xml?screen_name=";
$users = file("users.txt", FILE_IGNORE_NEW_LINES);
$i = 0;
foreach($users as $user){
$data = curl("$api$user");
preg_match("#<description>(.*?)</description>#is", $data, $matches);
$bio[$i]["user"] = $user;
$bio[$i]["description"] = $matches[1];
$i++;
}
function curl($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_close($ch);
return curl_exec($ch);
}
精彩评论