私自身の目的のために、完全に機能する Coinbase API システムを PHP で作成しました。ただし、システムの一部が正常に機能しないことがあります: 成行注文です。
以下に示すコードは、オブジェクト指向システムで発生する手順を再現したものです。このフォーラムでは、継承のレイヤーを掘り下げるよりも、線形コードの方がトラブルシューティングが簡単だと思いました。
Coinbase API は、コードのベースにあるコメント テキストに示されているように、エラー メッセージを返します。ここで説明されている「市場」注文 API には「価格」パラメーターは必要ありません: Coinbase Exchange API ドキュメント。要求されたフィールドを追加してエラー メッセージに応答すると、注文は最終的に成功しますが、タイプで示されるように、注文は「成行」注文ではなく「指値」注文として処理されます。
私が犯している間違いを見つけることができますか?
<?php
$settings = \parse_ini_file("API.ini", true);
$apiSecret = $settings['trader_sandbox']['API Secret'];
$apiKey = $settings['trader_sandbox']['API Key'];
$apiPassPhrase = $settings['trader_sandbox']['Passphrase'];
$urlBase = "https://api-public.sandbox.exchange.coinbase.com";
//get timestamp
$date = new \DateTime("now", new \DateTimeZone("America/Los_Angeles"));
$timestamp = $date->getTimestamp();
//API url
$relUrl = "/orders";
//set the method type GET|POST|DELETE|PUT
$method = "POST";
$params = [
"type" => "market",
"side" => "sell",
"product_id" => "BTC-USD",
"stp" => "dc",
"size" => "0.10000000"
];
//copied from coinbase's documentation added apiSecret
function signature($request_path = '', $body = '', $timestamp = false, $method = 'GET', $apiSecret=null) {
/**
* Modified $body assignment to exclude empty bodies
* @author Jared Clemence <jaredclemence@alum.drexel.edu>
* @ref https://community.coinbase.com/t/get-fills-invalid-signature-error-php-example-included/911
*/
$body = is_array($body) ? ( \count($body) > 0 ? json_encode($body) : null ) : $body;
$timestamp = $timestamp ? time() : $timestamp;
$what = $timestamp . $method . $request_path . $body;
return base64_encode(hash_hmac("sha256", $what, base64_decode($apiSecret), true));
}
$url = $urlBase . $relUrl;
$ch = curl_init($url);
$output = \json_encode($params);
$signature = \signature($relUrl, $output, $timestamp, $method, $apiSecret);
$headers = [
"User-Agent" => "Traderbot/v1.0",
"CB-ACCESS-KEY" => $apiKey,
"CB-ACCESS-SIGN" => $signature,
"CB-ACCESS-TIMESTAMP" => $timestamp,
"CB-ACCESS-PASSPHRASE" => $apiPassPhrase,
"Content-Type" => "application/json"
];
\curl_setopt($ch, CURLOPT_POST, true);
\curl_setopt($ch, CURLOPT_POSTFIELDS, $output);
foreach ($headers as $key => &$header) {
//this I found is necessary. Before I added this, the headers lacked the field data and only had the content values
$header = "{$key}:$header";
}
\curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
\curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
\curl_close($ch);
try {
$newResult = \json_decode($result);
$result = $newResult;
} catch (Exception $ex) {
}
var_dump( $result );
/**
*
* Output:
class stdClass#2 (1) {
public $message =>
string(13) "Invalid price"
}
*/