0

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!

1
  • Nobody can tell without a concrete example and the json input data. Possibly charsets. Commented Dec 10, 2011 at 19:48

1 Answer 1

1

You are not doing any error checking after your calls, so if something goes wrong, you will never hear about it.

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm getting this using curl_error() `Warning: curl_error() expects exactly 1 parameter, 0 given in C:\wamp\www`
@andrew see the manual link for how it's used. echo curl_error($ch);
Nice, I figured it out. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.