You are here:  » Quick Feed Question...

Support Forum



Quick Feed Question...

Submitted by drbigfresh on Fri, 2008-04-25 14:25 in

I am doing some stuff with the following feed:
http://www.google.com/reader/public/atom/user/16440287289307768570/state/com.google/broadcast

I can access all the info for the items perfectly, but I am trying to figure out how to get to the stuff at the beginning of the file such as:
generator
link
title
author/name
updated

and I am completely stuck.

Thanks!
Scott.

Submitted by support on Fri, 2008-04-25 15:46

Hi Scott,

Because the items at the beginning of the file are not part of each repeating record; they would need to be accessed by parsing the file with a higher level "Format String". As you will be currently using the following format string:

xml|FEED/ENTRY/

...the value required to access the top level elements would be:

xml|FEED/

The easiest way to use this is to have a separate record handler function for each level; for example:

<?php
  
function myHeaderRecordHandler($record)
  {
  }
  function 
myRecordHandler($record)
  {
  }
  
$url "http://....";
  
MagicParser_parse($url,"myHeaderRecordHandler","xml|FEED/");
  
MagicParser_parse($url,"myRecordHandler","xml|FEED/ENTRY/");
?>

If you need to use values from the header when processing each record; simply place the fields you need into global variables; declared as "global" in each of the record handler functions.

As a final point; the only drawback here is that it will involve requesting the feed twice which may not be desirable, both from a performance and resources on the remote server point of view. Therefore, what I would recommend is loading the XML into a string, and parsing the string instead of the URL directly; for example:

<?php
  
function myHeaderRecordHandler($record)
  {
    print 
$record["GENERATOR"];
  }
  function 
myRecordHandler($record)
  {
    print 
$record["TITLE"];
  }
  
$url "http://www.google.com/reader/public/atom/user/16440287289307768570/state/com.google/broadcast";
  
$xml "";
  
$fp fopen($url,"r");
  while(!
feof($fp)) $xml .= fread($fp,1024);
  
fclose($fp);
  
MagicParser_parse("string://".$xml,"myHeaderRecordHandler","xml|FEED/");
  
MagicParser_parse("string://".$xml,"myRecordHandler","xml|FEED/ENTRY/");
?>

Hope this helps!
Cheers,
David.

Submitted by drbigfresh on Sat, 2008-04-26 15:30

One last question.....
On the same feed, each entry has an optional set of elements like

<category term="mario kart"/>
<category term="mario kart wii"/>
<category term="MarioKart"/>
<category term="MarioKartWii"/>
<category term="wii"/>
<category term="wii wheel"/>
<category term="WiiWheel"/>

How can I loop thru those? It changes how many terms there are (if any) on each entry.

Thanks!
Your script is saving me hours of work.

Submitted by support on Sat, 2008-04-26 20:08

Hi,

Magic Parser will have resolved these duplicate entries by appending @1, @2.. etc. to the end of each additional field. In this case, the best way to handle it is simply to loop though $record looking for "CATEGORY-TERM" at the beginning of the field name and using the value as required; for example:

  foreach($record as $k => $v)
  {
    if (substr($k,"CATEGORY-TERM")===0)
    {
      print $v;
    }
  }

Hope this helps!
Cheers,
David.

Submitted by drbigfresh on Sat, 2008-04-26 20:45

Hmmm... If I do a print_r on $record, this is what I am getting:

[CATEGORY] =>
    [CATEGORY-TERM] => mario kart
    [CATEGORY@1] =>
    [CATEGORY@1-TERM] => mario kart wii
    [CATEGORY@2] =>
    [CATEGORY@2-TERM] => MarioKart
    [CATEGORY@3] =>
    [CATEGORY@3-TERM] => MarioKartWii
    [CATEGORY@4] =>
    [CATEGORY@4-TERM] => wii
    [CATEGORY@5] =>
    [CATEGORY@5-TERM] => wii wheel
    [CATEGORY@6] =>
    [CATEGORY@6-TERM] => WiiWheel

So I changed it to this:

foreach($record as $k => $v)
  {
 if (strpos($k, 'TERM' ) <> FALSE) {
   echo $v;
 }

which seems to be working, but I just wanted to double check and make sure there wasn't a better way to do it.

Thanks!

Submitted by support on Sat, 2008-04-26 20:48

Hi,

Sorry - yes that's fine - i'd misinterpreted how the field names would appear!

Cheers,
David.