php - when i send more then 1 header it returns 400 error
Get the solution ↓↓↓$url = 'https://discord.com/api/guilds/******323098841117/members/'.$_SESSION['cid'];
$data = array("access_token" => 'Bearer '.$_SESSION['token']);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));
$arr = ['Content-Type: application/json','Authorization: Bot NzI2NTQ5NjI1MDYx**********xBakg.ZfpEdgqsW3rZuPLTXSm5Q5p20bU'];
curl_setopt($ch, CURLOPT_HTTPHEADER, $arr);
$response = curl_exec($ch);
echo($response);
echo("Bearer ".$_SESSION['token']);
echo("Welcome back, ".$_SESSION['name']);
400 error
in headers bot token
bot is already on server
Answer
Solution:
The problem is not the header itself. The content-type sent isapplication/json
, but the data transmitted via cURL is not JSON, but a URL encoded key/value pair.
$data
is an array like set here
$data = array("access_token" => 'Bearer '.$_SESSION['token']);
and you correctly transform it to text
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
But it has to be converted to JSON, so just replacehttp_build_query
withjson_encode
like below.
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: too few arguments to function laravel
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.