views:

391

answers:

2

Hi, I am trying to create a social timeline. I pull in feeds form certain places so I have a timeline of thing I have done. The problem I am having is with Google reader Shared Items.

I want to get the time at which I shared the item which is contained in <entry gr:crawl-timestamp-msec="1269088723811"> Trying to get the element using $date = $xml->entry[$i]->link->attributes()->gr:crawl-timestamp-msec; fails because of the : after gr which causes a PHP error. I could figure out how to get the element, so thought I would change the name using the code below but it throws the following error

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "<?xml version="1.0"?><feed xmlns:idx="urn:atom-extension:indexing" xmlns:media="http://search.yahoo.com/mrss/" xmlns

<?php

$get_feed = file_get_contents('http://www.google.com/reader/public/atom/user/03120403612393553979/state/com.google/broadcast');

    $old = "gr:crawl-timestamp-msec";
    $new  = "timestamp";

    $xml_file = str_replace($old, $new, $get_feed);

    $xml = simplexml_load_file($xml_file);
    $i = 0;


        foreach ($xml->entry as $value)
        { 

            $id = $xml->entry[$i]->id;
            $date = date('Y-m-d H:i:s', strtotime($xml->entry[$i]->attributes()->timestamp ));
            $text = $xml->entry[$i]->title;
            $link = $xml->entry[$i]->link->attributes()->href;
            $source = "googleshared";

            echo "date = $date<br />";

            $sql="INSERT IGNORE INTO timeline (id,date,text,link, source) VALUES ('$id', '$date', '$text', '$link', '$source')";
            mysql_query($sql);

            $i++;
        }`

Could someone point me in the right direction please.

Cheers

Craig

A: 

I'm not sure whether this addresses your issue 100% but the way to address variables like the one you mention is

$date = $xml->entry[$i]->link->attributes()->{"gr:crawl-timestamp-msec"};

maybe that already helps.

Pekka
Hi Pekka, Using that code doesn't throw an error but it doesn't pull any information through.
Craig Ward
+1  A: 

The problem is because crawl-timestamp-msec is in a different namespace. Somewhere in the document (usually the root element, which looks to be <feed/> in your case), it will have an attribute along the lines of xmlns:gr="http://some/url/here". This says that the document will be using things from the http://some/url/here namespace, and will prefix all of these things with gr.

[Edit: the URL in question is http://www.google.com/schemas/reader/atom/]

To access it, you need to change

$xml->entry[$i]->link->attributes()->gr:crawl-timestamp-msec

to

$xml->entry[$i]->attributes('http://www.google.com/schemas/reader/atom/')->{'crawl-timestamp-msec'}

(Edit: the attribute is on the <entry/> element, not the <link/>, it seems)

Chris Smith
Hi Chris, Thanks for the prompt reply. Knew it was something todo with namespaces form the IBM website but couldn't figure it out. I have replaced what you said but it throws up another error `Notice: Use of undefined constant timestamp - assumed 'timestamp' in /Users/craigward/Dropbox/Websites/wip/cron/get_feed_data.php on line 52Notice: Use of undefined constant msec - assumed 'msec' in /Users/craigward/Dropbox/Websites/wip/cron/get_feed_data.php on line 52date = 0`
Craig Ward
@Craig Use Chris's answer in combination with mine (Using the curly brackets and quotes) and it should work.
Pekka
And +1 - I knew there was more to it :)
Pekka
Hi Guys, Cheers for the help so far.I have tried:$date = $xml->entry[$i]->link->attributes('http://www.google.com/schemas/reader/atom/')->{"crawl-timestamp-msec"};This doesn't throw any errors but doesn't pull any data.$date = $xml->entry[$i]->link->attributes('http://www.google.com/schemas/reader/atom/')->{crawl-timestamp-msec};This throws the following error:Notice: Use of undefined constant crawl - assumed 'crawl' in /Users/craigward/Dropbox/Websites/wip/cron/get_feed_data.php on line 52Notice: Use of undefined constant timestamp - assumed 'timestamp'Not sure where to go
Craig Ward
@Craig Taking a look at the feed, the crawl-timestamp-msec is on the entry itself not on the link element? So you'll need $xml->entry[$i]->attributes(...)->{'crawl-timestamp-msec'}
Chris Smith
@chris, not sure what you mean by ->attributes(...)-> , sorry, bit of a newbie at xml. do the dots represent something?
Craig Ward
@Craig Pass the URL to attributes() as before, just on the `entry` itself rather than the `link` element. The dots were me being lazy :). I've updated the answer.
Chris Smith
Excellent, Cheers Chris. That has worked a charm. Thanks for all the help :)
Craig Ward