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;
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?