简体   繁体   English

-d不是数组而是字符串时,将命令行cURL转换为php cURL

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

Here is my commandline cURL code 这是我的命令行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... 它运作完美...

my problem is I am trying to convert it to php cURL and I keep getting { "error" : "AUTHENTICATION ERROR" } 我的问题是我试图将其转换为php cURL,但我不断收到{“ error”:“ AUTHENTICATION ERROR”}

Here is my latest attempt at php... 这是我对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);
?>

its because of CURLOPT_SSL_VERIFYPEER is set to true, you can see your curl command contains --insecure then this CURLOPT_SSL_VERIFYPEER must be set to false, i also wrote a code the sends the same request that the curl command you posted does, try it 其原因是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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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