【问题标题】:Insert a PHP variable inside Curl在 Curl 中插入 PHP 变量
【发布时间】:2017-10-19 17:50:10
【问题描述】:

我的 Curl 命令可以正常工作

$ch = curl_init('http://api.pushingbox.com/pushingbox?devid=vB9C6311111098CB&sensor=DELTA&temperature=23');
curl_exec ($ch);

curl_close ($ch);

但我需要更改 de fix 值 DELTA 和 23 以跟随 php 变量

$传感器 $温度

我的问题是:如何在 Curl 命令中插入变量 $sensor 和 $temperature?

谢谢

【问题讨论】:

    标签: php curl


    【解决方案1】:

    您可以简单地连接这些值,例如:

    $delta = "your value";
    $temp = 23;
    
    $url = 'http://api.pushingbox.com/pushingbox?devid=vB9C6311111098CB&sensor=' . $delta . '&temperature=' . $temp;
    
    // or 
    // $url = "http://api.pushingbox.com/pushingbox?devid=vB9C6311111098CB&sensor=$delta&temperature=$temp";
    
    $ch = curl_init($url);
    curl_exec ($ch);
    
    curl_close ($ch);
    

    【讨论】:

    • 您能否指出发生了什么问题,以便我们为您提供帮助?
    • 使用这两个例子,不要发送消息。我不知道为什么
    • 如果挂载的 url 与您在它必须工作之前使用的相同。如果它不工作,你做错了什么。
    【解决方案2】:

    您的 CURL 命令是一个字符串。有几种方法可以将变量解析为字符串。

    使用双引号:

    $ch = curl_init("http://api.pushingbox.com/pushingbox?devid=vB9C6311111098CB&sensor=$sensor&temperature=$temperature");

    串联:

    $ch = curl_init('http://api.pushingbox.com/pushingbox?devid=vB9C6311111098CB&sensor='.$sensor.'&temperature='.$temperature);

    可能是最常见的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-05
      • 1970-01-01
      • 2015-10-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      • 2016-04-24
      相关资源
      最近更新 更多