Jump to content

write from a url to array (Yahoo keyword suggestion)


etrader

Recommended Posts

With this link http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion?appid=YahooDemo&query=Madonna&results=2 , one can see suggested keywords for a query. The result is like "madonna 4 minutes madonna 4 minutes lyrics". How to put the resulting keywords in a $array, as can be used for further php coding.

:-\

 

What about:

 

$contents = file_get_contents('http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion?appid=YahooDemo&query=Madonna&results=2');
$dataArray = implode(' ', $contents);

 

You might need to urlencode that address:

$uri = urlencode('http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion?appid=YahooDemo&query=Madonna&results=2');
$contents = file_get_contents($uri);
...

I get this error

Warning: file_get_contents(http%3A%2F%2Fsearch.yahooapis.com%2FWebSearchService%2FV1%2FrelatedSuggestion%3Fappid%3DYahooDemo%26query%3DMadonna%26results%3D2) [function.file-get-contents]: failed to open stream: No such file or directory in

 

and this one

Warning: implode() [function.implode]: Invalid arguments passed in

Yep, my mistake. Turns out the url actually points to an XML document. This should give you some insight:

 

$xml = simplexml_load_file('http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion?appid=YahooDemo&query=Madonna&results=2');
var_dump($xml);

 

$dataArray = array();

$xml = simplexml_load_file('http://search.yahooapis.com/WebSearchService/V1/relatedSuggestion?appid=YahooDemo&query=Madonna&results=2');
foreach($xml->Result as $result)
{
$dataArray = array_merge($dataArray, explode(' ', (string) $result));
}

$dataArray = array_unique($dataArray); // Remove this line if you want duplicate values.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.