如何在php中发送POST请求?
我尝试从post类型向服务器发送请求,但未成功, 它返回状态为400的请求 我的代码:如何在php中发送POST请求?,php,curl,post,https,Php,Curl,Post,Https,我尝试从post类型向服务器发送请求,但未成功, 它返回状态为400的请求 我的代码: $products = array('1'=>array('amount'=>'1','product_id'=>'11250')); $data = array('id' => '67', 'shipping' => '61', 'payment'=> '2','products'=> $products); $cert = base64_encode('myap
$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');
$header = array(
"authorization: Basic ".$cert,
"Content-Type: application/json",
"cache-control: no-cache"
);
$url = "https://myapp.co.il/api/aaa";
$curl = curl_init($url);
//curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
$response = curl_exec($curl);
curl_close($curl);
$response = json_decode($response,true);
var_dump($response);
?>
我的回答是:
数组(2){[“消息”]=>string(51)“错误的请求:语法错误,扭曲的JSON”[“状态”]=>int(400)}
但是如果将
内容类型
请求头设置为应用程序/json
然后使用CURLOPT_POSTFIELDS
,它不是用于JSON的,而是一个查询字符串
将标题更改为:
$header=[
“授权:基本”。$cert,
'内容类型:多部分/表单数据',
'缓存控制:无缓存'
];
并跳过http\u build\u query()
curl_setopt($curl,CURLOPT_POSTFIELDS,$data);
尝试echo json_encode($response);而不是$response=json_decode($response,true);然后扔掉回复。谢谢!我这样做了,但现在它这样做了:数组(2){[“消息”]=>string(22)“不支持的媒体类型”[“状态”]=>int(415)},如果您将其设置为multipart/form data
,但跳过http\u build\u query()
(只需插入数组)?现在它显示:数组(2){[“消息”=>string(48)“错误请求:无法创建邀请”[“状态”]=>int(400)}