Reputation: 8585
I'm having problem with CURL from a link. I'm able to get an output with file_get_contents();
But having problems with CURL
use json_decode
I get a NULL with cURL
, but with file_get_contents()
I get an Array
Using cURL
$url="https://example.com/"
$ch= curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$json= json_decode(curl_exec($ch),true);
echo $json; //outputs NULL
Using file_get_contents();
$json_pi = file_get_contents($url);
echo json_decode($json_pi,true);
Can anyone help me understand cURL? And why I might be getting these two conflicting results?
Thank you!
Upvotes: 0
Views: 1430
Reputation: 449395
You are not doing any error checking after your calls, so if something goes wrong, you will never hear about it.
Check the result of the CURL call using curl_error()
Check the result of the json_encode() call using json_last_error()
(PHP >= 5.3)
one of these will probably reveal what the problem is. For example, it could be that the curl call fetches the data in a non-UTF-8 character set, which will cause json_decode()
to break - it expects UTF-8 data at all times.
Upvotes: 1