谷歌搜索API没有返回结果

时间:2011-04-02 09:30:53

标签: php google-search-api

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;
}

当我运行此功能时,我得到:

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 

但即使在浏览器的地址栏中键入生成的查询,Google中的相同查询也会返回结果

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

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

1 个答案:

答案 0 :(得分:0)

Web Search API docs中,Google说:

  

截至2010年11月1日,Google网页搜索API为officially deprecated。根据我们的deprecation policy,它在弃用日期之后运营了三年多。它的最后一天是2014年9月29日。我们鼓励您调查Custom Search API,这可能会提供替代解决方案。