简体   繁体   中英

How can I search within a specific website with PHP and the Google API?

How can I search within a specific website with PHP and the Google API? When I search for a keyword, I want the result from website1.com AND website2.com AND website3.com . For example:

<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">

The result should be returned from these three websites only.

Note: The Google Web Search API has been officially deprecated as of November 1, 2010. It >will continue to work as per our deprecation policy, but the number of requests you may make >per day will be limited. Therefore, we encourage you to move to the new Custom Search API.

According to your form and assuming that the keyword field is named keyword this sample code is assuming you have only one site to search you can customize it to include many. Assuming that the site input is named 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM