Bitcoin Forum
April 19, 2024, 02:08:31 PM *
News: Latest Bitcoin Core release: 26.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1] 2 »  All
  Print  
Author Topic: [ANNOUNCE] Open source MtGOX PHP API Class  (Read 6405 times)
someguy123 (OP)
Sr. Member
****
Offline Offline

Activity: 336
Merit: 254


CEO of Privex Inc. (www.privex.io)


View Profile WWW
January 25, 2012, 08:09:39 PM
Last edit: July 26, 2012, 03:33:52 PM by someguy123
 #1

I've been using this class I built for a while, so I thought I'd release it to the bitcoin community as I haven't really saw many others like this:
https://github.com/Someguy123/MtGOX-PHP-API

Feel free to try it out, if you find any problems, just post in this thread here, or let me know on IRC: irc.freenode.org #bitcoin-otc #bitcoin - someguy123

Code:
Donations are accepted:
BTC: 1SoMGuYknDgyYypJPVVKE2teHBN4HDAh3
LTC: LSomguyTSwcw3hZKFts4P453sPfn4Y5Jzv
LQC: vbh3c3nCdWCnBq7gDNrJeXSt8WMg9Y64x8
NMC: N5omEguYDarGmHt2R23DFvkXYU49yPAAKN

If the class has helped you in any way, please donate to any of the addresses above ^ even a bitcent is fine :)

NOTE: This class is still using Version 0 of the MtGOX API, this means it could be deprecated at any time, but works right now as of 26/07/2012 despite the API changes

"With e-currency based on cryptographic proof, without the need to trust a third party middleman, money can be secure and transactions effortless." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1713535711
Hero Member
*
Offline Offline

Posts: 1713535711

View Profile Personal Message (Offline)

Ignore
1713535711
Reply with quote  #2

1713535711
Report to moderator
1713535711
Hero Member
*
Offline Offline

Posts: 1713535711

View Profile Personal Message (Offline)

Ignore
1713535711
Reply with quote  #2

1713535711
Report to moderator
someguy123 (OP)
Sr. Member
****
Offline Offline

Activity: 336
Merit: 254


CEO of Privex Inc. (www.privex.io)


View Profile WWW
January 28, 2012, 02:07:31 PM
 #2

I'm surprised nobody has replied yet...

Rdogg112
Newbie
*
Offline Offline

Activity: 9
Merit: 0


View Profile
January 28, 2012, 04:23:43 PM
 #3

Very nice work someguy123, very nice work indeed.
finway
Hero Member
*****
Offline Offline

Activity: 714
Merit: 500


View Profile
January 29, 2012, 03:35:20 AM
 #4

Great!

Yankee (BitInstant)
Legendary
*
Offline Offline

Activity: 1078
Merit: 1000


Charlie 'Van Bitcoin' Shrem


View Profile WWW
January 30, 2012, 03:38:13 PM
 #5

This is excellent, just ran thru the code and really nice work.

I will point this to people who ask us for help with the Gox API.

Bitcoin pioneer. An apostle of Satoshi Nakamoto. A crusader for a new, better, tech-driven society. A dreamer.

More about me: http://CharlieShrem.com
someguy123 (OP)
Sr. Member
****
Offline Offline

Activity: 336
Merit: 254


CEO of Privex Inc. (www.privex.io)


View Profile WWW
February 04, 2012, 10:46:04 PM
 #6

This is excellent, just ran thru the code and really nice work.

I will point this to people who ask us for help with the Gox API.
Thanks, doesn't seem to be getting much attention amazingly enough, even though I always see people asking about using the MtGOX API...
Also does anyone know if I would be able to add a link to my API on https://en.bitcoin.it/wiki/MtGox/API ?

Seal
Donator
Hero Member
*
Offline Offline

Activity: 848
Merit: 1078


View Profile WWW
February 09, 2012, 06:01:57 AM
 #7

This is excellent, just ran thru the code and really nice work.

I will point this to people who ask us for help with the Gox API.
Thanks, doesn't seem to be getting much attention amazingly enough, even though I always see people asking about using the MtGOX API...
Also does anyone know if I would be able to add a link to my API on https://en.bitcoin.it/wiki/MtGox/API ?

Nice work. Very clear, efficient and well structured code.

You might want to use this as a subclass and have a class wrapper with functions such as placebid($amt,$price), placeask($amt,$price) etc... That's what I have done as it saves you having to add the $path each time you place a call. It'll also make using each function simpler as each function will have a pre-defined set of inputs instead of having to look up the api page to find out what you should put in the $req = array() variable (which could lead to all sorts of mistakes caused by human error).

If I ever get round to tidying up my API code I'll share it one day.

Do you trade using your script?

DefiDive - Filter the noise
A clean crypto asset management terminal
Seal
Donator
Hero Member
*
Offline Offline

Activity: 848
Merit: 1078


View Profile WWW
February 09, 2012, 06:04:45 AM
 #8

... I'd be interested to hear from anyone who has written a PHP socket.io class for MtGox!

DefiDive - Filter the noise
A clean crypto asset management terminal
CA Coins
Donator
Sr. Member
*
Offline Offline

Activity: 305
Merit: 250


View Profile
February 13, 2012, 06:58:54 AM
 #9

Thanks for sharing your work!  Will definitely check it out.
Hunterbunter
Hero Member
*****
Offline Offline

Activity: 994
Merit: 1000


View Profile
February 13, 2012, 10:56:23 PM
 #10

This is excellent, just ran thru the code and really nice work.

I will point this to people who ask us for help with the Gox API.
Thanks, doesn't seem to be getting much attention amazingly enough, even though I always see people asking about using the MtGOX API...
Also does anyone know if I would be able to add a link to my API on https://en.bitcoin.it/wiki/MtGox/API ?

That's because you're advertising it like a programmer.

I'd bet that traffic to this thread would double if you put "MtGox Bot Source Code" or something in the title.
dooglus
Legendary
*
Offline Offline

Activity: 2940
Merit: 1330



View Profile
February 15, 2012, 12:28:06 PM
 #11

Here's the class I use to do the same thing:

https://github.com/dooglus/intersango/blob/aud/mtgox_api.php

It's more complete, offering functions like buy_btc, sell_btc, cancel_order, etc.

Use it if you like, but I can't offer any guarantees as to its correctness, but it works well enough for my purposes.

Just-Dice                 ██             
          ██████████         
      ██████████████████     
  ██████████████████████████ 
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
    ██████████████████████   
        ██████████████       
            ██████           
   Play or Invest                 ██             
          ██████████         
      ██████████████████     
  ██████████████████████████ 
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
██████████████████████████████
    ██████████████████████   
        ██████████████       
            ██████           
   1% House Edge
Seal
Donator
Hero Member
*
Offline Offline

Activity: 848
Merit: 1078


View Profile WWW
February 15, 2012, 05:45:23 PM
 #12

Here's the class I use to do the same thing:

https://github.com/dooglus/intersango/blob/aud/mtgox_api.php

It's more complete, offering functions like buy_btc, sell_btc, cancel_order, etc.

Use it if you like, but I can't offer any guarantees as to its correctness, but it works well enough for my purposes.

Thats pretty slick code, function wrappers Smiley This is what i've done but my code isnt tidy like yours Smiley

DefiDive - Filter the noise
A clean crypto asset management terminal
bat1
Newbie
*
Offline Offline

Activity: 32
Merit: 0


View Profile
June 07, 2012, 02:11:09 AM
 #13

dooglus, how may I execute the functions?

For example there is the function get_funds(), but how and where may I execute it?

Sorry for my dumb questions  Undecided
BCB
CTG
VIP
Legendary
*
Offline Offline

Activity: 1078
Merit: 1002


BCJ


View Profile
June 07, 2012, 03:37:07 AM
Last edit: June 07, 2012, 04:30:34 AM by BCB
 #14

<?PHP

include "mtgox_api.php";

$inst = new MtGox_API();

echo '<pre>' . print_r($inst->get_funds(),true) . '</pre>';

?>

Also you need to edit the first function in mtgox_api.php

function __construct($key, $secret)
    {
        $this->key = MTGOX_KEY;
        $this->secret = MTGOX_SECRET;
    }

so the vars match mtgox_config.php
bat1
Newbie
*
Offline Offline

Activity: 32
Merit: 0


View Profile
June 07, 2012, 08:10:11 AM
 #15

BCB, thank you very much... I have never been so close  Grin

But I please you to pardon my noobness : may I ask you what is incorrect in my code?

The following, modified as you told me :

Quote
<?php

define('MTGOX_KEY' , 'sdsfdfyssfd-sfdsdf-dsfdfds-fdsdfsfds-fddfsfsd');
define('MTGOX_SECRET', 'fdsgfgffftgrgretzt3355353tet5trr5353');

class MtGox_API
{

    function __construct($key, $secret)
    {
        $this->key = MTGOX_KEY;
        $this->secret = MTGOX_SECRET;
    }

    function query($path, array $req = array())
    {
        // generate a nonce as microtime, with as-string handling to avoid problems with 32bits systems
        $mt = explode(' ', microtime());
        $req['nonce'] = $mt[1].substr($mt[0], 2, 6);
 
        // generate the POST data string
        $post_data = http_build_query($req, '', 'name=sandy67&pass=lovebitcoins84');
 
        // generate the extra headers
        $headers = array('Rest-Key: ' . $this->key,
                         'Rest-Sign: '. base64_encode(hash_hmac('sha512', $post_data, base64_decode($this->secret), true)));
 
        // our curl handle (initialize if required)
        static $ch = null;
        if (is_null($ch)) {
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/4.0 (compatible; MtGox PHP client; '.php_uname('s').'; PHP/'.phpversion().')');
        }
        curl_setopt($ch, CURLOPT_URL, 'https://mtgox.com/api/' . $path);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($ch, CURLOPT_CAINFO, ABSPATH . "/cert/facacbc6.0");
 
        // run the query
        $res = curl_exec($ch);
        if ($res === false) throw new Exception('Could not get reply: ' . curl_error($ch));
        $dec = json_decode($res, true);
        if (!$dec) throw new Exception('Invalid data received, please make sure connection is working and requested API exists');
        return $dec;
    }

    function get_info()
    {
        return self::query('0/info.php');
    }
   
    ////////////////////////////////////////////////////////////////////////
    // * 0/getFunds.php
    //
    // Get your current balance
    //
    // https://mtgox.com/api/0/getFunds.php
    ////////////////////////////////////////////////////////////////////////

    function get_funds()
    {
        return self::query('0/getFunds.php');
    }

    ////////////////////////////////////////////////////////////////////////
    // * 0/buyBTC.php
    //
    // Place an order to Buy BTC
    //
    // POST data: amount=#&price=#
    //
    // returns a list of your open orders
    ////////////////////////////////////////////////////////////////////////

    function buy_btc($amount, $price)
    {
        return self::query('0/buyBTC.php',
                           array('amount' => $amount,
                                 'price' => $price));
    }

    ////////////////////////////////////////////////////////////////////////
    // * 0/sellBTC.php
    //
    // Place an order to Sell BTC
    //
    // POST data: &amount=#&price=#
    //
    // returns a list of your open orders
    ////////////////////////////////////////////////////////////////////////

    function sell_btc($amount, $price)
    {
        return self::query('0/sellBTC.php',
                           array('amount' => $amount,
                                 'price' => $price));
    }

    ////////////////////////////////////////////////////////////////////////
    // * 0/getOrders.php
    //
    // Fetch a list of your open Orders
    //
    // returned type: 1 for sell order or 2 for buy order
    //
    // status: 1 for active, 2 for not enough funds
    ////////////////////////////////////////////////////////////////////////

    function get_orders()
    {
        return self::query('0/getOrders.php');
    }

    ////////////////////////////////////////////////////////////////////////
    // * 0/cancelOrder.php
    //
    // Cancel an order
    //
    // POST data: oid=#&type=#
    //
    // oid: Order ID
    ////////////////////////////////////////////////////////////////////////

    function cancel_order($orderid)
    {
        return self::query('0/cancelOrder.php',
                           array('oid' => $orderid));
    }

    ////////////////////////////////////////////////////////////////////////
    // * 0/redeemCode.php
    //
    // Used to redeem a mtgox coupon code
    //
    // call with a post parameter "code" containing the code to redeem
    //
    // it will return an array with amount (float amount value of code), currency (3 letters, BTC or USD), reference (the transaction id), and status
    //
    ////////////////////////////////////////////////////////////////////////

    function deposit_coupon($code)
    {
        return self::query('0/redeemCode.php',
                           array('code' => $code));
    }

    ////////////////////////////////////////////////////////////////////////
    // * 0/withdraw.php
    //
    // withdraw / Send BTC
    //
    // POST data: group1=BTC&btca=bitcoin_address_to_send_to&amount=#
    //
    // pass btca parameter to withdraw to a btc adress
    // pass group1 for a coupon : BTC2CODE or USD2CODE
    // pass group1=DWUSD for a dwolla withdraw
    //
    // return code and status if successful
    ////////////////////////////////////////////////////////////////////////

    function withdraw_btc_coupon($amount)
    {
        return self::query('0/withdraw.php',
                           array('group1' => 'BTC2CODE',
                                 'amount' => $amount));
    }

    function withdraw_fiat_coupon($amount, $currency = CURRENCY)
    {
        return self::query('0/withdraw.php',
                           array('group1' => 'USD2CODE',
                                 'amount' => $amount,
                                 'Currency' => $currency)); // has to have capital 'C'; $currency = 'AUD' works
    }

    ////////////////////////////////////////////////////////////////////////
    // * 0/btcAddress.php
    //
    // get a bitcoin deposit adress for your account
    //
    // returns a bitcoin deposit address
    ////////////////////////////////////////////////////////////////////////

    function get_btc_address()
    {
        return self::query('0/btcAddress.php');
    }

    ////////////////////////////////////////////////////////////////////////
    // * 0/history_[CUR].csv
    //
    // Allows downloading your activity history for a given currency (BTC or USD for now).
    //
    // https://mtgox.com/api/0/history_BTC.php
    // https://mtgox.com/api/0/history_USD.php
    ////////////////////////////////////////////////////////////////////////

    function get_btc_history()
    {
        return self::query('0/history_BTC.csv'); /* doesn't work */
    }

    function get_usd_history()
    {
        return self::query('0/history_USD.csv'); /* doesn't work */
    }
}


$inst = new MtGox_API();

echo '<pre>' . print_r($inst->get_funds(),true) . '</pre>';

?>

... I got the following errors :

Quote
Warning: Missing argument 1 for MtGox_API::__construct(), called in myserver.com/json.php on line 211 and defined in myserver.com/json.php on line 9

Warning: Missing argument 2 for MtGox_API::__construct(), called in myserver.com/json.php on line 211 and defined in myserver.com/json.php on line 9

Fatal error: Uncaught exception 'Exception' with message 'Could not get reply: error setting certificate verify locations: CAfile: ABSPATH/cert/facacbc6.0 CApath: none ' in myserver.com/json.php:42 Stack trace: #0 myserver.com/json.php(63): MtGox_API->query('0/getFunds.php') #1 myserver.com/json.php(213): MtGox_API->get_funds() #2 {main} thrown in myserver.com/json.php on line 42

A thousand thanks  Tongue
bat1
Newbie
*
Offline Offline

Activity: 32
Merit: 0


View Profile
June 07, 2012, 08:50:51 AM
 #16

Okay I was able to fix the first set of errors by putting

Quote
<?php

class MtGox_API
{

    function __construct($key="sdsfdfyssfd-sfdsdf-dsfdfds-fdsdfsfds-fddfsfsd", $secret="fdsgfgffftgrgretzt3355353tet5trr5353")
    ...

bu I then still get

Quote
Fatal error: Uncaught exception 'Exception' with message 'Could not get reply: error setting certificate verify locations: CAfile: ABSPATH/cert/facacbc6.0 CApath: none ' in myserver.com/json.php:42 Stack trace: #0 myserver.com/json.php(187): MtGox_API->query('0/btcAddress.ph...') #1 myserver.com/json.php(213): MtGox_API->get_btc_address() #2 {main} thrown in myserver.com/json.php on line 42

What is wrong there?

Could some1 point me to the solution  Huh Help !
BCB
CTG
VIP
Legendary
*
Offline Offline

Activity: 1078
Merit: 1002


BCJ


View Profile
June 07, 2012, 01:10:46 PM
 #17

You need to install facacbc6.0 on your server

https://github.com/dooglus/intersango/tree/aud/cert


It is called in the curl option:

curl_setopt($ch, CURLOPT_CAINFO, ABSPATH . "/cert/facacbc6.0");
bat1
Newbie
*
Offline Offline

Activity: 32
Merit: 0


View Profile
June 07, 2012, 03:32:16 PM
 #18

...

Thank you soooo much!

 Grin  Grin  Grin

I am gonna try this little piece of code Smiley

I suppose that this file should be adapted with some specific informations, as I saw a kind of PGP encoding inside?

 Tongue
Fireball
Hero Member
*****
Offline Offline

Activity: 674
Merit: 500


View Profile WWW
June 11, 2012, 08:13:20 AM
 #19

So is there any PHP-based class for socket.io-based MtGox API?

Margin trading platform OrderBook.net (ICBIT): https://orderbook.net
Follow us in Twitter: https://twitter.com/orderbooknet
someguy123 (OP)
Sr. Member
****
Offline Offline

Activity: 336
Merit: 254


CEO of Privex Inc. (www.privex.io)


View Profile WWW
July 26, 2012, 03:34:34 PM
 #20

Just a note guys: Apparantly this still works, MtGox hasn't removed the version 0 API yet, so the wrapper should still function without error.

Pages: [1] 2 »  All
  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!