用PHP集成Coinbase Commerce

时间:2018-06-19 06:38:53

标签: php curl coinbase-api

我想在我的一个Web应用程序中集成Coinbase Commerce API。我已经引用此链接https://commerce.coinbase.com/docs/并在本地服务器中创建演示。我成功地获得了以下输出 enter image description here

以及屏幕下方 enter image description here

现在我想知道在获得最后一个屏幕后,我会告诉我该如何处理该代码。我是否需要在任何特定应用程序中打开它,或者我必须在我的代码中使用它。如果我需要在代码中使用,请提供示例代码。

此外,我还尝试制作"创建费用"以下代码,但我没有得到任何回复。

$metadata = array(
'customer_id' => '123456',
'customer_name' => 'adarsh bhatt'
);
   $request_body = array(
    'X-CC-Api-Key' => 'd59xxxxxxxxxxxxxxb8',
    'X-CC-Version' => '2018-03-22',
    'pricing_type' => 'fixed_price',
    'name' => 'Adarsh',
    'description' => ' This is test donation',
     'local_price' => array(
    'amount' => '100.00',
    'currency' => 'USD'
   ),
    'metadata' => $metadata
);
$req = curl_init('https://api.commerce.coinbase.com/charges');
curl_setopt($req, CURLOPT_RETURNTRANSFER, false);    
curl_setopt($req, CURLOPT_POST, true);
curl_setopt($req, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($req, CURLOPT_HTTPHEADER, array('Content-Type:   application/json', 'Content-Length: ' . strlen(json_encode($request_body))));
curl_setopt($req, CURLOPT_POSTFIELDS, http_build_query($request_body));
$respCode = curl_getinfo($req, CURLINFO_HTTP_CODE);
$resp = json_decode(curl_exec($req), true);
curl_close($req);
echo '<pre>';
print_r($output);
exit;

3 个答案:

答案 0 :(得分:0)

您可以尝试这种方式。对我来说有用

$curl = curl_init();
$postFilds=array(
    'pricing_type'=>'no_price',
    'metadata'=>array('customer_id'=>10)
);
$postFilds=urldecode(http_build_query($postFilds));
curl_setopt_array($curl, 
    array(

        CURLOPT_URL => "https://api.commerce.coinbase.com/charges",

        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_POSTFIELDS => $postFilds,
        CURLOPT_HTTPHEADER => array(
            "X-CC-Api-Key: APIKEY",
            "X-CC-Version: 2018-03-22",
            "content-type: multipart/form-data"
        ),
    )
);
$response = curl_exec($curl);
$err = curl_error($curl);

curl_close($curl);

答案 1 :(得分:0)

以下应该是创建费用并返回托管URL的有效请求:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.commerce.coinbase.com/charges/");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$post = array(
    "name" => "E currency exchange",
    "description" => "Exchange for Whatever",
    "local_price" => array(
        'amount' => 'AMOUNT',
        'currency' => 'USD'
    ),
    "pricing_type" => "fixed_price",
    "metadata" => array(
        'customer_id' => 'customerID',
        'name' => 'ANY NAME'
    )
);

$post = json_encode($post);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_POST, 1);

$headers = array();
$headers[] = "Content-Type: application/json";
$headers[] = "X-Cc-Api-Key: YOUR-API-KEY";
$headers[] = "X-Cc-Version: 2018-03-22";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

$result = curl_exec($ch);
curl_close ($ch);
$response = json_decode($result);
return $response->data->hosted_url;

答案 2 :(得分:0)

您好,您可以使用php sdk进行币库交易。

https://github.com/coinbase/coinbase-commerce-php

作曲者需要coinbase / coinbase-commerce

use CoinbaseCommerce\Resources\Charge;
use CoinbaseCommerce\ApiClient;

ApiClient::init('PUT_YOUR_API_KEY');


$chargeData = [
    'name' => 'The Sovereign Individual',
    'description' => 'Mastering the Transition to the Information Age',
    'local_price' => [
        'amount' => '100.00',
        'currency' => 'USD'
    ],
    'pricing_type' => 'fixed_price'
];
$chargeObj = Charge::create($chargeData);

var_dump($chargeObj);
var_dump($chargeObj->hosted_url);