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
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.
How can i get the output of that function = a variable to be used elsewhere outside the function?
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.
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)?
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:
<?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.
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?
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 />";
?>
You can of course do the matching with regexp if required...
Hope this helps!
Cheers,
David.
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...