[英]CURL POST request is not working with headers
我試圖向這個 API 發送一個 post 請求,它返回給我"error": "unauthorized", "error_description": "An Authentication object was not found in the SecurityContext"
當我使用 Z03D476861AFD3851410 發送相同的請求時它工作正常。
這是我正在使用的代碼
$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);
標頭應定義為數組,而不是關聯數組。
此外,由於您已將 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.