1

I usually manage to figure stuff out by myself, but on this occasion I've had to register an account and ask for help before I jump out the window.

I'm trying to output some basic JSON data to php, all I need to do is echo it out, the rest I'll figure out.

The API gives this guide:

{
 "success" : true,
 "message" : "",
 "result" : {
 "Bid" : 2.05670368,
 "Ask" : 3.35579531,
 "Last" : 3.35579531
 }
}

An example of the URL I'll be using: https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC

All I want to output is the 'Last' data, I don't care about the rest, keeping the decimal in the right place is also important.

I've tried all sorts, I can't get it to output it properly :(. I've ran a var_dump which spits out:

array(3) { ["success"]=> bool(true) ["message"]=> string(0) "" ["result"]=> array(3) { ["Bid"]=> float(0.00011505) ["Ask"]=> float(0.000116) ["Last"]=> float(0.00011505) } }

If someone could just tell me the few lines of code to put the 'Last' number into a variable called $lastBid I will love you long time!

Thanks guys!

War10ck
12.5k7 gold badges46 silver badges56 bronze badges
asked Aug 8, 2014 at 21:06
3
  • "I've tried all sorts [...]" - really? show us. Commented Aug 8, 2014 at 21:09
  • Really!? You want to go down that path...foreach ($data->result as $result[]) { //printf('%.9F',$result[2]); // echo $result[2]; //} Commented Aug 8, 2014 at 21:14
  • $data = json_decode($json, true); var_dump($data); echo $data->result[1]; Commented Aug 8, 2014 at 21:15

5 Answers 5

2

use json_decode - php method to decode json

http://php.net/manual/en/function.json-decode.php

$json = '{"foo-bar": 12345}';
$obj = json_decode($json);
print $obj->{'foo-bar'}; // 12345
answered Aug 8, 2014 at 21:08

Comments

0

Here you have an example,

$contents = file_get_contents("https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC");
$json = json_decode($contents); 
$lastBid = $json->result->Last; 

$lastBid would then be set to 0.01189802

answered Aug 8, 2014 at 21:09

3 Comments

I've got that far, I just can't figure out how to access the result->last data.
Thanks a lot for your help by the way! If you could show me how to throw the 'Last' data into a variable you have solved my woes!
With a printf("%0.6f", $lastBid); thrown in, that's got it!! XD You sir, deserve a bow! Thanks alot
0

You need to json_decode() the result.

answered Aug 8, 2014 at 21:09

Comments

0

that kind of data is called json. php permits you to convert that json (which is a string) to a php array. to get it, you need to convert it and then access to the value you'd like using array rules.

// save in a variable the data you're going to process
$json = '{
 "success" : true,
 "message" : "",
 "result" : {
 "Bid" : 2.05670368,
 "Ask" : 3.35579531,
 "Last" : 3.35579531
 }
 }';
// json_decode is a function that allows you to obtain an array 
// (the second parameter set to true indicates that the array'll be an associative one) 
$data = json_decode($json, TRUE);
/*
every php array has an internal pointer which points to a position in the array.
the end pointer, if not moved, points to the last position. to access to the value
you want, first get the last value (an array called "result"),
then access to the last value of that array (called "last").
the property you'll get is the float value you requested!
*/
var_dump(
 end(
 end($data)
 )
 );

and here, there is the output:

float(3.35579531)
answered Aug 8, 2014 at 21:29

Comments

0

Access it by:

$url = 'https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC';
$data = file_get_contents($url);
$data = json_decode($data); 
$last = $data->result->Last; 

If you like using arrays instead of object orrientation style, json_decode has an extra boolean param, that converts it too an array if you feel more comfortable using that.

$url = 'https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC';
$data = file_get_contents($url);
$data = json_decode($data,true); 
$last = $data['result']['Last']; 

Side note; For accessing API's I would rather advice you to use curl instead of file_get_contents. It gives you better control, for instance with timeouts. But you have many more options. You can use this function;

function curl($URL,&$errmsg){
 $c = curl_init();
 curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
 curl_setopt($c, CURLOPT_URL, $URL);
 curl_setopt($c, CURLOPT_TIMEOUT, 10);
 $contents = curl_exec($c);
 if (curl_errno($c)){
 $errmsg = 'Failed loading content.';
 curl_close($c);
 return;
 }
 else{
 curl_close($c);
 return($contents);
 }
}

And your code then would be:

$url = 'https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC';
$data = curl($url, $errmsg);
$data = json_decode($data,true); 
$last = $data['result']['Last'];
answered Aug 8, 2014 at 21:35

1 Comment

Thanks, the array feature will come in very useful too! Thanks for answer :)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.