简体   繁体   中英

using POST JSON data with PHP cURL. Cannot get response

Hi I'm trying to POST some data in an array using JSON to receive a response and output the response. So far I have followed all the parameters closely but it fails to fetch the data.

I am using the Coinbase API to 'generate' a button https://coinbase.com/api/doc/1.0/buttons.html

I have also put the correct API in the $ch variable below as per this page

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

It fails to fetch anything back. I have posted the correct details to get a response with some data but it fails, any ideas?

Here is my code

<?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;

?>

Quick fix for it will be to disable certificate checking:

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

More secure and proper will be to export CA certificate file (certificate of a company that signed site certificate) in X.509 PEM format and use path to it:

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

You can also use Mozilla certificate database: http://curl.haxx.se/ca/cacert.pem It includes DigiCert High Assurance EV Root CA used on coinbase.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM