Import external URL array with PHP

0

This link link generates an array, would you like to import it into a local array, how do I?

I tried to do it:

$url = "https://api.coinmarketcap.com/v1/ticker/";

function curl_get_contents($url) {
    $ch = curl_init();
    $timeout = 5;

    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

    $data[] = curl_exec($ch);

    curl_close($ch);

    return $data;
}

$contents = curl_get_contents($url);

echo count($contents); // 1

But only one index with all the arrays inside it is created, the result is 1, I would like to keep the count number of $ contents for the number of indexes of the URL.

    
asked by anonymous 20.02.2018 / 21:07

1 answer

2

This content is in JSON, you were missing decode. Here's how:

$contents = json_decode(curl_get_contents($url));
print_r($contents);

This will generate an array of objects. If you need an array of associative arrays, pass true to the decoder in the second argument:

$contents = json_decode(curl_get_contents($url), true);
    
20.02.2018 / 21:09