0

I am trying to POST "/orders" to GDAX using a simple PHP function. I am getting no response and no errors. Orders are not getting placed and there are no PHP errors/warnings.

Not sure where I am going wrong.

function gdaxPost ($path, $post_array){
    $url = 'https://api-public.sandbox.gdax.com/'.$path;

    // Sandbox API #1 - fake key
    $key = "03cc35bd4fb48ardad8097e0a45f";
    $secret = "ihGzWV+li8AweKcL+oMDUvBzlmq9fR7z6rKksg43VFcWA3zysg6TxM+gGhEn0wg==";
    $passphrase = "jqer9jxgfa6qcl";

    $time = time();
    $data = $time."POST"."/orders";
    $sign = base64_encode(hash_hmac("sha256", $data, base64_decode($secret), true));                
    $headers = array(                
        'CB-ACCESS-KEY: '.$key,
        'CB-ACCESS-SIGN: '.$sign,
        'CB-ACCESS-TIMESTAMP: '.$time,
        'CB-ACCESS-PASSPHRASE: '.$passphrase,
        'Content-Type: application/json'
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_array));
    curl_setopt($ch, CURLOPT_URL, $url);
    $res = curl_exec($ch);
    return $res;
}

$path = 'orders';
$post_array = array(
"price" => "600",
"size" => "1.01",
"side" => "buy",
"type" => 'limit',
"time_in_force" => "GTC",
"product_id" => "BTC-USD"
);

print_r(json_decode(gdaxPost ($path, $post_array)),true);

EDIT: I fixed the code!

This is the updated working code:

function gdaxPost ($path, $post_array){
    $url = 'https://api-public.sandbox.gdax.com/'.$path;

    // Sandbox API #1 - fake key
    $key = "03cc35bd4fb48ardad8097e0a45f";
    $secret = "ihGzWV+li8AweKcL+oMDUvBzlmq9fR7z6rKksg43VFcWA3zysg6TxM+gGhEn0wg==";
    $passphrase = "jqer9jxgfa6qcl";

    $time = time();
    $data = $time."POST"."/".$path.json_encode($post_array);
    $sign = base64_encode(hash_hmac("sha256", $data, base64_decode($secret), true));                
    $headers = array(                
        'CB-ACCESS-KEY: '.$key,
        'CB-ACCESS-SIGN: '.$sign,
        'CB-ACCESS-TIMESTAMP: '.$time,
        'CB-ACCESS-PASSPHRASE: '.$passphrase,
        'Content-Type: application/json'
    );

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2228.0 Safari/537.36');
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($post_array));
    curl_setopt($ch, CURLOPT_URL, $url);
    $res = curl_exec($ch);
    return $res;
}

$path = 'orders';
$post_array = array(
"price" => "600",
"size" => "1.01",
"side" => "buy",
"type" => 'limit',
"time_in_force" => "GTC",
"product_id" => "BTC-USD"
);

print_r(gdaxPost ($path, $post_array));
robinCTS
  • 5,746
  • 14
  • 30
  • 37
Jsp
  • 166
  • 1
  • 2
  • 13
  • Thanks robinCTS for adding the addendum. This code might help someone. – Jsp Feb 16 '18 at 05:57
  • No problemo. Though it might be better to add some explanation of what you fixed and why the original code didn't work. Also, not sure if you are aware, but it's a good idea to "ping" users you write comments to, so they get notified - it was only by chance I noticed your comment. You do this by using `@` like I did in this comment. (If you had prepended `@` to my name in your comment, that would have worked too.) See [How do comment @replies work?](//meta.stackexchange.com/q/43019) for further details. – robinCTS Feb 16 '18 at 07:10

0 Answers0