Jump to content

PHP, Curl, and JSON


Go to solution Solved by jazzman1,

Recommended Posts

Guest Xanza

Hey all. I've been playing with curl via the PHP command line, and I came up with this little curl based API interface to display the current BTC price from mtgox.com via the API. I've tried dumping the returned JSON'ized curl information into a string, and I've tried to query it, but so far no luck. I've used this script before for the Github API and it worked just fine. I've come to the conclusion it's because of the way that the JSON arrays are structured. Any help would be appreciated:

 

<?php

$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($c, CURLOPT_URL, 'http://data.mtgox.com/api/2/BTCUSD/money/ticker');

$content = curl_exec($c);
curl_close($c);

$api = json_decode($content);
return $api->avg;

?>

The above code, when run with 

php -f btc.php

returns an error:

PHP Notice:  Undefined property: stdClass::$avg in btc.php on line 12

Which makes me believe that my return:

return $api->avg;

is incorrect. So, I rechecked the API and the return path seems to be:

Object->data->avg;

So, I restructured my code to test to see if something like this would work:

return $api->data->avg;
OR
return $api->data['avg'];

And I receive a non-object error:

PHP Notice:  Trying to get property of non-object in btc.php on line 13

I was able to accomplish something very similar with Ruby and elinks:

#!/bin/bash

elinks -dump https://www.bitstamp.net/api/ticker/ > /tmp/btc.json
cat /tmp/btc.json | ruby -e "require 'rubygems'; require 'json'; puts JSON[sTDIN.read]['ask'];"

however, this is a script that I'd like to take with me (just so I can check on the BTC price on the go without a web browser), and not all of my terminals have Ruby; also elinks -dump is pretty slow compared to curl and PHP.

 

Any ideas how I can access and return the current price within the JSON array?

Link to comment
https://forums.phpfreaks.com/topic/276711-php-curl-and-json/
Share on other sites

 

<?php
  $c = curl_init();
          curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
          curl_setopt($c, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
          curl_setopt($c, CURLOPT_URL, 'http://data.mtgox.com/api/2/BTCUSD/money/ticker');
 
  $content = curl_exec($c);
  curl_close($c);
 
  $api = json_decode($content, TRUE);
 
 
  return print_r($api['data']['avg'], TRUE);
 
?>
Edited by PaulRyan
Link to comment
https://forums.phpfreaks.com/topic/276711-php-curl-and-json/#findComment-1423667
Share on other sites

  • Solution

Paul, that's wrong b/s you have individual json objects names.

 

<?php

$data = '{"result":"success","data":{"high":{"value":"207.00000","value_int":"20700000","display":"$207.00","display_short":"$207.00","currency":"USD"},"low":{"value":"172.13000","value_int":"17213000","display":"$172.13","display_short":"$172.13","currency":"USD"},"avg":{"value":"190.06255","value_int":"19006255","display":"$190.06","display_short":"$190.06","currency":"USD"},"vwap":{"value":"189.49989","value_int":"18949989","display":"$189.50","display_short":"$189.50","currency":"USD"},"vol":{"value":"105772.39701647","value_int":"10577239701647","display":"105,772.40\u00a0BTC","display_short":"105,772.40\u00a0BTC","currency":"BTC"},"last_local":{"value":"205.97101","value_int":"20597101","display":"$205.97","display_short":"$205.97","currency":"USD"},"last_orig":{"value":"159.39420","value_int":"15939420","display":"159.39\u00a0\u20ac","display_short":"159.39\u00a0\u20ac","currency":"EUR"},"last_all":{"value":"207.57907","value_int":"20757907","display":"$207.58","display_short":"$207.58","currency":"USD"},"last":{"value":"205.97101","value_int":"20597101","display":"$205.97","display_short":"$205.97","currency":"USD"},"buy":{"value":"205.55333","value_int":"20555333","display":"$205.55","display_short":"$205.55","currency":"USD"},"sell":{"value":"205.97101","value_int":"20597101","display":"$205.97","display_short":"$205.97","currency":"USD"},"item":"BTC","now":"1365510862857980"}}';

$obj = json_decode($data);

echo '<pre>'.print_r($obj->{'data'}->{'avg'}, true).'</pre>';

Result:

 

 

 

stdClass Object(    [value] => 190.06255    [value_int] => 19006255    [display] => $190.06    [display_short] => $190.06    [currency] => USD)

 

Link to comment
https://forums.phpfreaks.com/topic/276711-php-curl-and-json/#findComment-1423684
Share on other sites

I am very surised how you got a correct result with this notation because the json_decode function turns a json object into a php object.

 

So, to get a corect result you need to use a proper php object syntax.

 

Can you show me what did you test? 

Link to comment
https://forums.phpfreaks.com/topic/276711-php-curl-and-json/#findComment-1423813
Share on other sites

You should read the documentation again. json_decode takes a second argument of a boolean value (TRUE)

If the second argument if present, it returns an array instead of an object.

 

Read up before you comment. Enjoy the new tip :)

Edited by PaulRyan
Link to comment
https://forums.phpfreaks.com/topic/276711-php-curl-and-json/#findComment-1423829
Share on other sites

Guest Xanza

In the interest of saving face, I tried all options presented;

 

PaulRyan's Method:

root@ks4003010 .bin]$ clear
root@ks4003010 .bin]$ php -f test.php
root@ks4003010 .bin]$ 

jazzman1's Method:

root@ks4003010 .bin]$ php -f btc.php
$211.55
root@ks4003010 .bin]$ 

And this is the complete finished product that returns the result I was looking for:

<?php
 
$c = curl_init();
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($c, CURLOPT_HTTPHEADER, array('Accept: application/json', 'Content-Type: application/json'));
curl_setopt($c, CURLOPT_URL, 'http://data.mtgox.com/api/2/BTCUSD/money/ticker');

$data = curl_exec($c);
curl_close($c);
 
$obj = json_decode($data);
 
echo print_r($obj->{'data'}->{'avg'}->{'display_short'}."\n", true);

?>

I thank you both for your help! I learned quite a bit with this little test.

Link to comment
https://forums.phpfreaks.com/topic/276711-php-curl-and-json/#findComment-1423830
Share on other sites

You should read the documentation again. json_decode takes a second argument of a boolean value (TRUE)

If the second argument if present, it returns an array instead of an object.

 

Read up before you comment. Enjoy the new tip :)

Hm.... intersting.

 

I never used json_decode converting json objects into associative php arrays.

 

Anyway, I've tested your suggestion minutes ago and.........it's true.

 

Here is it:

<?php     
    $data = '{"result":"success","data":{"high":{"value":"207.00000","value_int":"20700000","display":"$207.00","display_short":"$207.00","currency":"USD"},"low":{"value":"172.13000","value_int":"17213000","display":"$172.13","display_short":"$172.13","currency":"USD"},"avg":{"value":"190.06255","value_int":"19006255","display":"$190.06","display_short":"$190.06","currency":"USD"},"vwap":{"value":"189.49989","value_int":"18949989","display":"$189.50","display_short":"$189.50","currency":"USD"},"vol":{"value":"105772.39701647","value_int":"10577239701647","display":"105,772.40\u00a0BTC","display_short":"105,772.40\u00a0BTC","currency":"BTC"},"last_local":{"value":"205.97101","value_int":"20597101","display":"$205.97","display_short":"$205.97","currency":"USD"},"last_orig":{"value":"159.39420","value_int":"15939420","display":"159.39\u00a0\u20ac","display_short":"159.39\u00a0\u20ac","currency":"EUR"},"last_all":{"value":"207.57907","value_int":"20757907","display":"$207.58","display_short":"$207.58","currency":"USD"},"last":{"value":"205.97101","value_int":"20597101","display":"$205.97","display_short":"$205.97","currency":"USD"},"buy":{"value":"205.55333","value_int":"20555333","display":"$205.55","display_short":"$205.55","currency":"USD"},"sell":{"value":"205.97101","value_int":"20597101","display":"$205.97","display_short":"$205.97","currency":"USD"},"item":"BTC","now":"1365510862857980"}}';
     
    $obj = json_decode($data, true);
     
    echo '<pre>'.print_r($obj['data']['avg'], true).'</pre>';

Results:

 

Array(    [value] => 190.06255    [value_int] => 19006255    [display] => $190.06    [display_short] => $190.06    [currency] => USD)

 

 

Very good forum.

 

I will take a note in the future :)

Link to comment
https://forums.phpfreaks.com/topic/276711-php-curl-and-json/#findComment-1423862
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.