4

I was wondering if there was any way to make a Parser in PHP in which gets the values from this site https://btc-e.com/api/2/btc_usd/ticker and sets them as variables in php code?

I have looked at php parsers a bit and the only thing I found was parsers that echo all the information on a website.

ProGM
  • 6,949
  • 4
  • 33
  • 52
JVarhol
  • 606
  • 3
  • 7
  • 18

4 Answers4

12

Since that URL returns a JSON response:

<?php

$content=file_get_contents("https://btc-e.com/api/2/btc_usd/ticker");
$data=json_decode($content);
//do whatever with $data now
?>
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
  • I have tried this but for some reason I get an error And I get this error Fatal error: Cannot use object of type stdClass as array in C:\wamp3\www\DROPBOX\Dropbox\FTP\Test2.php on line 5 – JVarhol Jan 09 '14 at 14:09
  • 1
    by default `json_decode` will give you an object back, if you have to use it as an array then you can use `$data=json_decode($content,true);` – Hanky Panky Jan 10 '14 at 06:16
4

You can use file_get_contents to get the data from the URL and json_decode to parse the result, because the site you have linked is returning a JSON array, that can be parsed by php natively.

Example:

$bitcoin = json_decode(file_get_contents("https://btc-e.com/api/2/btc_usd/ticker"), true);

In the $bitcoin variable you will have an associative array with the values of the JSON string.

Result:

array(1) {
  ["ticker"]=>
  array(10) {
    ["high"]=>
    float(844.90002)
    ["low"]=>
    int(780)
    ["avg"]=>
    float(812.45001)
    ["vol"]=>
    float(13197445.40653)
    ["vol_cur"]=>
    float(16187.2271)
    ["last"]=>
    float(817.601)
    ["buy"]=>
    float(817.951)
    ["sell"]=>
    float(817.94)
    ["updated"]=>
    int(1389273192)
    ["server_time"]=>
    int(1389273194)
  }
}
ProGM
  • 6,949
  • 4
  • 33
  • 52
2

The data on that page is called Json (JavaScript Object Notation) (its not output as json mime type, but it is formated like json).
If you know that the data will be json, you can aquire it as a string from the page (using for example the file_get_contents function) and decode it to an associative array with the json_decode function:

<?php
$dataFromPage = file_get_contents($url);
$data = json_decode($dataFromPage, true);
// Then just access the data from the assoc array like:
echo $data['ticker']['high'];
// or store it as you wish:
$tickerHigh = $data['ticker']['high'];
Jite
  • 5,761
  • 2
  • 23
  • 37
  • When I use this code I get this error Fatal error: Cannot use object of type stdClass as array in C:\wamp3\www\DROPBOX\Dropbox\FTP\Test2.php on line 5 – JVarhol Jan 09 '14 at 13:56
  • @JVarhol are you sure that you have set the second parameter to true to the `json_decode($dataFromPage, true)` function? – ProGM Jan 09 '14 at 14:15
  • this is my code – JVarhol Jan 09 '14 at 14:20
  • @ProGM I have set it to true, I have included my code above, Is there any other possibility of why it is giving me an error? – JVarhol Jan 09 '14 at 14:55
  • @JVarhol check http://stackoverflow.com/questions/6815520/cannot-use-object-of-type-stdclass-as-array . Try to cast to array or use print_r($data); after json_decode and check the value of that data – ProGM Jan 09 '14 at 15:01
  • @JVarhol try to access the data like this: `echo $data->ticker->high; $tickerHigh = $data->ticker->high;` – ProGM Jan 09 '14 at 15:26
  • @ProGM Seems like that worked. So now I can use the variable $tickerHigh. Is there anyway to set the script to recheck the prices every, say, 10 minutes. – JVarhol Jan 09 '14 at 15:30
  • @JVarhol You can use some [http://davidwalsh.name/php-cache-function](kind of caching mechanism). – ProGM Jan 09 '14 at 15:33
  • If you are using the script to fetch data and store it, a cronjob could be a good solution. (Sorry for not responding on earlier comments, just got back and seems you got it sorted, altho it should be an assoc array and not an object). – Jite Jan 09 '14 at 15:50
  • @Jite I will look into a cronjob, but currently it seems that it is working. Exactly what does a cronjob do? – JVarhol Jan 09 '14 at 15:54
  • Please mark an answer as correct (would recommend Hanky 웃 Panky answer, cause that one was first) if you got what you needed. :) – Jite Jan 09 '14 at 15:56
  • I always mark an answer as correct, but thanks for reminding me – JVarhol Jan 09 '14 at 15:58
2
<?
function GetJsonFeed($json_url)
{
$feed = file_get_contents($json_url);
return json_decode($feed, true);
}
$LTC_USD = GetJsonFeed("https://btc-e.com/api/2/ltc_usd/ticker");
$LTC_USD_HIGH = $LTC_USD["ticker"]["last"];

$BTC_USD = GetJsonFeed("https://btc-e.com/api/2/btc_usd/ticker");
$BTC_USD_HIGH = $BTC_USD["ticker"]["last"];
?>
Khan Shahrukh
  • 6,109
  • 4
  • 33
  • 43