Bitcoin Forum

Bitcoin => Bitcoin Technical Support => Topic started by: Jeremy West spendbitcoins.com on August 07, 2011, 08:15:43 PM



Title: Last price from Mt Gox json
Post by: Jeremy West spendbitcoins.com on August 07, 2011, 08:15:43 PM
Can anyone help? I'm trying to grab the last price from Mt Gox using the following code:

<?php
    $url = 'https://mtgox.com/api/0/data/ticker.php';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_REFERER, $url);
    $json = curl_exec($ch);
    curl_close($ch);
    echo $json;//['ticker']['last'];
?>

and it's just hanging. Can anyone spot an error or if it's completely wrong just give me the right code? 1 BTC bounty if it works!

Thanks!


Title: Re: Last price from Mt Gox json
Post by: jackjack on August 07, 2011, 08:41:51 PM
Here's what I usually use
Code:
<?php
$a
=ouvrirpage("https://mtgox.com/code/data/ticker.php");
$res=json_decode($atrue);
echo 
$res['ticker']['last'];


function 
ouvrirpage($site){
$ch curl_init($site);
curl_setopt($chCURLOPT_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($chCURLOPT_RETURNTRANSFER1);
$res curl_exec($ch);
curl_close($ch);
return $res;
}
?>

Maybe MtGox needs an useragent header


Title: Re: Last price from Mt Gox json
Post by: wumpus on August 07, 2011, 08:42:47 PM
Yeah, you should use https://mtgox.com/code/data/ticker.php ... The URL that you use requires logging in.



Title: Re: Last price from Mt Gox json
Post by: markm on August 08, 2011, 01:50:33 AM
I read somewhere too that useragents that actually send a useragent field might be favoured over those  that do not?

(Dunno if bots would actually be requred to pretend to be (vanilla ?) browsers in order to be permitted to use the bot-enabling API though, has anyone tried being honest about which version of which bot is making the request?)

-MarkM-


Title: Re: Last price from Mt Gox json
Post by: antares on August 08, 2011, 11:41:29 PM
acutally a user agent seems not to be required. In my android App I don't use one either. And It would not really make sense, at least when using the MtGox API, as this one is intended for non-browser traffic


Title: Re: Last price from Mt Gox json
Post by: brandon@sourcewerks on August 10, 2011, 04:00:59 PM
Just wanted to get it out there,  BOTs are easily written to appear as browsers or even do it the dirty way and run in the browser.

The useragent is configurable in the HTTP headers that you send with the request.  Also, MT.Gox likes a unique user-agent.


Title: Re: Last price from Mt Gox json
Post by: alkhdaniel on August 10, 2011, 05:20:53 PM
Code:
<?php
$opts 
= array(
  
'http'=> array(
    
'method'=>   "GET",
    
'user_agent'=>    "MozillaXYZ/1.0"));
$context stream_context_create($opts);
$json file_get_contents('https://mtgox.com/code/data/ticker.php'false$context);
$json json_decode($json);
echo 
$json->{'ticker'}->{'last'};
?>



Title: Re: Last price from Mt Gox json
Post by: Jeremy West spendbitcoins.com on August 10, 2011, 09:06:06 PM
Sorry, forgot to post here that I already got this working with help from another thread. Thanks!