Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: daniel_zane on March 08, 2013, 08:20:56 PM



Title: [PHP] Simple Current Rate Snippet
Post by: daniel_zane on March 08, 2013, 08:20:56 PM
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.


Title: Re: [PHP] Simple Current Rate Snippet
Post by: hamdi on March 08, 2013, 08:34:54 PM
have a minute, i build you one.


Title: Re: [PHP] Simple Current Rate Snippet
Post by: hamdi on March 08, 2013, 08:41:38 PM

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".


Title: Re: [PHP] Simple Current Rate Snippet
Post by: hamdi on March 08, 2013, 08:42:53 PM
if you feel like thanking me, do it here:

1tHXmntuyAvFmJrZfrRNWkrnB9Rq3nnu4


thanks


Title: Re: [PHP] Simple Current Rate Snippet
Post by: daniel_zane on March 08, 2013, 08:52:47 PM
Shows up blank? Nothing shows up.


Title: Re: [PHP] Simple Current Rate Snippet
Post by: hamdi on March 08, 2013, 08:57:16 PM
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.


Title: Re: [PHP] Simple Current Rate Snippet
Post by: gweedo on March 08, 2013, 08:58:13 PM
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.


Title: Re: [PHP] Simple Current Rate Snippet
Post by: Bitsky on March 08, 2013, 10:06:37 PM
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.


Title: Re: [PHP] Simple Current Rate Snippet
Post by: hamdi on March 09, 2013, 02:04:56 PM
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.


Title: Re: [PHP] Simple Current Rate Snippet
Post by: matt.collier on March 30, 2013, 04:16:49 AM
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";