views:

20

answers:

2

Hi,

I am trying to get a list of Twitter users using their API. When I query the API in my browser (http://api.twitter.com/1/statuses/followers.xml?screen_name=atomictom), it returns an XML doc with 100 users, as it should.

However, when performing the query from my php file:

$file=fopen("http://api.twitter.com/1/statuses/followers.xml?screen_name=atomictom", "r");
$xmlString=fread($file,13421772);
fclose($file);
echo $xmlString; 

it only returns 1, and sometimes 2 users. It actually varies when I refresh! Any ideas on why this would happen? I suspect a problem with fopen or fread. Unfortunately, in fread I cannot use filesize($file), as it is a resource and not a string.

Thank you so much for your help!

A: 

Are you sure your PHP code is logged in? It seems likely that when you try it from the browser, it's using your own logged in cookie, but your PHP code doesn't have access to that.

zigdon
Thanks for the reply. I'm actually not logged in to Twitter and have no Twitter cookies in my browser. Also, I don't believe you have to be logged in to do the /statuses/followers call. The variance in what the file returns (sometimes 1 user, sometimes 1 incomplete user, etc.) makes me think it's not a Twitter issue, but one on my side. Any ideas?
Aaron Marks
A: 

You could use file_get_contents() instead. There is no need to pass a file size arg to it, and it will work well if you have allow_url_fopen = On set in your php.ini file.

<?php
    echo file_get_contents("http://api.twitter.com/1/statuses/followers.xml?screen_name=atomictom");
?>
PureForm
Thanks so much! This worked perfectly.
Aaron Marks