Bitcoin Forum

Other => Beginners & Help => Topic started by: Tosh on July 01, 2011, 11:28:22 AM



Title: MT Gox API- am I too stupid?
Post by: Tosh on July 01, 2011, 11:28:22 AM
Hi all!

I tried to scrape the tickerdata from Mt Gox, but it doesn't work.
I tested it several ways, but nothing works. When I open the API-adress in the browser, I get a download with getDepth.php or Ticker.php in which the data is included, but there is no way to scrape it.
I saw somebody writing here, that Mt.Gox needs to be accessed with an Useragent set, but even that changes nothing.


Can someone help me please, and tell me, what I'm doing wrong?

Here is a php snippet which works for all other sites out there but not for Mt Gox

Code:
function get_data($url)
{
  $useragent="Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0";
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}
$site="https://mtgox.com/code/data/getDepth.php";
$data=get_data($site);
echo $data;

Thanks


Title: Re: MT Gox API- am I too stupid?
Post by: Bitcoinreminder.com on July 01, 2011, 12:13:14 PM
If you know Ruby, then maybe this helps you.

http://forum.bitcoin.org/index.php?topic=24504.0


Title: Re: MT Gox API- am I too stupid?
Post by: Tosh on July 01, 2011, 01:13:14 PM
Thanks for your help, but I never touched Ruby before.

I've got a trading bot that worked fine before the Mt Gox hack and now I don't get the API things to work. It's a little bit annoying...


Title: Re: MT Gox API- am I too stupid?
Post by: zybron on July 01, 2011, 01:28:02 PM
What might help and is not apparent from their API page is that you have to POST your username and password, even for the 'public' API addresses. Or, at least, I was only able to successfully retrieve data after doing so. I'm using vb.net, however, so I'm not sure how to code the same methods in PHP.


Title: Re: MT Gox API- am I too stupid?
Post by: SlipperySlope on July 01, 2011, 02:28:17 PM
This works for me.  I am developing a Java client for the Mt Gox API and use the linux curl utility for testing.
Code:
curl -v -k --referer https://mtgox.com https://mtgox.com/code/data/getDepth.php

The -v option gives a verbose trace of the HTTP protocol, and is not otherwise needed. The -k option ignores the Mt Gox SSL certificate checking - and should not be used in a production script.


Title: Re: MT Gox API- am I too stupid?
Post by: Tosh on July 01, 2011, 04:38:33 PM
Thank you all for your help, now it works!

For those who want to do some stuff with the Mt Gox API via php, that works:

Puplic stuff
Code:
function get_data($url)
{
  $useragent="Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0";
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}
$site="https://mtgox.com/code/data/getDepth.php";
$data=get_data($site);
echo $data;

Account stuff:
Code:
function get_data($url)
{
  $useragent="Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0";
  $ch = curl_init();
  $timeout = 5;
  curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_POSTFIELDS, "name=yourname&pass=yourpass");
  curl_setopt($ch, CURLOPT_FAILONERROR, TRUE);
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
  $data = curl_exec($ch);
  curl_close($ch);
  return $data;
}
$site="https://mtgox.com/code/getFunds.php";
$data=get_data($site);
echo $data;

A referrer is not necessary, but you have to set the useragent.


Title: Re: MT Gox API- am I too stupid?
Post by: SlipperySlope on July 01, 2011, 08:14:39 PM
Thanks, I saved your samples and was not aware of the curl_setopt feature.


Title: Re: MT Gox API- am I too stupid?
Post by: angelo95 on July 04, 2011, 09:03:30 PM
Mate you can do all of this with a one-liner...

Code:
curl -s -k https://mtgox.com/code/data/getDepth.php