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

当我运行这个函数时,我得到:

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

但我无法得到任何结果。谁能帮忙解释一下我做错了什么?

4

1 回答 1

0

Web Search API 文档中,谷歌说:

自 2010 年 11 月 1 日起,Google Web Search API正式弃用。根据我们的弃用政策,它在弃用日期后的三年多时间内运行。它的最后运行日期是 2014 年 9 月 29 日。我们鼓励您调查自定义搜索 API,它可能会提供替代解决方案。

于 2016-06-18T07:38:51.550 回答