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 docsで、Google は次のように述べています。

Google Web Search API は、2010 年 11 月 1 日に正式に廃止されました。Google の廃止ポリシーに従って、廃止日から 3 年以上にわたって運用されていました。運用の最終日は 2014 年 9 月 29 日でした。別の解決策を提供する可能性があるCustom Search APIを調査することをお勧めします。

于 2016-06-18T07:38:51.550 に答える