title: How to declare CURL body for CoinBase API call in php
body: <p>I want use php curl to interact with coinbase api. Simple API calls that does not require data to be passed are successful. What I want to do is <a href="https://developers.coinbase.com/api/v2#create-address" rel="nofollow noreferrer">create address</a>.<br>
  CLI curl works. The command line curl command sample is below:</p>
  
  <blockquote>
    <p>curl <a href="https://api.coinbase.com/v2/accounts/82de7fcd-db72-5085-8ceb-bee19303080b/addresses" rel="nofollow noreferrer">https://api.coinbase.com/v2/accounts/82de7fcd-db72-5085-8ceb-bee19303080b/addresses</a> \
      -X POST \
      -H 'Content-Type: application/json' \
      -H 'Authorization: Bearer abd90df5f27a7b170cd775abf89d632b350b7c1c9d53e08b340cd9832ce52c2c' \
      -d '{"name": "New receive address"}'
    }</p>
  </blockquote>
  
  <p>My php code excerpt</p>
  
  <pre><code>$apiurl = "https://api.coinbase.com";
  $secret = "coinbase api secret";
  $method = "POST";
  $requestPath = "/v2/accounts/actualAccountID/addresses";
  $body = "";
  $url = $apiurl.$requestPath;
  $data["name"] = "curl smj6 ary";
  $body=json_encode($data);
  
  $string = $timestamp.$method.$requestPath.$body;
  $sig = hash_hmac('sha256', $string, $secret);
  $ch = curl_init();
  // Disable SSL verification
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
  // Will return the response, if false it print the response
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
  // Set the url
  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);
  }
  
  $headers = [
      "CB-ACCESS-KEY: xxx",
      "CB-ACCESS-SIGN:$sig",
      "CB-ACCESS-TIMESTAMP: $timestamp",
      "CB-VERSION: 2018-03-21",
      "accept: application/json;charset=utf-8"
  ];
  
  curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
  
  // Execute
  $result_json = curl_exec($ch);
  </code></pre>
  
  <p>It returns</p>
  
  <blockquote>
    <p>{"errors":[{"id":"authentication_error","message":"invalid signature"}]}</p>
  </blockquote>
  
  <p>Since it works with listing user. I guess th error occurs the way iam passing post data to curl. </p>
  
  <p>Similar Questions that I found on SO but none solves my issue. Please help!</p>
  
  <ol>
  <li><a href="https://stackoverflow.com/questions/31655711/invalid-signature-coinbase/31682238#31682238">Invalid Signature Coinbase</a></li>
  <li><a href="https://stackoverflow.com/questions/28622045/coinbase-invalid-signature-php-buy-api-request?rq=1">CoinBase &quot;invalid signature&quot; PHP Buy API Request</a></li>
  <li><a href="https://stackoverflow.com/questions/42265223/how-to-declare-curl-body-for-coinbase-api-call">How to declare CURL body for CoinBase API call</a></li>
  <li><a href="https://stackoverflow.com/questions/46962816/api-key-authentication-for-coinbase">Api key authentication for coinbase</a></li>
  </ol>
  
  <p>UPDATE:</p>
  
  <pre><code>$apiurl = "https://api.coinbase.com/v2/";
  $requestPath = "accounts/$accountid/addresses";
  </code></pre>
  
  <p>returns same error</p>