2012-07-07 3 views
0

PHP와 Google API를 사용하여 특정 웹 사이트 내에서 어떻게 검색합니까? 키워드를 검색 할 때 website1.comwebsite2.comwebsite3.com의 결과를 원합니다. 예 :PHP와 Google API를 사용하여 특정 웹 사이트 내에서 어떻게 검색합니까?

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

결과는이 세 웹 사이트에서만 반환되어야합니다.

답변

1

참고 : Google 웹 검색 API는 2010 년 11 월 1 일부터 공식적으로 지원 중단 될 예정입니다.> Google의 지원 중단 정책에 따라 계속 작동하지만> 요청할 수있는 하루 수는 제한됩니다 . 따라서 새 맞춤 검색 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... 
이라고 가정합니다.
관련 문제