0

I am using ubuntu server, testing to fetch an external url with wget and cURL in terminal via putty.

wget www.google.com

works fine and i got response.

curl -v www.google.com

works find and i got response

Now in my php script i have

$url = 'www.google.com';
$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, $url);
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlSession, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curlSession, CURLOPT_SSL_VERIFYPEER, 0);
$jsonData = curl_exec($curlSession);
curl_close($curlSession);

var_dump("curl without proxy ", $jsonData);
echo "<br>";
echo "<br>";

I am getting NULL as response

enter image description here

When i test it with a fresh server or localhost or another server, the code is working fine.

Is there any settings that need to be checked. In php.ini settings

allow_url_fopen is ON
Alaksandar Jesus Gene
  • 4,587
  • 9
  • 39
  • 62
  • I suggest you make sure errors are properly displayed (see e.g. https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display) and run it again to get an explicit error message, then you can start debugging. – shevron Apr 21 '21 at 08:21
  • Thanks. But no errors were thrown. Note, this is a working code on my local and other server – Alaksandar Jesus Gene Apr 21 '21 at 08:27
  • What have you tried to debug the problem? Like: anything? Remove all unneccessary parts from your code to see what happens – Nico Haase Apr 21 '21 at 08:58
  • 1
    Are you literally calling `www.google.com`? And if so, why would you expect JSON to be returned? – El_Vanja Apr 21 '21 at 09:00
  • www.google.com was an example. I am not sure we can publicly say the apis developers call. – Alaksandar Jesus Gene Apr 21 '21 at 10:20
  • @NicoHaase My question was exactly how to debug the problem, where no errors are thrown. I also confirm this is a working code and php file has no other codes – Alaksandar Jesus Gene Apr 21 '21 at 10:21
  • What else did you try? https://stackoverflow.com/questions/8227909/curl-exec-always-returns-false holds some hints about what to do if `curl_exec` returns false – Nico Haase Apr 21 '21 at 10:26
  • Thanks to all. That server was behind proxy. Once we define the port for proxy everything worked fine. – Alaksandar Jesus Gene Apr 27 '21 at 04:17

1 Answers1

0

I just tested your code and actually by removing the json_decode the result is showing. This is because you try to convert from json a html response. www.google.com does not return json but html.

You can see json_decode errors by adding the option JSON_THROW_ON_ERROR at the fourth parameter like this :

$jsonData = json_decode(curl_exec($curlSession),false, 512, JSON_THROW_ON_ERROR);

The final code looks like this without the json_decode :

$url = 'www.google.com';
$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, $url);
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlSession, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($curlSession, CURLOPT_SSL_VERIFYPEER, 0);
//This is where the problem was, the response wasn't json but html
$jsonData = curl_exec($curlSession);

curl_close($curlSession);

var_dump("curl without proxy ", $jsonData);
echo "<br>";
echo "<br>";

What i don't undestand however is how your code can work in another server. Are you calling another url ? Because www.google.com might just be an example for you to hide the real url of the server for company reasons

Nopraz
  • 34
  • 1
  • 5
  • Awesome. But did not work. As i mentioned, i was trying to debug on multiple servers. By mistake copy pasted the code from where i was expecting json data. I mentioned google.com since it is easy understanding – Alaksandar Jesus Gene Apr 21 '21 at 10:19