[英]I am not getting expected response from Curl request in PHP
I have a curl request which is working fine in https://reqbin.com/ and in the Postman but when I implement same in PHP it is not working; 下面是普通的 CURL 命令;
curl -X POST \
'http://example.com' \
-H 'content-type: application/json' \
-d '{"Header": {"Token": "xyz"}, "Body": {"Email": "abc@gmail.com", "FirstName": "test", "Surname": "test"}}'
我從中得到的回應是;
{"Header":{"Status":"OK"},"Body":{"BackUrl":"https:example.com","Redirect":"YES"}}
但是當我在 PHP 中實現相同的功能時;
$curl = curl_init();
$headers = array(
"content-type: application/json"
);
$postData = '{"Header": {"Token": "xyz"}, "Body": {"Email": "abc@gmail.com", "FirstName": "test", "Surname": "test"}}';
$opt_arr = array(
CURLOPT_URL => 'http://example.com',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postData
);
curl_setopt_array($curl, $opt_arr);
echo $resp = curl_exec($curl);
curl_close($curl);
我沒有收到預期的回復;
{"Header":{"Status":"ERROR","ErrorMsg":"INVALID_REQUEST"},"Body":{"BackUrl":"","Redirect":"NO"}}
我需要幫助將上述工作 curl 轉換為正確的 PHP 代碼。
我不熟悉 PHP,盡管我看到發送"Header": {"Token": "xyz"}
的區別。 curl 命令在 post 數據中發送它,而 PHP 在 http 標頭中發送它。 PHP 變量應該是
$headers = array(
"content-type: application/json"
);
$postDataHeader = array(
'Token' => 'xyz'
);
$postDataBody = array(
'Email' => 'abc@gmail.com',
'FirstName' => 'test',
'Surname' => 'test'
);
$postData = json_encode(array(
'Header' => $postDatHeader,
'Body' => $postDataBody
));
After many unsuccessful attempts in PHP, I tried to implement it in javascript and here I got to know that error is with SSL security, hence I added HTTPS into the URL, and it worked.
$curl = curl_init();
$headers = array(
"content-type: application/json"
);
$postData = '{"Header": {"Token": "xyz"}, "Body": {"Email": "abc@gmail.com", "FirstName": "test", "Surname": "test"}}';
$opt_arr = array(
CURLOPT_URL => 'https://example.com',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => $headers,
CURLOPT_POSTFIELDS => $postData
);
curl_setopt_array($curl, $opt_arr);
echo $resp = curl_exec($curl);
curl_close($curl);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.