Hello,
I have meanwhile searched half the internet and this forum here. I just didn’t get any further with my problem.
GET requests all work without problems. However, if I want to send a POST request, the only response I get is {“message”:“invalid signature”}.
Can anyone tell me where the problem is?
My script:
> function signature($request_path='', $body='', $timestamp=false, $secret = '', $method='GET') {
> $body = is_array($body) ? json_encode($body) : $body;
> $timestamp = $timestamp ? $timestamp : time();
> $what = $timestamp.$method.$request_path.$body;
> return base64_encode(hash_hmac("sha256", $what, base64_decode($secret), true));
> }
> function make_request($url, $method, $headers, $body = ''){
> $ch = curl_init();
> curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
> curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
> curl_setopt($ch, CURLOPT_URL, $url);
> curl_setopt($ch, CURLOPT_VERBOSE, true);
> if ($method == "POST") {
> curl_setopt($ch, CURLOPT_POST, TRUE);
> curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
> }
> curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
> $result_json = curl_exec($ch);
> curl_close($ch);
> return $result_json;
> }
>
>
>
> try {
> $apiurl = "https://api.pro.coinbase.com";
> $secret = "My_Secret";
> $api_key = "Api_Key";
> $passphrase = "My_Passphrase";
> $method = "POST";
> $requestPath = "/order";
> $body = "";
>
> $url = $apiurl . $requestPath;
> $data["profile_id"] = $profile_id;
> $data["type"] = "market";
> $data["side"] = "buy";
> $data["stp"] = "dc";
> $data["stop"] = "loss";
> $data["time_in_force"] = "GTC";
> $data["post_only"] = "false";
> $data["product_id"] = $product_id;
> $data["funds"] = $fund;
> $body = json_encode($data);
>
> $user_agent = $_SERVER['HTTP_USER_AGENT'];
> $list = explode(" ", $user_agent);
> $user_agent = $list[0];
>
> $timestamp = (string) time();
>
> $string = $timestamp . $method . $requestPath;
> $sig = signature($requestPath, '', $timestamp, $secret);
>
> $headers = [
> "CB-ACCESS-KEY: ".$api_key,
> "CB-ACCESS-SIGN: ".$sig,
> "CB-ACCESS-TIMESTAMP: ".$timestamp,
> "CB-ACCESS-PASSPHRASE: ".$passphrase,
> "User-Agent:". $user_agent,
> "Accept: application/json",
> "Content-Type: application/json",
> ];
> $result_json = make_request($url, $method, $headers);
> var_dump($result_json);
> die;
> }
>
> catch(Exception $e){
> var_dump($e->getMessage(''));
> var_dump(json_decode($result_json));
> die;
> }