【问题标题】:cURL to PHP equivalentcURL 到 PHP 等价物
【发布时间】:2016-07-03 05:00:23
【问题描述】:

我正在尝试将 cURL 命令正确转换为 PHP,但似乎找不到正确的命令。我可以使用 cURL 运行此命令,但我需要通过 TROPO 运行此命令,而且它们只有几个脚本选项。

所以我正在尝试使用 PHP。

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

我的尝试,我错过了什么?

<?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;
    } 
?>

【问题讨论】:

  • *&gt; 是拼写错误还是出现在您的原始代码中? (应该是?&gt;
  • @Matt 有趣的是,*? 在键盘上的距离很远。那么它怎么可能是一个错字
  • @nmnsud :D 我的意思是,当然,是原始代码中的错误还是在将其转录到 Stack Overflow 时引入的?
  • @nmnsud 同样,并非所有键盘布局都如此en.wikipedia.org/wiki/File:KB_Japanese.svg
  • @Matt 啊!它与我的键盘完全不同。 :)

标签: php curl


【解决方案1】:

尝试使用下面的 cURL 命令:

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

https://superuser.com/a/149335得到这个答案

更新:

这里我提供了PHP代码:

$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);

希望这会有所帮助

【讨论】:

  • 也可以。但我没有看到它的 PHP 解决方案。
  • 这行得通,但在我的阅读中,我认为 CURLOPT_POSTFIELDS 就像在 URL 中添加参数,并且 -d 将信息放在正文中。
猜你喜欢
  • 2012-12-10
  • 1970-01-01
  • 1970-01-01
  • 2014-04-18
  • 2015-04-18
  • 2020-09-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多