Thursday, 5 September 2013

Why url returns JSON data via browser but shows nothing with curl (CURLOPT_RETURNTRANSFER is true)?

Why url returns JSON data via browser but shows nothing with curl
(CURLOPT_RETURNTRANSFER is true)?

I want to get json data from here: JSON url;
Using Chrome I can see all JSON data, but using curl (below code) it seems
to redirect and get lost (if CURLOPT_FOLLOWLOCATION is false, it does
nothing):
$json_url =
'http://cartolafc.globo.com/mercado/filtrar.json?page=1&order_by=media&status_id=7&posicao_id=';
$ch = curl_init($json_url);
curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, true );
//curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, false );
//even killing the redirect process it does not return JSON data
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U;
Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915
Firefox/1.0.7");
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$data = curl_exec( $ch );
$dataDecoded = json_decode($json);
print_r($dataDecoded);
I saw something about simulating a browser with curl but I tought agent
would do the trick. Maybe something about server using cookies... I really
don't know. I saw other answers here today but they didn't solve my
problem. Am I missing something?
Thank you.

No comments:

Post a Comment