简体   繁体   English

使用PHP在CURL的标头中发布参数和数组

[英]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);

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 编辑:我已经更新了工作解决方案的帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM