views:

578

answers:

2

I'm trying to make a simple site that uses php and the Twitter API to write out my latest post from twitter. I found a tutorial showing me how to get my timeline (or last 20 posts) in xml format, but can't figure out how to print out just the latest post, and just the text of it (not the time, date, etc that come with the xml/rss/etc).

My code looks like this

<h2 id = "latest">

    <?php
        var $username='myusername';  
        var $password='mypassword'; 
        var $responseInfo=array();

        function latest_status($format='json',$id=null) {
            $request = 'http://twitter.com/statuses/user_timeline.'.$format;
            if($id) {
                $postargs = "id=$id";
                return $this->process($request,$postargs);
            }
            return $this->process($request);
        }

        echo latest_status("json");
    ?>
</h2>

And it's returning this:

[{"contributors":null,"created_at":"Tue Feb 16 19:56:08 +0000 2010","in_reply_to_user_id":null,"source":"API","favorited":false,"in_reply_to_status_id":null,"truncated":false,"user":{"notifications":null,"profile_link_color":"0000ff","description":"","verified":false,"profile_background_tile":false,"created_at":"Tue Feb 16 01:16:15 +0000 2010","profile_background_color":"9ae4e8","profile_image_url":"http://s.twimg.com/a/1265999168/images/default_profile_1_normal.png","time_zone":"Hawaii","profile_sidebar_fill_color":"e0ff92","followers_count":0,"screen_name":"whisperingweb","lang":"en","friends_count":0,"profile_sidebar_border_color":"87bc44","statuses_count":2,"following":null,"protected":false,"favourites_count":1,"location":"","name":"Chris Armstrong","contributors_enabled":false,"profile_text_color":"000000","id":114608397,"geo_enabled":true,"profile_background_image_url":"http://s.twimg.com/a/1265999168/images/themes/theme1/bg.png","utc_offset":-36000,"url":null},"in_reply_to_screen_name":null,"geo":null,"id":9199090048,"text":"Someone was on your website"},{"favorited":false,"source":"web","in_reply_to_user_id":null,"created_at":"Tue Feb 16 18:50:21 +0000 2010","geo":null,"user":{"verified":false,"description":"","notifications":false,"profile_text_color":"000000","screen_name":"whisperingweb","profile_background_image_url":"http://s.twimg.com/a/1265999168/images/themes/theme1/bg.png","url":null,"profile_link_color":"0000ff","followers_count":0,"statuses_count":2,"profile_background_tile":false,"created_at":"Tue Feb 16 01:16:15 +0000 2010","friends_count":0,"profile_background_color":"9ae4e8","contributors_enabled":false,"time_zone":"Hawaii","favourites_count":0,"profile_sidebar_fill_color":"e0ff92","protected":false,"location":"","name":"Chris Armstrong","lang":"en","geo_enabled":true,"profile_sidebar_border_color":"87bc44","id":114608397,"following":false,"utc_offset":-36000,"profile_image_url":"http://s.twimg.com/a/1265999168/images/default_profile_1_normal.png"},"contributors":null,"in_reply_to_status_id":null,"id":9196705546,"in_reply_to_screen_name":null,"truncated":false,"text":"The quick brown fox jumps over the lazy dog"}]

I'm pretty new to php, and completely new to the Twitter API, so would appreciate any help or advice.

edit: Have changed example from xml to json

+2  A: 

Use format='json' instead: json_decode($response)[0]['text'].

In this case, use JSON because it has a much more natural mapping to PHP's internal datatypes, which makes it dead-easy to extract the content you want. You can still use XML, but you have to iterate over the resulting structure, which is relatively complex.

djc
Whats the benefit of JSON over XML?
Chris Armstrong
JSON can be parsed faster than XML *in php*.
poke
It's a standard you can access directly without caring about parsing the data (which can be quite cumbersome in terms of xml and even more raw html).
Femaref
Ok, so I wouldn't need an extra library for JSON like I would with XML? If so, how do I parse JSON directly?
Chris Armstrong
The following is throwing up a syntax error, don't suppose you can see where the problem is?return json_decode($this->process($request))[0]['text'];The error is unexpected '['
Chris Armstrong
You just use json_decode(), then $decoded[0]['text'].
djc
Maybe if you specify what error it throws?
djc
$decoded = json_decode($this->process($request)); return $decoded[0]['text'];// gives me Fatal error: Cannot use object of type stdClass as array. Sorry if this is a noob error
Chris Armstrong
Ah, should have been $decoded[0]->text
Chris Armstrong
+2  A: 

That's just your webbrowser displaying the contents of the xml file without tags. You'd need to use an xml library (e.g. simplexml, http://php.net/manual/en/book.simplexml.php) to get the informations you want.

edit to reflect OP change

Use the json_decode method (see http://www.php.net/manual/en/function.json-decode.php). The output can be accessed like an array.

For further questions, also, please use the documentation of PHP and google (I got the information about json by googleing "json in php").

Femaref
Any tips on how I would implement the library? Is it through the library that I would specify which post, and which elements of it, to display?
Chris Armstrong
you need to use the functions offered by the simplexml library (see http://www.php.net/manual/en/simplexml.examples-basic.php for a basic tutorial) and just get comfortable with it. I agree with djc though, use the json possibilty, it will spare you a lot of tears.
Femaref
Ok thanks, I've changed the example to be json instead of xml. What do I need to add to select just the latest post from the json data?
Chris Armstrong