注:Google Web Search APIは、2010年11月1日をもって正式に非推奨になりました。これは、非推奨ポリシーに従って引き続き機能しますが、1日あたりのリクエスト数は制限されます。したがって、新しいカスタム検索APIに移行することをお勧めします。
フォームによると、キーワードフィールドに名前が付けられていると仮定すると、keyword
このサンプルコードでは、検索するサイトが1つしかないことを前提としており、多くを含めるようにカスタマイズできます。サイト入力に名前が付けられていると仮定します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...