I have written this simple piece of code :
$ch = curl_init();
//Set options
curl_setopt($ch, CURLOPT_URL, "http://www.php.net");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$website_content = curl_exec ($ch);
In my case $website_content
comes as false
.
Can anyone suggest/advice something what could be going wrong?
In my case I need to set VERIFYHOST
and VERIFYPEER
to false
, like this:
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
before the call to curl_exec($ch)
.
Because i am working between two development environments with self-assigned certificates.
With valid certificates there is no need to set VERIFYHOST
and VERIFYPEER
to false
because the curl_exec($ch)
method will work and return the response you expect.
This happened to me yesterday and in my case was because I was following a PDF manual to develop some module to communicate with an API and while copying the link directly from the manual, for some odd reason, the hyphen
from the copied link was in a different encoding and hence the curl_exec()
was always returning false
because it was unable to communicate with the server.
It took me a couple hours to finally understand the diference in the characters bellow:
https://www.e-example.com/api
https://www.e-example.com/api
Every time I tried to access the link directly from a browser it converted to something likehttps://www.xn--eexample-0m3d.com/api
.
It may seem to you that they are equal but if you check the encoding of the hyphens
here you'll see that the first hyphen
is a unicode characters U+2010 and the other is a U+002D.
Hope this helps someone.
Source: Stackoverflow.com