简体   繁体   English

cURL与PHP等效

[英]cURL to PHP equivalent

I am trying to correctly convert a cURL command to PHP, but I can't seem to find the correct commands. 我正在尝试将cURL命令正确地转换为PHP,但是我似乎找不到正确的命令。 I can run this command with cURL but I need to run this through TROPO, and they only have a few script options. 我可以使用cURL运行此命令,但是我需要通过TROPO运行此命令,并且它们只有几个脚本选项。

So I am trying in PHP. 所以我正在尝试使用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;
    } 
?>

Try to use cURL command below: 尝试在下面使用cURL命令:

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

Got this answer from https://superuser.com/a/149335 https://superuser.com/a/149335获得了这个答案

UPDATED: 更新:

Here I have provided PHP code: 在这里,我提供了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);

Hope this will help 希望这会有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM