-1

I want to get results from Google. According to web API doc of Google this is my code.

<!DOCTYPE html>
<html>
<head>
    <title>testing</title>
</head>
<body>
    <?php
        $page = 10;
        $query = "Baby";
        $url = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=".$query;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; .NET CLR 1.1.4322)');
        $body = curl_exec($ch);
        curl_close($ch);
        $json = json_decode($body);
        var_dump($json);
        echo $json;
    ?>
</body>
</html>

But I am not getting anything on page neither error. Can anyone help me to figure it out why its not working and how to catch errors?

Anwar
  • 1
  • 1
  • Have you checked error log? Have you checked that curl extension installed on your server? – u_mulder Jan 17 '15 at 15:44
  • You have to check for errors with curl itself. Check [`echo curl_error($ch);`](http://php.net/manual/en/function.curl-error.php) before you close the handle. – Michael Berkowski Jan 17 '15 at 15:44
  • A blank page in PHP when output is expected, means _go look in your error log_. Always when developing and testing code, at the top of your script `error_reporting(E_ALL); ini_set('display_errors', 1);` – Michael Berkowski Jan 17 '15 at 15:45
  • 1
    Or simply use: $json = file_get_contents("https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=" . $query ); – Adib Jan 17 '15 at 15:45

2 Answers2

0

Can be thousand of reasons. Like, inability of cURL to establish an outbound connection because of some server misconfiguration....

Try looking at the page source. Is it displaying the </body> there? If not, then its a fatal error you don't see. Display errors ini_set('display_errors',1) to see them.

Use CURLOPT_VERBOSE to see what's happening. Try the following:

curl_setopt($curlhandle, CURLOPT_VERBOSE, true);
$verbose = fopen('php://temp', 'rw+');
curl_setopt($curlHandle, CURLOPT_STDERR, $verbose);
Oleg Dubas
  • 2,320
  • 1
  • 10
  • 24
0

The only error is that you are trying to echo object, returned by json_decode.

<b>Catchable fatal error</b>:  Object of class stdClass could not be converted to string in ...
shukshin.ivan
  • 11,075
  • 4
  • 53
  • 69
  • Well, I think, the error is not what you are looking for :) You should create output out of $json by yourself. It doesnt contain html markup, only clean data. – shukshin.ivan Jan 17 '15 at 15:55