0

PHPとGoogleAPIを使用して特定のWebサイト内を検索するにはどうすればよいですか?キーワードを検索するとき、ANDANDの結果がwebsite1.com必要website2.comですwebsite3.com。例えば:

<input name="search" value="keyword"> 
<input name="as_site" value="site1.com"> 
<input name="as_site" value="site2.com">
<input name="as_site" value="site3.com">

結果は、これら3つのWebサイトからのみ返されます。

4

1 に答える 1

1

注: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...
于 2012-07-07T15:25:41.063 に答える