Author

Topic: Empty data from MtGox after sending requests. (Read 401 times)

newbie
Activity: 33
Merit: 0
August 13, 2013, 02:16:07 PM
#1
Hi,

I use a PHP script to send requests to MtGox, but lately get a lot of empty data back on requests and have to try hundreds or thousands of times before I get actual data back after sending a request. Did anyone experience a similar problem, any idea how to resolve?

The code is here, key and secret removed:

     
function mtgox_query($path, array $req = array()) {
    // API settings
    $key = ''; 
    $secret = '';
    // 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, '', '&');
    $prefix = '';
    if (substr($path, 0, 2) == '2/') {
        $prefix = substr($path, 2)."\0";
    }
    // generate the extra headers
    $headers = array(
        'Rest-Key: '.$key,
        'Rest-Sign: '.base64_encode(hash_hmac('sha512', $prefix.$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://data.mtgox.com/api/'.$path);
    curl_setopt($ch, CURLOPT_URL, 'https://23.67.8.44/api/'.$path);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    // 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;
    print json_encode($dec);
}
?>

Thanks, Dirk.
Jump to: