Read Json in PHP?

2

I have the following code in my PHP :

<?php

  $url = "https://api.cartolafc.globo.com/mercado/destaques";

  $response =  file_get_contents($url); 
  $jogadores = json_decode($response,true);

It should return the file json , where I get the information and put it on another page and it worked until a while back, but it stopped. and absolutely nothing has been changed.

How can I resolve this problem?

When I use:

var_dump(json_decode($response, true));

It only returns NULL

    
asked by anonymous 27.04.2017 / 01:07

1 answer

2

Set USERAGENT to Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0) , both with file_get_contents or curl :

With file_get_contents :

<?php

    $url = "https://api.cartolafc.globo.com/mercado/destaques";

    $options = array(
        'http' => 
          array(
            'method' => 'GET', 
            'user_agent' => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)', 
            'timeout' => 1
            )
    );

    $context = stream_context_create($options);
    $file = file_get_contents($url, false, $context);
    $sol = json_decode($file, true);    
    var_dump($sol);

With curl :

<?php

    $curl = curl_init();

    curl_setopt_array($curl, array(
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_URL => 'https://api.cartolafc.globo.com/mercado/destaques',
        CURLOPT_SSL_VERIFYPEER => FALSE,
        CURLOPT_USERAGENT => 'Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)'
    ));

    $result = curl_exec($curl); 
    $sol = json_decode($result, true);  
    var_dump($sol);

References:

27.04.2017 / 01:42