tags:

views:

511

answers:

2

I want to do something very simple, but it seems the way of doing this simple task is impossible. I need to pull my twitter home timeline. NOT all my recent tweets, but the tweets of the people I follow. I'm hoping this can be done with PHP, but I have no idea. Any help is appreciated, and please, talk to me like I'm a noob - be thorough lol.

+6  A: 

Take a look at this page: http://apiwiki.twitter.com/Things-Every-Developer-Should-Know

You'll have to use curl in PHP.

The tricky part is the curl options. You can see how I used them in the example below.

The part you want from the apiwiki is the example in answer #8. Specifically:

Get updates from users you follow in XML, authenticated: curl -u username:password http://api.twitter.com/1/statuses/friends_timeline.xml

Here's the friends timeline docs. You can get the info you want in XML, JSON, RSS, or Atom form. JSON would probably be the easiest, since you can parse that simply with PHP.

Ok, to turn this into PHP you can use this:

<?php
// create a new cURL resource
$curl = curl_init();
// set URL and other appropriate options
$options = array(CURLOPT_URL => 'http://api.twitter.com/1/statuses/friends_timeline.json',
                 CURLOPT_HEADER => true,
                 CURLOPT_USERPWD => 'YOUR_USERNAME:YOUR_PASSWORD'
                );            

// set URL and other appropriate options
curl_setopt_array($curl, $options);
// grab URL and pass it to the browser
curl_exec($curl);
// close cURL resource, and free up system resources
curl_close($curl);
?>

Just tesed it on my account. The above code gives you you're friends' updates in JSON form.

You may not need the header. You can just leave out the "CURLOPT_HEADER => true," line, if you don't.

Edit:

Of course a pile of JSON is only so useful.... Here's an example of how to change to code above to take the JSON and print certain chosen items out in a human readable form:

<?php
// create a new cURL resource
$curl = curl_init();
// set URL and other appropriate options
$options = array(CURLOPT_URL => 'http://api.twitter.com/1/statuses/friends_timeline.json',  
                 CURLOPT_USERPWD => 'USERNAME:PASSWORD',
                 CURLOPT_RETURNTRANSFER => true
                );            

// set URL and other appropriate options
curl_setopt_array($curl, $options);
// grab URL and pass it to the browser
$json = curl_exec($curl);
// close cURL resource, and free up system resources
curl_close($curl);
$obj = json_decode($json);    
foreach($obj as $var => $value)
{
    echo "Message number: $var <br/>";    
    echo "Name: " . $obj[$var]->user->name;
    echo "Handle: " . $obj[$var]->user->screen_name . "<br/>";        
    echo "Message: " . $obj[$var]->text;        
    echo "Created" . $obj[$var]->created_at . "<br/>";                    
    echo "URL" . $obj[$var]->user->url . "<br/>";
    echo "Location" . $obj[$var]->user->location . "<br/>";       
    echo "<br/>";
}
?>
Peter Ajtai
[Basic authentication is set to be removed from the Twitter API this month](http://apiwiki.twitter.com/OAuth-FAQ#WhenareyougoingtoturnoffBasicAuth). Use OAuth if you want this to keep working for more than a few weeks.
Matchu
A: 

Use an oAuth library specifically designed for Twitter such as twitteroauth to access this.

The library itself comes with examples. You might be able to adapt some of Peter's code for this library.

MiffTheFox