Search code examples
phpcurlcoinbase-api

Coinbase Pro post place order php


I am trying to place an order with the coinbase pro api using php. Just to tell you from the beginning is that the authentication works fine when i use the get method so that part is fine. For example i get my account my orders and everything related.

The problems comes up when i try to place an order using post (or other post methods i thing) as this is the first post method i am trying out.

So here is what i am trying to do:

Note that values are just for explanation:

creating the order:

$_eur = $_eur - ($_eur*$this->Eurfee); //Eur is i get the amount i have and subtract the fee of 0.15% for terms an purposes lets say i have 100
$_order = array(
            'product_id' => 'LTC-EUR',
            'side' => $side, //buy
            'price' => $this->lowPrice, //112.2
            'size' => $_eur / $this->lowPrice
          );

Now send the request;

$request = '/orders';
$this->__sendRequest($request, $_order, 'POST', true);

Here is the send request function:

private function __sendRequest($request, $body='', $method='GET', $auth=false)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $this->endpoint.$request);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    if (strcasecmp($method, 'POST') === 0)
    {
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
    }
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);

    if ($auth) {
        $headers = array();
        $headers[] = "Content-Type: application/json";
        $headers[] = "CB-ACCESS-KEY: " . $this->key;
        $headers[] = "CB-ACCESS-SIGN: " . $this->__signature($request, $body, $method);
        $headers[] = "CB-ACCESS-TIMESTAMP: " . $this->timestamp;
        $headers[] = "CB-ACCESS-PASSPHRASE: " . $this->passphrase;
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

    }

    $output = curl_exec($ch);
    $output = json_decode($output);

    if(curl_getinfo($ch, CURLINFO_HTTP_CODE) !== 200) {
        return $output;
    }

    curl_close($ch);
    return $output;
}

And here is the signature if maybe there is the problem with post requests:

private function __signature($request_path='', $body='', $method='GET') {
    $body = is_array($body) ? json_encode($body) : $body;

    $what = $this->timestamp.$method.$request_path.$body;

    return base64_encode(hash_hmac("sha256", $what, base64_decode($this->secret), true));
}

When i send the request the output is null and of course the order is not placed. Can anybody please help me out. I think its the curl that i am doing something wrong there maybe with post methods?

And another question for a one maybe that understands coinbase fees. Should i calculate the size with the fee included and coibase will take the fee when completed or i should just calculate the size and let coinbase calculate the fee by it self?

Regards,


Solution

  • The function works properly! I found out that the size amount i was sending was not satisfying the minimum after i found out the message i was getting from coinbase!