0

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
  • @FrankerZ still same issue Commented Aug 10, 2018 at 7:11

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

6 Comments

I have added the 'Content-Type: multipart/form-data' in my code but still parameters are empty
Did you remove line I've put in PS section?
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)
Error is resolved thank you very much for helping i was stuck in this problem from last 2 days <3
So last change I said was the solution?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.