【发布时间】:2021-01-10 01:32:06
【问题描述】:
我尝试将请求从帖子类型发送到服务器,但没有成功,它返回状态为 400 的 BadRequest,
我的代码:
<?php
$products = array('1'=>array('amount'=>'1','product_id'=>'11250'));
$data = array('id' => '67', 'shipping' => '61', 'payment'=> '2','products'=> $products);
$cert = base64_encode('myapp@myapp.co.il:1234abcd');
$url = "https://myapp.co.il/api/aaa";
$ch = curl_init($url);
$payload = json_encode($data);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"authorization: Basic ".$cert,
"Content-Type: application/json",
"cache-control: no-cache"
));
$result = curl_exec($ch);
curl_close($ch);
var_dump($result);
?>
它如此回报我:
array (2) {["message"] => string (51) "Bad Request: Syntax error, distorted JSON" ["status"] => int (400)}
【问题讨论】:
-
您发送了
$postdata,没有在任何地方定义。 -
好吧,直接浏览到 $url 值会返回 400 那么你为什么希望你的程序收到不同的响应呢?