Bin*_*ing 5 php api url search
简而言之,我正在寻找一种方法:
$results_array = google("search terms"); // returns array of URLs
Run Code Online (Sandbox Code Playgroud)
因此,举例来说,如果我的搜索词是"猫视频"我的$ results_array [0]可能是YouTube网址,而$ results_array 1可能是在Vimeo.
我已经看到了谷歌自定义搜索API,但它们都需要复杂的JSON的转换,ATOM,REST或其他一些系统这是我想要做的过于复杂.
有没有简单的解决方案?
编辑:我找到了它,感谢另一篇文章
感谢这篇文章,我能够弄明白.简而言之,我只是使用以下内容:
$results = json_decode(file_get_contents(
'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q='.
urlencode($search)));
echo $results->responseData->results[$resultNumber]->url;
Run Code Online (Sandbox Code Playgroud)
我对这些挫折感到有些惊讶和沮丧 - 这似乎是一个非常普遍的问题,实际上,这是一个非常简单的答案.这确实涉及JSON,但它对用户完全透明.也许对我提出的问题更准确的解决方案是:
function google($query) {
$results = json_decode(file_get_contents(
'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q='.
urlencode($search)));
return $results->responseData->results
}
Run Code Online (Sandbox Code Playgroud)
如果你想获得视频结果那么你可以尝试这个。
//replace space between words with +
$query = "cat+video";
$start = 0;
/*
this url will give you json response with 4 results each time.
u have to change the $start like 0, 4, 8,...
use json_decode() and get it in array
*/
$url = 'https://ajax.googleapis.com/ajax/services/search/video?v=1.0&q='.$query.'&start='.$start
Run Code Online (Sandbox Code Playgroud)
希望它会有所帮助。