doudizhi947129 2018-07-13 07:44
浏览 76

cURL PHP标头和数据

I am attempting to recreate a cURL request that looks like this:

curl -X "POST" "https://urlhere.com" \
  -H "authorization: TOKEN" \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d $'{
   "subscriptionid": "",
   "templateid": "",
   "to": [
      ""
   ],
   "subject": "",
   "data": {
      "foo": 123,
      "bar": 123
   }
}'

Can anyone help me figure out how to create this in PHP? I currently have:

curl_setopt($cURL,CURLOPT_URL, $url);
curl_setopt($cURL,CURLOPT_POST, 1);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array('authorization: '. $token));
curl_setopt($cURL, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($cURL, CURLOPT_HTTPHEADER, array('Accept: application/json'));
curl_setopt($cURL,CURLOPT_POSTFIELDS, $json);
curl_setopt($cURL, CURLINFO_HEADER_OUT, true);

And of course my init, exec, and close statements.

But I receive back an error 401. Unauthorized.

  • 写回答

1条回答 默认 最新

  • douxigai8757 2018-07-13 08:40
    关注

    According to your given curl this is the code which i generated from postman

    <?php
    
    $curl = curl_init();
    
    curl_setopt_array($curl, array(
      CURLOPT_URL => "https://urlhere.com/",
      CURLOPT_RETURNTRANSFER => true,
      CURLOPT_ENCODING => "",
      CURLOPT_MAXREDIRS => 10,
      CURLOPT_TIMEOUT => 30,
      CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
      CURLOPT_CUSTOMREQUEST => "POST",
      CURLOPT_POSTFIELDS => "{
       \"subscriptionid\": \"\",
       \"templateid\": \"\",
       \"to\": [
          \"\"
       ],
       \"subject\": \"\",
       \"data\": {
          \"foo\": 123,
          \"bar\": 123
       }
    }",
      CURLOPT_HTTPHEADER => array(
        "accept: application/json",
        "authorization: TOKEN",
        "cache-control: no-cache",
        "content-type: application/json"
      ),
    ));
    
    $response = curl_exec($curl);
    $err = curl_error($curl);
    
    curl_close($curl);
    
    if ($err) {
      echo "cURL Error #:" . $err;
    } else {
      echo $response;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用