【问题标题】:using POST JSON data with PHP cURL. Cannot get response使用带有 PHP cURL 的 POST JSON 数据。无法得到回应
【发布时间】:2013-09-02 21:13:56
【问题描述】:

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

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

我还根据此页面将正确的 API 放入下面的 $ch 变量中

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;

?>

【问题讨论】:

  • curl_error 的任何输出?
  • 啊,它给了我这个.... 卷曲错误:SSL 证书问题,验证 CA 证书是否正常。详细信息:错误:14090086:SSL 例程:SSL3_GET_SERVER_CERTIFICATE:证书验证失败。我在本地工作

标签: php json api curl


【解决方案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

【讨论】:

  • 谢谢你解决了这个问题!
猜你喜欢
  • 2018-02-01
  • 2016-06-11
  • 1970-01-01
  • 1970-01-01
  • 2017-12-06
  • 2011-04-27
  • 2016-01-09
  • 1970-01-01
  • 2014-12-13
相关资源
最近更新 更多