简体   繁体   中英

google search api not returning results

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

When I run this function I get:

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 

But the same query in Google returns results even typing the generated query which is in address bar of browser returns valid results

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

But I am unable to get any results. Can anyone please help and explain what I am doing wrong?

In the Web Search API docs , Google says:

The Google Web Search API is officially deprecated as of November 1, 2010. Per our deprecation policy it operated for more than three years past its deprecation date. Its last day of operation was September 29, 2014. We encourage you to investigate the Custom Search API , which may provide an alternative solution.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM