dthl8036
2016-07-03 05:00 阅读 30
已采纳

cURL到PHP等价

I am trying to correctly convert a cURL command to PHP, but I can't seem to find the correct commands. I can run this command with cURL but I need to run this through TROPO, and they only have a few script options.

So I am trying in PHP.

curl https://api.particle.io/v1/devices/310029000547353138383138/setNum \ -d access_token=e650d0dec0476de7d64b23110fed31dcb3cbxxxx \ -d args=2085555555

My try, what am I missing?

<?php
    function submitValue() {
      $ch = curl_init("https://api.particle.io/v1/devices/310029000547353138383138/setNum");
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($ch, CURLOPT_POST, true);
      curl_setopt($ch, CURLOPT_HTTPHEADER, 'access_token=e650d0dec0476de7d64b23110fed31dcb3cbxxxx&"args=2085555555"');                                                                       
      );

      $output = curl_exec($ch);

      if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != '200') {
        return null;
      }
      return $output;
    } 
?>
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    dounianji7883 dounianji7883 2016-07-03 05:18

    Try to use cURL command below:

    curl --data "access_token=e650d0dec0476de7d64b23110fed31dcb3cbxxxx&args=2085555555" https://api.particle.io/v1/devices/310029000547353138383138/setNum
    

    Got this answer from https://superuser.com/a/149335

    UPDATED:

    Here I have provided PHP code:

    $data = json_decode(array(
          "access_token" => "e650d0dec0476de7d64b23110fed31dcb3cbxxxx",
          "args" => "2085555555"
    ));
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.particle.io/v1/devices/310029000547353138383138/setNum');
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $output = curl_exec($ch);
    curl_close($ch);
    

    Hope this will help

    点赞 评论 复制链接分享

相关推荐