-1

I'm trying to create a widget which produces real-time data from popular cryptocurrencies. What I need is the symbol, name, price, and percent change in 24hours from 10 of the biggest gainers and 10 of the biggest losers in coins.

I'm using coinmarketcap's API documentation.

My code thus far is,

$API_KEY = "https://api.coinmarketcap.com/v2/ticker/?start=0&limit=100&sort=rank";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $API_KEY);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
$result = json_decode($server_output);




$dataForAllDays = $result['data'];
$dataForSingleCoin = $dataForAllDays['1'];
    echo $dataForSingleCoin['symbol']

and it's producing a blank page. This is my first time coding anything like this so any ideas, feedback, etc. is welcome!

Patrick Q
  • 6,373
  • 2
  • 25
  • 34

2 Answers2

0

You are doing no error checking at all and assuming that nothing can ever be wrong. Something has gone wrong.

You basically need to look up every function you use in the PHP manual (or product documentation if it isn't builtin) and determine what needs to be done to detect errors—or if error checking is needed at all.

For instance:

  1. curl_init()

    Returns a cURL handle on success, FALSE on errors.

    $ch = curl_init();
    if (!$ch) {
        // Error: abort and report
    }
    
  2. curl_setopt()

    Returns TRUE on success or FALSE on failure.

    if (!curl_setopt($ch, CURLOPT_URL, $API_KEY)) {
        // Error: abort and report
    }
    
  3. curl_exec()

    Returns TRUE on success or FALSE on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure.

    $server_output = curl_exec ($ch);
    if (!$server_output) {
        // Error: abort and report
    }
    
  4. json_decode()

    Returns the value encoded in json in appropriate PHP type. Values true, false and null are returned as TRUE, FALSE and NULL respectively. NULL is returned if the json cannot be decoded or if the encoded data is deeper than the recursion limit.

    […]

    In the event of a failure to decode, json_last_error() can be used to determine the exact nature of the error.

    $result = json_decode($server_output);
    if (is_null($result)) {
        // Error: abort and report, possibly calling json_last_error() or json_last_error_msg()
    }
    
  5. Etc.

Álvaro González
  • 142,137
  • 41
  • 261
  • 360
0

Few things i noticed in your code:-

1) In the last line "echo $dataForSingleCoin['symbol']" you are missing a semicolon i.e. ;
2) json_decode function returns a stdClass object for a JSON object, not an array but you are trying to access it as an array, for e.g. in $result['data'], it should be $result->data. If you want json_decode function to return a PHP array for JSON object as well, then please add the second parameter in the json_decode function as true, then it will return a PHP associated array for JSON Object as well. for ref. please look at http://php.net/manual/en/function.json-decode.php
3) Below code will give you some output and then you can take it from there:-

$API_KEY = "https://api.coinmarketcap.com/v2/ticker/?start=0&limit=100&sort=rank"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $API_KEY); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $server_output = curl_exec ($ch); curl_close ($ch); $result = json_decode($server_output, true); $dataForAllDays = $result['data']; $dataForSingleCoin = $dataForAllDays['1']; echo $dataForSingleCoin['symbol'];

Ankur Sharma
  • 187
  • 2
  • 14