Reputation: 1
First time using curl in php. After carefully reading the documentation I'm just not getting any response, already checked the api with postman and phpinfo() is showing that curl is enabled.
I've tried it on both these rest api's:
My code:
$ch = curl_init('https://jsonplaceholder.typicode.com/users');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$users = json_decode($result);
curl_close($ch);
print_r($users);
Results:
var_dump($result) // returned bool(false)
var_dump($users) // returned null
Upvotes: 0
Views: 3330
Reputation:
It seems link your code is working : https://paiza.io/projects/g-ahrlYeA8tY_c-Y7UigkA
Try :
file_get_contents('https://jsonplaceholder.typicode.com/users');
If its loading then i think there is a problem with your php-curl lib
Try to load other urls and check if same problem persists.
Here is a sample curl function which works most of the time, try this if it works.
function &web_curl_http($url)
{
$c = curl_init();
curl_setopt( $c , CURLOPT_URL , $url);
curl_setopt( $c , CURLOPT_USERAGENT, "Mozilla/5.0 (Linux Centos 7;) Chrome/74.0.3729.169 Safari/537.36");
curl_setopt( $c , CURLOPT_RETURNTRANSFER, true);
curl_setopt( $c , CURLOPT_SSL_VERIFYPEER, false);
curl_setopt( $c , CURLOPT_SSL_VERIFYHOST, false);
curl_setopt( $c , CURLOPT_TIMEOUT, 10000); // 10 sec
$data = curl_exec($c);
curl_close($c);
return $data;
}
Upvotes: 1