命令行cURL到php cURL当-d不是一个数组而是一个字符串


commandline cURL to php cURL when -d is not an array but a string

下面是我的cURL命令行代码

curl -v --insecure -XPOST -H 'X-USER: nxxx' -H 'X-SIGNATURE: dxxx' -H "Content-type: application/json" -d '444' 'https://cxxx.com/api/balance'

it works perfect…

我的问题是我试图将其转换为php cURL,我一直得到{"error": "AUTHENTICATION error"}

这是我的最新尝试php…

<?php
$json_url = "https://cxxx.com/api/balance";
// -d variable from cURL
$json_string = "444";
$headers = array();
$headers[] = 'X-USER: nxxx';
$headers[] = 'X-SIGNATURE: dxxx';
$headers[] = 'Content-Type: application/json';

$ch = curl_init($json_url);

// Configuring curl options
$options = array(
CURLOPT_RETURNTRANSFER => true,
CURLOPT_VERBOSE => true,
CURLOPT_SSL_VERIFYPEER => true,
CURLOPT_POST => true,
CURLOPT_HTTPAUTH => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $json_string,//encoding for -d variable cURL
);
// Setting curl options
curl_setopt_array( $ch, $options );
// Getting results
$result = curl_exec($ch); 
echo ($result);
?>

这是因为CURLOPT_SSL_VERIFYPEER被设置为true,你可以看到你的curl命令包含--insecure,那么这个CURLOPT_SSL_VERIFYPEER必须被设置为false,我也写了一个代码,发送相同的请求,你发布的curl命令,试试它

<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://cxxx.com/api/balance");
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, "444");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //since you put --insecure
curl_setopt($ch, CURLOPT_HTTPHEADER,["X-USER: nxxx","X-SIGNATURE: dxxx","Content-type: application/json"]);
$response = curl_exec($ch);
echo $response;