Author

Topic: 2 BTC bounty for a bit of code (Read 1361 times)

hero member
Activity: 700
Merit: 501
August 09, 2011, 04:49:58 PM
#13
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. Smiley 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!
full member
Activity: 168
Merit: 100
Firstbits: 175wn
August 08, 2011, 07:13:20 PM
#12
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.
hero member
Activity: 560
Merit: 500
legendary
Activity: 1176
Merit: 1280
May Bitcoin be touched by his Noodly Appendage
August 08, 2011, 06:25:27 PM
#10
Well ok, you should test the code later: http://www.downforeveryoneorjustme.com/mtgox.com
legendary
Activity: 1176
Merit: 1280
May Bitcoin be touched by his Noodly Appendage
August 08, 2011, 06:19:25 PM
#9
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
hero member
Activity: 560
Merit: 500
August 08, 2011, 06:15:44 PM
#8
Code:
$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
I modified it just a tiny bit, tested it on my server and it works.
Code:
$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."";
echo 
"\$day = ".$day."";
?>
For some reason wrapping json_decode() in parenthesis slowed it down a lot.
legendary
Activity: 1176
Merit: 1280
May Bitcoin be touched by his Noodly Appendage
August 08, 2011, 06:14:03 PM
#7
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
Nope, you should test with the curl function then
hero member
Activity: 700
Merit: 501
August 08, 2011, 06:08:53 PM
#6
Code:
$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
legendary
Activity: 1176
Merit: 1280
May Bitcoin be touched by his Noodly Appendage
August 08, 2011, 06:07:30 PM
#5
While dealing with php files, you always need to put the code between ''
hero member
Activity: 560
Merit: 500
August 08, 2011, 06:02:51 PM
#4
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. Wink

Code:
$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);
?>
hero member
Activity: 700
Merit: 501
August 08, 2011, 05:58:39 PM
#3
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. Wink
legendary
Activity: 1176
Merit: 1280
May Bitcoin be touched by his Noodly Appendage
August 08, 2011, 05:05:57 PM
#2
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);
hero member
Activity: 700
Merit: 501
August 08, 2011, 04:55:30 PM
#1
The following code gives my site it's current exchange rate.

Quote
$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!
Jump to: