简体   繁体   中英

php curl for Google api not working but file_get_contents does

i am trying to do this

$url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
    . "q=Paris%20Hilton&userip=$_SERVER[SERVER_ADDR]";

// sendRequest
// note how referer is set manually
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_REFERER, 'http://google.com');
$body = curl_exec($ch);
curl_close($ch);

// now, process the JSON string
$json = json_decode($body);
// now have some fun with the results...

that's pretty much straight forward from their documents , but it does not seem to return anything but file_get_contents does for the same url just not https but http

i tried with only http for curl also but no luck. any help is appreciated guys :)

There will be a small issue and a notice generated by that, which should be changed:

$url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&"
. "q=Paris%20Hilton&userip=" . $_SERVER['SERVER_ADDR'];

apart from that, do you know you definitely have Curl extension enabled? do you get an error?

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