Posting parameters and array in header in CURL using PHP

Clash Royale CLAN TAG#URR8PPP
Posting parameters and array in header in CURL using PHP
i am facing an issue i just need to send an array in header and some parameters in body but when i add
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Token:'.$token));
this line sends token but parameters array get empty but when i just remove the line, parameters successfully send but token is missing
here is the code have look:
$data = array(
'name' => $_POST['name'],
'city' => $_POST['city'],
'mobileNo' => $_POST['mobileNo'],
);
$params = http_build_query($data);
$curl = curl_init('URL');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
curl_setopt($curl, CURLOPT_HTTPHEADER,
array('Token:'.$token));
$result = curl_exec($curl);
1 Answer
1
It is because you overwrite headers and because of that missing content type header. You must add that header to your headers array:
curl_setopt($curl, CURLOPT_HTTPHEADER,array(
'Token: '.$token,
'Content-Type: application/x-www-form-urlencoded',
'Content-Length: '.strlen($params)
));
edit: I've updated post to the working solution
I have added the 'Content-Type: multipart/form-data' in my code but still parameters are empty
– Yasir Mehmood
Aug 10 at 7:43
Did you remove line I've put in PS section?
– nospor
Aug 10 at 7:44
Yes i have removed it but still not working
– Yasir Mehmood
Aug 10 at 7:46
Ok, one more try: bring back line I told to remove, set Content-type to
application/x-www-form-urlencoded and add another header: 'Content-Length: '.strlen($params)– nospor
Aug 10 at 8:16
application/x-www-form-urlencoded
'Content-Length: '.strlen($params)
Error is resolved thank you very much for helping i was stuck in this problem from last 2 days <3
– Yasir Mehmood
Aug 10 at 11:10
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
@FrankerZ still same issue
– Yasir Mehmood
Aug 10 at 7:11