0

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"}]}
2
  • Just edited it and shared that. Commented Aug 19, 2017 at 4:07
  • 1
    It should be json_decode($string, true). The second parameter to json_decode is a boolean not a string. Commented Aug 19, 2017 at 4:09

2 Answers 2

2

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'];

    }


?>
Sign up to request clarification or add additional context in comments.

3 Comments

Ah, the [0]! Can you please explain why I need the 0?
echo "<pre>"; var_dump($data); . because your ask key is inside the o'th index .
Thank you so much by the way! It worked. But I'd love an explanation for why I needed that 0 in between result and ask. I thought in an associative array, I'd just need result before.
0

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

1 Comment

I wrote it. I shared it. I have it in other functions getting from other resources and it seems to be working for them but not for Bittrex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.