Bitcoin Forum

Bitcoin => Project Development => Topic started by: Jeremy West spendbitcoins.com on August 08, 2011, 09:55:30 PM



Title: 2 BTC bounty for a bit of code
Post by: Jeremy West spendbitcoins.com on August 08, 2011, 09:55:30 PM
The following code gives my site it's current exchange rate.

Quote
<?php
$string = file_get_contents("http://bitcoincharts.com/t/weighted_prices.json");
$json_a=json_decode($string,true);
echo $json_a['USD']['24h'];
?>

2 BTC bounty for the first person who rewrites it to echo the lower of the last price on Mt Gox and the 24-hour weighted average on Mt Gox.

Thanks!


Title: Re: 2 BTC bounty for a bit of code
Post by: jackjack on August 08, 2011, 10:05:57 PM
Code:
$a=ouvrirpage("https://mtgox.com/code/data/ticker.php");
$res=(json_decode($a, true));
$low = $res['ticker']['low'];

$a=ouvrirpage("http://bitcoincharts.com/t/weighted_prices.json");
$res=(json_decode($a, true));
$day = $res['USD']['24h'];

echo min($low, $day);

function ouvrirpage($site){
$ch = curl_init($site);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}

or if you don't like curl:

Code:
$a=file_get_contents("https://mtgox.com/code/data/ticker.php");
$res=(json_decode($a, true));
$low = $res['ticker']['low'];

$a=file_get_contents("http://bitcoincharts.com/t/weighted_prices.json");
$res=(json_decode($a, true));
$day = $res['USD']['24h'];

echo min($low, $day);


Title: Re: 2 BTC bounty for a bit of code
Post by: Jeremy West spendbitcoins.com on August 08, 2011, 10:58:39 PM
Hmmm.... My code returns this:

http://spendbitcoins.com/last-24hr.php

While your code returns this:

http://spendbitcoins.com/jefftest/last-24hr.php

How do I get your code to just return a figure like mine does? Sorry for my ignorance. I'm not a programmer at all. ;)


Title: Re: 2 BTC bounty for a bit of code
Post by: NothinG on August 08, 2011, 11:02:51 PM
Hmmm.... My code returns this:

http://spendbitcoins.com/last-24hr.php

While your code returns this:

http://spendbitcoins.com/jefftest/last-24hr.php

How do I get your code to just return a figure like mine does? Sorry for my ignorance. I'm not a programmer at all. ;)

Code:
<?php
$a
=file_get_contents("https://mtgox.com/code/data/ticker.php");
$res=(json_decode($atrue));
$low $res['ticker']['low'];

$a=file_get_contents("http://bitcoincharts.com/t/weighted_prices.json");
$res=(json_decode($atrue));
$day $res['USD']['24h'];

echo 
min($low$day);
?>


Title: Re: 2 BTC bounty for a bit of code
Post by: jackjack on August 08, 2011, 11:07:30 PM
While dealing with php files, you always need to put the code between '<?' and '?>'


Title: Re: 2 BTC bounty for a bit of code
Post by: Jeremy West spendbitcoins.com on August 08, 2011, 11:08:53 PM
Code:
<?php
$a
=file_get_contents("https://mtgox.com/code/data/ticker.php");
$res=(json_decode($atrue));
$low $res['ticker']['low'];

$a=file_get_contents("http://bitcoincharts.com/t/weighted_prices.json");
$res=(json_decode($atrue));
$day $res['USD']['24h'];

echo 
min($low$day);
?>

Hmmm.... Does that code return a value to you? Maybe it's a problem with my server, but on my link it just hangs. Does it return a value for anyone else who clicks on it? http://spendbitcoins.com/jefftest/last-24hr.php (http://spendbitcoins.com/jefftest/last-24hr.php)


Title: Re: 2 BTC bounty for a bit of code
Post by: jackjack on August 08, 2011, 11:14:03 PM
Hmmm.... Does that code return a value to you? Maybe it's a problem with my server, but on my link it just hangs. Does it return a value for anyone else who clicks on it? http://spendbitcoins.com/jefftest/last-24hr.php (http://spendbitcoins.com/jefftest/last-24hr.php)
Nope, you should test with the curl function then


Title: Re: 2 BTC bounty for a bit of code
Post by: NothinG on August 08, 2011, 11:15:44 PM
Code:
<?php
$a
=file_get_contents("https://mtgox.com/code/data/ticker.php");
$res=(json_decode($atrue));
$low $res['ticker']['low'];

$a=file_get_contents("http://bitcoincharts.com/t/weighted_prices.json");
$res=(json_decode($atrue));
$day $res['USD']['24h'];

echo 
min($low$day);
?>

Hmmm.... Does that code return a value to you? Maybe it's a problem with my server, but on my link it just hangs. Does it return a value for anyone else who clicks on it? http://spendbitcoins.com/jefftest/last-24hr.php (http://spendbitcoins.com/jefftest/last-24hr.php)
I modified it just a tiny bit, tested it on my server and it works.
Code:
<?php
$a
=file_get_contents("https://mtgox.com/code/data/ticker.php");
$res=json_decode($atrue);
$low $res['ticker']['low'];

$a=file_get_contents("http://bitcoincharts.com/t/weighted_prices.json");
$res=json_decode($atrue);
$day $res['USD']['24h'];

echo 
"\$low = ".$low."<br />";
echo 
"\$day = ".$day."<br />";
?>
For some reason wrapping json_decode() in parenthesis slowed it down a lot.


Title: Re: 2 BTC bounty for a bit of code
Post by: jackjack on August 08, 2011, 11:19:25 PM
Code:
<?
$a=file_get_contents("http://mtgox.com/code/data/ticker.php");
$res=(json_decode($a, true));
$low = $res['ticker']['low'];

$a=file_get_contents("http://bitcoincharts.com/t/weighted_prices.json");
$res=(json_decode($a, true));
$day = $res['USD']['24h'];

echo min($low, $day);
?>
file_get_contents might not work with https

http://mtgox.com/code/data/ticker.php is *really* long to respond, too


Title: Re: 2 BTC bounty for a bit of code
Post by: jackjack on August 08, 2011, 11:25:27 PM
Well ok, you should test the code later: http://www.downforeveryoneorjustme.com/mtgox.com


Title: Re: 2 BTC bounty for a bit of code
Post by: NothinG on August 08, 2011, 11:28:31 PM
Well ok, you should test the code later: http://www.downforeveryoneorjustme.com/mtgox.com
Looks up to me.


Title: Re: 2 BTC bounty for a bit of code
Post by: drawoc on August 09, 2011, 12:13:20 AM
file_get_contents might not work with https

http://mtgox.com/code/data/ticker.php is *really* long to respond, too

http://mtgox.com/code/data/ticker.php looks like it redirects to https to me.

Anyway I tried this and I'm getting a similar problem to Jeremy, so I'll try to see if I can fix it.

On my machine, specifying http://mtgox.com/code/data/ticker.php results in a 500 on the page, and this in the log:
Code:
[Mon Aug 08 19:39:18 2011] [error] [client 127.0.0.1] PHP Warning:  file_get_contents(http://mtgox.com/code/data/ticker.php): failed to open stream: HTTP request failed!  in /var/www/hi.php on line 2

Specifying https://mtgox.com/code/data/ticker.php results in the page hanging forever, and nothing at all in the logs. (not even an acknowledgement of the request, like Apache normally does).

Comment the line out that grabs the mt.gox data and the page loads fine.

Strangely, stream_get_wrappers() does show that https is available on my machine.

Yes, Mt.Gox is up here, just very slow.

I don't have the php curl wrapper installed, maybe I'll install it and try the other example.

Edit: Yep, the curl one works perfectly fine.


Title: Re: 2 BTC bounty for a bit of code
Post by: Jeremy West spendbitcoins.com on August 09, 2011, 09:49:58 PM
Code:
$a=ouvrirpage("https://mtgox.com/code/data/ticker.php");
$res=(json_decode($a, true));
$low = $res['ticker']['low'];

$a=ouvrirpage("http://bitcoincharts.com/t/weighted_prices.json");
$res=(json_decode($a, true));
$day = $res['USD']['24h'];

echo min($low, $day);

function ouvrirpage($site){
$ch = curl_init($site);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($ch);
curl_close($ch);
return $res;
}

or if you don't like curl:

Code:
$a=file_get_contents("https://mtgox.com/code/data/ticker.php");
$res=(json_decode($a, true));
$low = $res['ticker']['low'];

$a=file_get_contents("http://bitcoincharts.com/t/weighted_prices.json");
$res=(json_decode($a, true));
$day = $res['USD']['24h'];

echo min($low, $day);

Cool. Got it working with the curl one (never rechecked that one with the <? /?> Last coding I did was GW-Basic in the early 90s. :) And that was just copying and changing code, have never really coded anything from scratch.

Anyway, bounty on its way to your signature bitcoin address. Thanks!