Get data returned via json with PHP

1

Well, I tried to capture the URL data in php anyway: link But I can not, I tried cUrl, I tried to save the page returned, anyway, everything I tried was unsuccessful! Always returns false ...

$url = "https://api.cartolafc.globo.com/time/slug/artilheiroCoral/1";

$c = curl_init();
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_URL, $url);

curl_setopt($c, CURLOPT_FRESH_CONNECT, true);

$result = curl_exec($c);
curl_close($c);


var_dump($result);
    
asked by anonymous 02.05.2017 / 14:32

2 answers

1

To turn straight into Array you can do this:

   <?php 
function get_web_page( $url )
{
    $options = array(
        CURLOPT_RETURNTRANSFER => true,     // return web page
        CURLOPT_HEADER         => false,    // don't return headers
        CURLOPT_FOLLOWLOCATION => true,     // follow redirects
        CURLOPT_ENCODING       => "",       // handle all encodings
        CURLOPT_USERAGENT      => "spider", // who am i
        CURLOPT_AUTOREFERER    => true,     // set referer on redirect
        CURLOPT_CONNECTTIMEOUT => 120,      // timeout on connect
        CURLOPT_TIMEOUT        => 120,      // timeout on response
        CURLOPT_MAXREDIRS      => 10,       // stop after 10 redirects
        CURLOPT_SSL_VERIFYPEER => false     // Disabled SSL Cert checks
    );

    $ch      = curl_init( $url );
    curl_setopt_array( $ch, $options );
    $content = curl_exec( $ch );
    $err     = curl_errno( $ch );
    $errmsg  = curl_error( $ch );
    $header  = curl_getinfo( $ch );
    curl_close( $ch );

    $header['errno']   = $err;
    $header['errmsg']  = $errmsg;
    $header['content'] = $content;
    return $header;
}

$json = get_web_page('https://api.cartolafc.globo.com/time/slug/artilheiroCoral/1');
var_dump($json);
    
02.05.2017 / 14:41
0

You need to add a User-Agent, so that CURL becomes closer to an ordinary browser, view a list of UA here .

To define a User-Agent there are two ways, choose one :

Adding CURLOPT_USERAGENT ( -A ):

curl_setopt($c, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');

Via header (using CURLOPT_HTTPHEADER ):

curl_setopt($c, CURLOPT_HTTPHEADER, [
  'User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36'
]);

In any of the situations will send the header of User-Agent , which should be enough to connect, if not also add Referer , for example .

I'm completely ignoring security aspects related to the original code .     

02.05.2017 / 15:00