Author

Topic: [PHP] Simple Current Rate Snippet (Read 926 times)

member
Activity: 105
Merit: 10
March 29, 2013, 11:16:49 PM
#10
Here's a version with the v2 API

Code:
$tuCurl = curl_init();
curl_setopt($tuCurl, CURLOPT_URL, "https://data.mtgox.com/api/2/BTCUSD/money/ticker");
curl_setopt($tuCurl, CURLOPT_PORT , 443);
curl_setopt($tuCurl, CURLOPT_HEADER, 0);
curl_setopt($tuCurl, CURLOPT_RETURNTRANSFER, true);
$tuData = curl_exec($tuCurl);
$obj = json_decode($tuData);
$averagePrice = $obj->data->avg->value;
$lastPrice = $obj->data->last->value;
echo $averagePrice."\n";
echo $lastPrice."\n";
hero member
Activity: 826
Merit: 500
March 09, 2013, 09:04:56 AM
#9
that's another story.
but i sure won't code these 3 lines into it now, for a guy who can't even say thanks.
hero member
Activity: 576
Merit: 514
March 08, 2013, 05:06:37 PM
#8
Note that directly calling mtgox each time the page gets loaded makes it possible to get your website blocked by mtgox.
Either someone keeps F5 pressed or your site gets enough normal traffic.
Your server then would hammer mtgox with API requests what will get you banned.
It's better to add some local caching to protect yourself from that.
legendary
Activity: 1498
Merit: 1000
March 08, 2013, 03:58:13 PM
#7
Code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL,"https://mtgox.com/api/1/BTCUSD/ticker");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0;");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$gox=json_decode(curl_exec($ch), true);
curl_close ($ch);
$gox=$gox["return"]["last"]["display_short"];


echo $gox;

That should work for you better, since API v0 was phased out

Donation Address in sig.
hero member
Activity: 826
Merit: 500
March 08, 2013, 03:57:16 PM
#6
php scripts should start with


and also you need CURL installed on your server.

alternatively try this:

Code:
$gox=file_get_contents("https://mtgox.com/api/0/data/ticker.php");
$gox=explode(",\"buy\":",$gox);$gox=explode(",",$gox[1]);$gox=$gox[0];


echo $gox;


if both doesn't work, your server is not allowing outgoing connections.
newbie
Activity: 20
Merit: 0
March 08, 2013, 03:52:47 PM
#5
Shows up blank? Nothing shows up.
hero member
Activity: 826
Merit: 500
March 08, 2013, 03:42:53 PM
#4
if you feel like thanking me, do it here:

1tHXmntuyAvFmJrZfrRNWkrnB9Rq3nnu4


thanks
hero member
Activity: 826
Merit: 500
March 08, 2013, 03:41:38 PM
#3

Code:
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_URL,"https://mtgox.com/api/0/data/ticker.php");
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0;");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$gox=curl_exec($ch);curl_close ($ch);
$gox=explode(",\"buy\":",$gox);$gox=explode(",",$gox[1]);$gox=$gox[0];


echo $gox;


in the end you get the current buy rate on the variable "$gox".
hero member
Activity: 826
Merit: 500
March 08, 2013, 03:34:54 PM
#2
have a minute, i build you one.
newbie
Activity: 20
Merit: 0
March 08, 2013, 03:20:56 PM
#1
I've searched and found quite a few WP plugin and other random codes but none seem to work.  I found a javascript one but its too slow.

I'm looking for a simple PHP script to get the current BTC rate. Non-WP.
Jump to: