[英]CURL POST request is not working with headers
im trying to send a post request to this API and it returns me with "error": "unauthorized", "error_description": "An Authentication object was not found in the SecurityContext"
when i send the same request with postman it works fine.我试图向这个 API 发送一个 post 请求,它返回给我
"error": "unauthorized", "error_description": "An Authentication object was not found in the SecurityContext"
当我使用 Z03D476861AFD3851410 发送相同的请求时它工作正常。
here is the code that I'm using这是我正在使用的代码
$url = config('payhere.cancel_url');
$postRequest = array(
'subscription_id'=>$payhereID
);
$headers = array(
'Authorization' =>'Bearer '.$accessToken,
'Content-Type'=>'application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// SSL important
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,$postRequest);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch);
curl_close($ch);
$respo = $this -> response['response'] = json_decode($output);
The headers should be defined as an array, not an associative array.标头应定义为数组,而不是关联数组。
Also, because you have set your Content-Type as application/json
, your request data should be represented as JSON, this can be done using json_encode
.此外,由于您已将 Content-Type 设置为
application/json
,因此您的请求数据应表示为 JSON,这可以使用json_encode
来完成。
$url = config('payhere.cancel_url');
$postRequest = array(
'subscription_id'=>$payhereID
);
$headers = array(
'Authorization: Bearer '.$accessToken,
'Content-Type: application/json'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
// SSL important
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($postRequest));
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($ch);
curl_close($ch);
$respo = $this->response['response'] = json_decode($output);
https://www.php.net/manual/en/function.curl-setopt.php https://www.php.net/manual/en/function.curl-setopt.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.