我正在开发一个codeigniter
的应用程序。在我的应用程序中,需要与谷歌搜索集成。在我的应用程序中有一个文本框和一个提交按钮,如果用户搜索某些内容,它将在google中搜索数据并在我的页面中显示结果。我该怎么做?任何人的任何想法。
我已经成功使用了,
<?php
$query = "Steve Jobs";
$api_url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&&rsz=large&q=".$query;
$body = file_get_contents($api_url);
$json = json_decode($body);
for($x=0;$x<count($json->responseData->results);$x++)
{
echo "<b>Result ".($x+1)."</b>";
echo "<br>URL: ";
echo $json->responseData->results[$x]->url;
echo "<br>VisibleURL: ";
echo $json->responseData->results[$x]->visibleUrl;
echo "<br>Title: ";
echo $json->responseData->results[$x]->title;
echo "<br>Content: ";
echo $json->responseData->results[$x]->content;
echo "<br><br>";
}
在Codeigniter中,您可以为此创建一个辅助函数(如果要多次使用),
应用程序/助手/google_helper.php
<?php
function google_search($query)
{
$api_url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&&rsz=large&q=".$query;
$body = file_get_contents($api_url);
return json_decode($body);
}