You are here:  » How to grab something from yahoo xml


How to grab something from yahoo xml

Submitted by murph on Sat, 2006-10-21 12:23 in

How can i get the totalresultsavailable from the yahoo api search results (xml)

http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=yahoodemo&query=www.test.com&results=2

Submitted by murph on Sat, 2006-10-21 12:39

Something like this:

  function myMetaDataHandler($record)
  {
    print "<p>Total Results: ".$record["TOTALRESULTSAVAILABLE"]."</p>";
  }
  function myRecordHandler($record)
  {
    print_r($record);
  }
  // extract meta data
  MagicParser_parse("http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=yahoodemo&query=test.com&results=2","myMetaDataHandler","xml|RESULTSET/");
  // extract records
  MagicParser_parse("http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=yahoodemo&query=test.com&results=2","myRecordHandler","xml|RESULTSET/RESULT/");

Although that doesn't print the total results...

Submitted by support on Sat, 2006-10-21 15:06

Hi,

In this instance of the Yahoo API, the Total Results Available is an attribute of the RESULTSET element, so you need to look at the following variable:

$record["RESULTSET-TOTALRESULTSAVAILABLE"];

The following should work:

  function myMetaDataHandler($record)
  {
    print "<p>Total Results: ".$record["RESULTSET-TOTALRESULTSAVAILABLE"]."</p>";
  }
  function myRecordHandler($record)
  {
    print_r($record);
  }
  // extract meta data
  MagicParser_parse("http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=yahoodemo&query=test.com&results=2","myMetaDataHandler","xml|RESULTSET/");
  // extract records
  MagicParser_parse("http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=yahoodemo&query=test.com&results=2","myRecordHandler","xml|RESULTSET/RESULT/");

Cheers,
David.

Submitted by murph on Sat, 2006-10-21 18:31

thanks...

Submitted by murph on Sat, 2006-10-21 20:54

How can i get the output of that function = a variable to be used elsewhere outside the function?

Submitted by support on Sun, 2006-10-22 04:36

Hi,

You just need to put the value into a global variable; for example:

  function myMetaDataHandler($record)
  {
    global $totalResultsAvailable;
    $totalResultsAvailable = $record["RESULTSET-TOTALRESULTSAVAILABLE"];
  }
  function myRecordHandler($record)
  {
    print_r($record);
  }
  // extract meta data
  MagicParser_parse("http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=yahoodemo&query=test.com&results=2","myMetaDataHandler","xml|RESULTSET/");
  // extract records
  MagicParser_parse("http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=yahoodemo&query=test.com&results=2","myRecordHandler","xml|RESULTSET/RESULT/");
  print "Total Results: ".$totalResultsAvailable;

Cheers,
David.

Submitted by murph on Sun, 2006-10-22 12:36

Ecsellent, cheers! :)

Submitted by murph on Fri, 2006-10-27 14:24

I need to be able to read the yahoo xml and loop through each using pregmatch to find a url. Is this possible with magic parser (not the pregmacth bit, just the loop and stop each result)?

Submitted by support on Fri, 2006-10-27 15:40

Hi,

You can do what you like to process each record in your second record handler function. For example, the following code prints out the DISPLAYURL of each result:

View Output

<?php
  
require("MagicParser.php");
  function 
myMetaDataHandler($record)
  {
    global 
$totalResultsAvailable;
    
$totalResultsAvailable $record["RESULTSET-TOTALRESULTSAVAILABLE"];
  }
  function 
myRecordHandler($record)
  {
    print 
$record["DISPLAYURL"]."<br />";
  }
  
// extract meta data
  
MagicParser_parse("http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=yahoodemo&query=test.com&results=2","myMetaDataHandler","xml|RESULTSET/");
  
// extract records
  
MagicParser_parse("http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=yahoodemo&query=test.com&results=2","myRecordHandler","xml|RESULTSET/RESULT/");
  print 
"Total Results: ".$totalResultsAvailable;
?>

You can access any of the other URLs in the result in exactly the same way, for example $record["CLICKURL"]

Hope this helps;
Cheers,
David.

Submitted by murph on Fri, 2006-10-27 20:58

This is a different code than the earlier totalresults example. I'm now looking to loop through each , check it over and then move onto the next if i haven't found what i'm looking for. I can't see how to do that?

Submitted by support on Sat, 2006-10-28 10:26

Hi,

You can tell Magic Parser to stop parsing by returning TRUE from your myRecordHandler function. Assuming that we're still talking about the same Yahoo! URL (the second one in the examples in this thread), then, for example, if you want to look for the URL containing speedtest and then stop parsing, you could do that like this:

<?php
  
require("MagicParser.php");
  function 
myMetaDataHandler($record)
  {
    global 
$totalResultsAvailable;
    
$totalResultsAvailable $record["RESULTSET-TOTALRESULTSAVAILABLE"];
  }
  function 
myRecordHandler($record)
  {
    global 
$urlFound;
    if (
strpos($record["DISPLAYURL"],"speedtest"))
    {
      
$urlFound $record["DISPLAYURL"];
      return 
true;
    }
  }
  
// extract meta data
  
MagicParser_parse("http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=yahoodemo&query=test.com&results=2","myMetaDataHandler","xml|RESULTSET/");
  
// extract records
  
MagicParser_parse("http://api.search.yahoo.com/WebSearchService/V1/webSearch?appid=yahoodemo&query=test.com&results=2","myRecordHandler","xml|RESULTSET/RESULT/");
  print 
"Total Results: ".$totalResultsAvailable."<br />";
  print 
"URL Found: ".$urlFound."<br />";
?>

View Output

You can of course do the matching with regexp if required...
Hope this helps!
Cheers,
David.