使用PHP cURL的POST JSON数据。无法得到回应

时间:2013-09-02 21:13:57

标签: php json api curl

您好我正在尝试使用JSON在数组中发布一些数据来接收响应并输出响应。到目前为止,我已经密切关注了所有参数,但无法获取数据。

我正在使用Coinbase API来“生成”按钮 https://coinbase.com/api/doc/1.0/buttons.html

我已经根据此页面在下面的$ ch变量中放置了正确的API

https://coinbase.com/docs/api/authentication

它无法取回任何东西。我已经发布了正确的细节以获得一些数据的响应,但它失败了,有什么想法吗?

这是我的代码

<?php
$data = array(
  "button" => array(
    "name" => "Product Name",
    "price_string" => "1.23",
    "price_currency_iso" => "USD",
    "custom" => "Order 123",
    "description" => "Sample description",
    "type" => "buy_now",
    "style" => "custom_large"
  )
);                                                                    

$json_data = json_encode($data);                                                                                   


$ch = curl_init('https://coinbase.com/api/v1/buttons?api_key=MYAPIKEY');                                                                      
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $json_data);                                                                  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);                                                                      
curl_setopt($ch, CURLOPT_HTTPHEADER, array(                                                                          
'Content-Type: application/json',                                                                                
'Content-Length: ' . strlen($json_data))                                                                       
);                                                                                                                   

$output = curl_exec($ch);

$result = json_decode($output);

echo $result->button->type;

?>

1 个答案:

答案 0 :(得分:1)

快速修复它将禁用证书检查:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

更安全和正确的是以X.509 PEM格式导出CA证书文件(签署网站证书的公司的证书)并使用其路径:

curl_setopt($ch, CURLOPT_CAINFO, "/path/to/CA.crt");

您还可以使用Mozilla证书数据库:http://curl.haxx.se/ca/cacert.pem它包含coinbase.com上使用的DigiCert High Assurance EV Root CA

相关问题