2011-04-02 161 views
0
function google_auth_dosearch($string) 
{ 
    $request = trim("http://ajax.googleapis.com/ajax/services/search/web"); 
    $referrer = trim("http://localhost/"); 
    //$results = 5; 
    $version = "1.0"; 
    //$output = 'php'; 
    //$type='phrase'; 

    // entire phrase, limit to 4 results 
    $getargs = 
     '?v='.$version. 
     '&key=ABQIAAAA4oH5MwaexHdhZg4UWRNB1RT2yXp_ZAY8_ufC3CFXhHIE1NvwkxTzUf4N43torAasiY6JD5CaJS6n7Q&rsz=small&q="'.urlencode 
     ($string).'"'; 
    echo $request.$getargs; // Get the curl 
    session object $session = curl_init($request.$getargs); // Set the GET options. 
    curl_setopt($session, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 
    5.1'); 
    curl_setopt($session, CURLOPT_HTTPGET, true); 
    curl_setopt($session, CURLOPT_HEADER, true); 
    curl_setopt($session, CURLOPT_RETURNTRANSFER, true); 

    $response = null; // Do the POST and then close the session 
    $response = curl_exec($session); 
    curl_close($session); 
    var_dump($response); // Get HTTP Status code from the response 
    $status_code = array(); 
    preg_match('/\d\d\d/', $response, $status_code); 
    echo "<br>"; 
    var_dump(json_decode($response, true)); 
    echo "<br>";  // Check for errors 
    switch ($status_code[0]) { 
    case 200: 
     echo "Success"; 
     break; 
    case 503: 
     return 
      ('Your call to Google Web Services failed and returned an HTTP 
    status of 503. That means: Service 
    unavailable. An internal problem 
    prevented us from returning data to 
    you.'); 
     //break; 
    case 403: 
     return 
      ('Your call to Google Web Services failed and returned an HTTP 
    status of 403. That means: Forbidden. 
    You do not have permission to access 
    this resource, or are over your rate 
    limit.'); 
     //break; 
    case 400: 
     // You may want to fall through here and read the specific XML error 
     return 
      ('Your call to Google Web Services failed and returned an HTTP 
    status of 400. That means: Bad 
    request. The parameters passed to the 
    service did not match as expected. The 
    exact error is returned in the XML 
    response.'); 
     //break; 
    default: 
     return ('Your call to Google Web Services returned an unexpected HTTP 
    status of:'.$status_code 
      [0]); 
    } 
    // Get the serialized PHP array from the response, bypassing the header 
    if (!(preg_match('^{"response.*}^', $response, $json))) { 
     $json = null; 
     print_r($response); 
    } 

    $results = json_decode($json[0]); 
    $urls = array(); 
    // for now let's not suport highlighting by attaching query string as 
    // it will break our unique call later 
     if (!$results->responseData->results) { 
     $message = "Invalid \n".$request; 
     print_r($response); 
     die($message); } 
    foreach($results->responseData->results as $result) { 
     if (substr($result->url, -4) == ".pdf") 
      $urls[] = $result->cacheUrl; 
     else 
      $urls[] = $result->url; 
    } 
    return $urls; 
} 

当我运行这个功能,我得到:谷歌搜索API不返回结果

SuccessHTTP/1.1 200 OK 
Pragma: no-cache Expires: Fri, 01 Jan 1990 00:00:00 GMT 
Date: Sat, 02 Apr 2011 09:23:27 GMT 
Cache-Control: max-age=0, must-revalidate 
Content-Type: text/javascript; charset=utf-8 
X-Embedded-Status: 200 
X-Content-Type-Options: nosniff 
X-Frame-Options: SAMEORIGIN X-XSS-Protection: 1; mode=block Server: GSE 
Transfer-Encoding: chunked 

{"responseData": {"results":[],"cursor":{"moreResultsUrl":"http://www.google.com/search?oe\u003dutf8\u0026ie\u003dutf8\u0026source\u003duds\u0026start\u003d0\u0026hl\u003den\u0026q\u003d%22What+is+the+best+tool+to+do+plagiarism+checking?%22"}}, "responseDetails": null, "responseStatus": 200} 
Invalid http://ajax.googleapis.com/ajax/services/search/web 

但谷歌在同一查询返回的结果,即使输入生成的查询是在浏览器的地址栏中返回有效结果

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&rsz=small&q=%22What+is+the+best+tool+to+do+plagiarism+checking 

但我无法得到任何结果。任何人都可以请帮忙解释我做错了什么?

+1

请仅**将您的代码缩进4个空格**以将其标记为代码。我解决了大部分问题,但您需要在脚本中修复换行符。 – deceze 2011-04-02 09:42:27

+0

谢谢......对不起,麻烦......请记住 – shazia 2011-04-02 09:49:35

回答