EGP
EGP

Reputation: 397

PHP JSON Data Issue

I'm trying to fetch the Ask price from Bittrex API. I'm unable to get this data. I've tried both of the following and neither is working:

function bittrex_mco_btc(){

   $data = json_decode(getResource('https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-mco'),'TRUE');
   return $data->result->ask;

}

function bittrex_mco_btc(){

   $data = json_decode(getResource('https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-mco'),'TRUE');
   return $data['result']['Ask'];

}

I think I need to use 'result' because it's an associative array, but I have no idea what's wrong beyond that.

Oh, also here's the getResource:

function getResource($url){
$ch = curl_init();
// SETTING CURL OPTIONS
$timeout = 5;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

$data = curl_exec($ch);
curl_close($ch);
return $data;}

and here's the json:

{"success":true,"message":"","result":[{"MarketName":"BTC-MCO","High":0.00281740,"Low":0.00162000,"Volume":1490023.83431235,"Last":0.00216002,"BaseVolume":3208.40974458,"TimeStamp":"2017-08-19T04:06:23.39","Bid":0.00216001,"Ask":0.00219186,"OpenBuyOrders":953,"OpenSellOrders":3453,"PrevDay":0.00278935,"Created":"2017-07-02T00:37:16.957"}]}

Upvotes: 0

Views: 57

Answers (2)

JYoThI
JYoThI

Reputation: 12085

1st: you should access it like this . because your ask key is inside the 0'th index .

2nd: json_decode second parameter should be a Boolean value .but your given string.

Note: echo "<pre>"; print_r($data); Understand the structure of array.

PHP :

     $data = json_decode(getResource('https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-mco'),TRUE);

    // $data = json_decode(file_get_contents('https://bittrex.com/api/v1.1/public/getmarketsummary?market=btc-mco'),TRUE);

     return $data['result'][0]['Ask'];

    }


?>

Upvotes: 2

lluisrojass
lluisrojass

Reputation: 439

getResource is not a native php function is it from a library?

Upvotes: 0

Related Questions