Retrieve data returned via json with PHP

Asked

Viewed 777 times

1

Guys, I’ve tried in every way to capture with php the URL data: https://api.cartolafc.globo.com/time/slug/artilheiroCoral/1 But I can’t, I tried by Curl, I tried to save the returned page, 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);
  • Since you have tried in many ways I think it would be interesting to include what your attempts have been, after all it is impossible to know what you have tried.

2 answers

1

To turn direct into Array can do so :

   <?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);
  • Actually I can’t even recover the data..

  • Which error appears ?

  • vardump return is false

  • I set and tested working :)

  • Thank you very much! Working perfectly.

  • Gives an up on the answer to rank her.

Show 1 more comment

0

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


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

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, that should be enough to connect, if do not add also the Referer, for example.

I am completely ignoring security aspects related to the original code.

Browser other questions tagged

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