Bitcoin Forum
April 18, 2024, 06:17:57 PM *
News: Latest Bitcoin Core release: 26.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: 2 BTC bounty for a bit of code  (Read 1326 times)
Jeremy West spendbitcoins.com (OP)
Hero Member
*****
Offline Offline

Activity: 700
Merit: 501



View Profile WWW
August 08, 2011, 09:55:30 PM
 #1

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!

1713464277
Hero Member
*
Offline Offline

Posts: 1713464277

View Profile Personal Message (Offline)

Ignore
1713464277
Reply with quote  #2

1713464277
Report to moderator
The Bitcoin software, network, and concept is called "Bitcoin" with a capitalized "B". Bitcoin currency units are called "bitcoins" with a lowercase "b" -- this is often abbreviated BTC.
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1713464277
Hero Member
*
Offline Offline

Posts: 1713464277

View Profile Personal Message (Offline)

Ignore
1713464277
Reply with quote  #2

1713464277
Report to moderator
1713464277
Hero Member
*
Offline Offline

Posts: 1713464277

View Profile Personal Message (Offline)

Ignore
1713464277
Reply with quote  #2

1713464277
Report to moderator
1713464277
Hero Member
*
Offline Offline

Posts: 1713464277

View Profile Personal Message (Offline)

Ignore
1713464277
Reply with quote  #2

1713464277
Report to moderator
jackjack
Legendary
*
Offline Offline

Activity: 1176
Merit: 1233


May Bitcoin be touched by his Noodly Appendage


View Profile
August 08, 2011, 10:05:57 PM
Last edit: August 08, 2011, 10:20:26 PM by jackjack
 #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);

Own address: 19QkqAza7BHFTuoz9N8UQkryP4E9jHo4N3 - Pywallet support: 1AQDfx22pKGgXnUZFL1e4UKos3QqvRzNh5 - Bitcointalk++ script support: 1Pxeccscj1ygseTdSV1qUqQCanp2B2NMM2
Pywallet: instructions. Encrypted wallet support, export/import keys/addresses, backup wallets, export/import CSV data from/into wallet, merge wallets, delete/import addresses and transactions, recover altcoins sent to bitcoin addresses, sign/verify messages and files with Bitcoin addresses, recover deleted wallets, etc.
Jeremy West spendbitcoins.com (OP)
Hero Member
*****
Offline Offline

Activity: 700
Merit: 501



View Profile WWW
August 08, 2011, 10: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

NothinG
Hero Member
*****
Offline Offline

Activity: 560
Merit: 500



View Profile
August 08, 2011, 11: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:
<?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);
?>

jackjack
Legendary
*
Offline Offline

Activity: 1176
Merit: 1233


May Bitcoin be touched by his Noodly Appendage


View Profile
August 08, 2011, 11:07:30 PM
 #5

While dealing with php files, you always need to put the code between '<?' and '?>'

Own address: 19QkqAza7BHFTuoz9N8UQkryP4E9jHo4N3 - Pywallet support: 1AQDfx22pKGgXnUZFL1e4UKos3QqvRzNh5 - Bitcointalk++ script support: 1Pxeccscj1ygseTdSV1qUqQCanp2B2NMM2
Pywallet: instructions. Encrypted wallet support, export/import keys/addresses, backup wallets, export/import CSV data from/into wallet, merge wallets, delete/import addresses and transactions, recover altcoins sent to bitcoin addresses, sign/verify messages and files with Bitcoin addresses, recover deleted wallets, etc.
Jeremy West spendbitcoins.com (OP)
Hero Member
*****
Offline Offline

Activity: 700
Merit: 501



View Profile WWW
August 08, 2011, 11:08:53 PM
 #6

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

jackjack
Legendary
*
Offline Offline

Activity: 1176
Merit: 1233


May Bitcoin be touched by his Noodly Appendage


View Profile
August 08, 2011, 11: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

Own address: 19QkqAza7BHFTuoz9N8UQkryP4E9jHo4N3 - Pywallet support: 1AQDfx22pKGgXnUZFL1e4UKos3QqvRzNh5 - Bitcointalk++ script support: 1Pxeccscj1ygseTdSV1qUqQCanp2B2NMM2
Pywallet: instructions. Encrypted wallet support, export/import keys/addresses, backup wallets, export/import CSV data from/into wallet, merge wallets, delete/import addresses and transactions, recover altcoins sent to bitcoin addresses, sign/verify messages and files with Bitcoin addresses, recover deleted wallets, etc.
NothinG
Hero Member
*****
Offline Offline

Activity: 560
Merit: 500



View Profile
August 08, 2011, 11:15:44 PM
 #8

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

jackjack
Legendary
*
Offline Offline

Activity: 1176
Merit: 1233


May Bitcoin be touched by his Noodly Appendage


View Profile
August 08, 2011, 11: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

Own address: 19QkqAza7BHFTuoz9N8UQkryP4E9jHo4N3 - Pywallet support: 1AQDfx22pKGgXnUZFL1e4UKos3QqvRzNh5 - Bitcointalk++ script support: 1Pxeccscj1ygseTdSV1qUqQCanp2B2NMM2
Pywallet: instructions. Encrypted wallet support, export/import keys/addresses, backup wallets, export/import CSV data from/into wallet, merge wallets, delete/import addresses and transactions, recover altcoins sent to bitcoin addresses, sign/verify messages and files with Bitcoin addresses, recover deleted wallets, etc.
jackjack
Legendary
*
Offline Offline

Activity: 1176
Merit: 1233


May Bitcoin be touched by his Noodly Appendage


View Profile
August 08, 2011, 11:25:27 PM
 #10

Well ok, you should test the code later: http://www.downforeveryoneorjustme.com/mtgox.com

Own address: 19QkqAza7BHFTuoz9N8UQkryP4E9jHo4N3 - Pywallet support: 1AQDfx22pKGgXnUZFL1e4UKos3QqvRzNh5 - Bitcointalk++ script support: 1Pxeccscj1ygseTdSV1qUqQCanp2B2NMM2
Pywallet: instructions. Encrypted wallet support, export/import keys/addresses, backup wallets, export/import CSV data from/into wallet, merge wallets, delete/import addresses and transactions, recover altcoins sent to bitcoin addresses, sign/verify messages and files with Bitcoin addresses, recover deleted wallets, etc.
NothinG
Hero Member
*****
Offline Offline

Activity: 560
Merit: 500



View Profile
August 08, 2011, 11:28:31 PM
 #11

Well ok, you should test the code later: http://www.downforeveryoneorjustme.com/mtgox.com
Looks up to me.

drawoc
Full Member
***
Offline Offline

Activity: 168
Merit: 100

Firstbits: 175wn


View Profile
August 09, 2011, 12:13:20 AM
Last edit: August 09, 2011, 12:46:17 AM by drawoc
 #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.

Donate: 175WNXmJ1WVhFgVGKUqEhYtAQGRYAvqPA
Jeremy West spendbitcoins.com (OP)
Hero Member
*****
Offline Offline

Activity: 700
Merit: 501



View Profile WWW
August 09, 2011, 09: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!

Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!