Read Json in PHP?

Asked

Viewed 298 times

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 gather the information and assemble on another page and it worked until a while ago, but it stopped. and absolutely nothing was changed.

How can I solve this problem?


When I use:

var_dump(json_decode($response, true));

He only returns NULL

  • You’re not making the mistake failed to open stream using the function file_get_contents?

  • Gave yes :/ but with the reply of the friend below, it worked :)

  • In these cases, when the error message appears, put it along with the question, as it makes it easier for anyone to help you understand the problem. In this case it was not crucial, as it would be the most obvious mistake, but it is not always so, so do it in possible future questions.

  • Blz Anderson, but the issue of Failed to open error... only appeared on one of my servers, in the main where the application runs did not show this error, simply returned NULL without showing any error, thank you. hugging

  • It must be the configuration of this server to hide the error messages. Generally servers that run in production are configured like this to avoid leaking some information to malicious users.

1 answer

2


Configure the USERAGENT with the parameter Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0), so much 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:

  • Opa worked perfectly thanks a lot :) you think you better use file_get_contents or Curl ?

  • Another detail if you look --> https://api.cartolafc.globo.com/mercado/destaques will see the number of scales and the values do not hit kkk example ai in api the player Arrascaeta is with 478.000 scales but when I assemble the structure here --> www.fccartolaclub.com.br he appears with 480,000 not only he but the other tbm players, ta giving a difference of 2 to 3 thousand more

  • @Andréluizmardonis good to have helped, now the data obtained escapes from our programming, because it comes from an api, check next to the site the why of this, and if the problem has been solved accepted as a response!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.