duanhui9840 2019-05-20 12:28
浏览 73
已采纳

在PHP中命令cURL?

I'm using cURL cmd for trying a API.

I will need help to translate this into PHP :

curl -X POST "https://open.faceit.com/chat/v1/rooms/hub-4d2be024-1573-4312-9c56-425003027f08-general/messages" -H "accept: application/json" -H "Content-Type: application/json" -H "Authorization: Bearer <ACCES_TOKEN>" -d "{ \"body\": \"test\"}"

Thank you for your future help !

  • 写回答

1条回答 默认 最新

  • doushi1510 2019-05-20 12:32
    关注

    Something like this should do it, there are a lot of post on using cUrl with PHP

    How to POST JSON Data With PHP cURL? , Set bearer token with cURL

    <?php
      $curl_handle=curl_init();
      curl_setopt($curl_handle,CURLOPT_URL,'https://open.faceit.com/chat/v1/rooms/hub-4d2be024-1573-4312-9c56-425003027f08-general/messages');
      curl_setopt($curl_handle,CURLOPT_CONNECTTIMEOUT,2);
    
      $authorization = "Authorization: Bearer ".$token;
      curl_setopt($curl_handle, CURLOPT_HTTPHEADER, array('Content-Type: application/json' , $authorization ));
    
      # Return response instead of printing.
      curl_setopt( $curl_handle, CURLOPT_RETURNTRANSFER, true );
    
       $post = json_encode(["body"=>"test"]);
      curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post);
    
      $buffer = curl_exec($curl_handle);
      curl_close($curl_handle);
    
      if (empty($buffer)){
          print "Nothing returned from url.<p>";
      }
      else{
          print $buffer;
      }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 有偿请人帮写个安卓系统下禁止装软件及禁止拷入文件的程序
  • ¥100 用 H.265 对音视频硬编码 (CUDA)
  • ¥20 mpich安装完成后出问题
  • ¥15 multisim仿真
  • ¥15 stm32循迹小车代码问题
  • ¥15 输入一堆单词,使其去重输出
  • ¥15 qc代码,修改和添加东西
  • ¥50 Unity的粒子系统使用shadergraph(内置管线)制作的一个顶点偏移shader,但是粒子模型移动时,顶点也会偏移
  • ¥15 如何用python处理excel的数据(极值标准化)
  • ¥15 三向应力状态求剪应力