Pages:
Author

Topic: [ANNOUNCE] Open source MtGOX PHP API Class (Read 6406 times)

BCB
vip
Activity: 1078
Merit: 1002
BCJ
July 26, 2012, 11:42:15 AM
#21
someguy123,

Nice clean work.

I pulled some examples for my own code.

Thank you.
 
sr. member
Activity: 336
Merit: 254
CEO of Privex Inc. (www.privex.io)
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.
hero member
Activity: 674
Merit: 500
So is there any PHP-based class for socket.io-based MtGox API?
newbie
Activity: 32
Merit: 0
...

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
BCB
vip
Activity: 1078
Merit: 1002
BCJ
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");
newbie
Activity: 32
Merit: 0
Okay I was able to fix the first set of errors by putting

Quote

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 !
newbie
Activity: 32
Merit: 0
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

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 '
' . print_r($inst->get_funds(),true) . '
';

?>

... 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
BCB
vip
Activity: 1078
Merit: 1002
BCJ

include "mtgox_api.php";

$inst = new MtGox_API();

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

?>

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
newbie
Activity: 32
Merit: 0
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
donator
Activity: 848
Merit: 1078
February 15, 2012, 01: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
legendary
Activity: 2940
Merit: 1330
February 15, 2012, 08:28:06 AM
#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.
hero member
Activity: 994
Merit: 1000
February 13, 2012, 06: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.
donator
Activity: 305
Merit: 250
February 13, 2012, 02:58:54 AM
#9
Thanks for sharing your work!  Will definitely check it out.
donator
Activity: 848
Merit: 1078
February 09, 2012, 02:04:45 AM
#8
... I'd be interested to hear from anyone who has written a PHP socket.io class for MtGox!
donator
Activity: 848
Merit: 1078
February 09, 2012, 02: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?
sr. member
Activity: 336
Merit: 254
CEO of Privex Inc. (www.privex.io)
February 04, 2012, 06: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 ?
legendary
Activity: 1078
Merit: 1000
Charlie 'Van Bitcoin' Shrem
January 30, 2012, 11:38:13 AM
#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.
hero member
Activity: 714
Merit: 500
January 28, 2012, 11:35:20 PM
#4
Great!
newbie
Activity: 9
Merit: 0
January 28, 2012, 12:23:43 PM
#3
Very nice work someguy123, very nice work indeed.
sr. member
Activity: 336
Merit: 254
CEO of Privex Inc. (www.privex.io)
January 28, 2012, 10:07:31 AM
#2
I'm surprised nobody has replied yet...
Pages:
Jump to: