注意:Google Web Search API 已于 2010 年 11 月 1 日正式弃用。根据我们的弃用政策,它将继续工作,但您每天可以发出的请求数将受到限制。因此,我们鼓励您迁移到新的自定义搜索 API。
根据您的表单并假设关键字字段被命名,keyword
此示例代码假设您只有一个要搜索的站点,您可以对其进行自定义以包含多个站点。假设站点输入被命名site
// The request also includes the userip parameter which provides the end
// user's IP address. Doing so will help distinguish this legitimate
// server-side traffic from traffic which doesn't come from an end-user.
$query = $_POST['keyword'];
$site = $_POST['site'];
$url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
. "q={$query}+Site:{$site}&userip=USERS-IP-ADDRESS";
// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, /* Enter the URL of your site here */);
$body = curl_exec($ch);
curl_close($ch);
// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...