Tag Archives: PHP. Mount Gox

Source code: requestGox.php


This script handles the communication with Mout Gox. It is taken from here.


function mtgox_query($path, $params) {
    // API settings
    $key = getenv("surgekey");
    $secret = getenv("surgesecret");

    $mt = explode(' ', microtime());
    $req = array();
    $req['nonce'] = $mt[1].substr($mt[0], 2, 6);

    // generate the POST data string
    $post_data = http_build_query($req, '', '&');
    if (getenv("surgeargs")) $post_data = $post_data.'&'.getenv("surgeargs");

    // generate the extra headers
    $headers = array(
        'Rest-Key: '.$key,
        'Rest-Sign: '.base64_encode(hash_hmac('sha512', $post_data, base64_decode($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_SSL_VERIFYHOST, 0);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 

    //set debug proxy
    //curl_setopt($ch, CURLOPT_PROXY, ''); 

    // run the query
    $res = curl_exec($ch);

    if ($res == false) return "error while executing request to MtGox:\t\n".curl_error($ch);
    return $res;

//argv[1] should contain uri of mtgox api path like '0/info.php'
echo mtgox_query($argv[1], $argv[2]);