【问题标题】:how to use curl to incorporate this into my script?如何使用 curl 将其合并到我的脚本中?
【发布时间】:2013-02-25 02:19:02
【问题描述】:

有人建议我在我的脚本中使用它

        curl -X POST https://api.twilio.com/2010-04-01/Accounts/HIDDEN/SMS/Messages.json \
        -u HIDDEN\
        -d "From=+442033228389" \
        -d "To=hidden" \
        -d 'Body=test'

但是简单的剪切和粘贴不能解决问题?我将如何将其合并到我的脚本中?

结果:

var_dump($输出); 返回:布尔(假)

var_dump($info); 返回:

array(26) { ["url"]=> 字符串(95) "https://api.twilio.com/2010-04-01/Accounts/AC7ae43150d51cce16de4be6ed0be5ca90/SMS/Messages.json" ["content_type"]=> NULL ["http_code"]=> int(0) ["header_size"]=> int(0) ["request_size"]=> int(0) ["filetime"]=> int(-1) ["ssl_verify_result"]=> int(0) ["redirect_count"]=> int(0) ["total_time"]=> float(0.093) ["namelookup_time"]=> float(0) ["connect_time"]=> float(0.093) ["pretransfer_time"]=> float(0) ["size_upload"]=> 浮动(0) ["size_download"]=> 浮动(0) ["speed_download"]=> 浮动(0) ["speed_upload"]=> 浮动(0) ["download_content_length"]=> 浮动(-1) ["upload_content_length"]=> 浮动(-1) ["starttransfer_time"]=> 浮动(0) ["redirect_time"]=> 浮动(0)[“redirect_url”]=>字符串(0)“”[“primary_ip”]=>字符串(15) "174.129.254.101" ["certinfo"]=> 数组 (0) { } ["primary_port"]=> int(443) ["local_ip"]=> 字符串(11) "192.168.0.2" ["local_port"]=> 整数(28469)}

【问题讨论】:

标签: php curl


【解决方案1】:
<?php

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://api.twilio.com/2010-04-01/Accounts/HIDDEN/SMS/Messages.json");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, true);

$data = array(
    'From' => '+442033228389',
    'To' => 'hidden',
    'Body' => 'test'
);
/* // WHERE $username = your account username
   // Where $password = Your account password
*/
curl_setopt($ch, CURLOPT_USERPWD, $username . ":" . $password);

curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
$output = curl_exec($ch);
$info = curl_getinfo($ch);
curl_close($ch);

【讨论】:

  • 我正在向网络服务发送数据,而不是提取数据
  • 发送数据遵循相同的路径,例如,您只需将其更改为 Post。在这里查看更多详细信息。您仍将使用同一个库...我正在用详细信息更新我的答案。
  • 这应该根据您的示例来解决问题,根据 api 所需的输入,您可能只需要在将数组添加到 curl_setopt 之前对该数组进行 json_encode
  • 刚刚运行了你给我的例子,更新了所需的信息,但我仍然没有收到我预期的通知
  • 您似乎缺少身份验证。编辑您的原始问题并删除您的帐户 URL 项目,这样每个人都看不到它。此外,您还需要添加我将在上面的示例中编辑的身份验证项目,请参阅 cmets。
【解决方案2】:

如果你想在 PHP 脚本中执行 shell 命令,你必须使用函数 execshell_execsystemproc_open 或简单的反引号运算符 `

$output = `curl -X POST https://api.twilio.com/2010-04-01/Accounts/HIDDEN/SMS/Messages.json -u HIDDEN -d "From=+442033228389" -d "To=hidden" -d 'Body=test'`;

但是,如果您想在 PHP 中使用 curl 功能,更好的方法是使用 curl 扩展。举个例子:

<?php

// check if the curl extension is available
if(!function_exists('curl_init')) {
    die('the curl extension is not installed');
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.twilio.com/2010-04-01/Accounts/HIDDEN/SMS/Messages.json');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "From=+442033228389\nTo=hidden\nBody=test");

$result = curl_exec($ch);

// json_decode is used to translate the result into an object
var_dump(json_decode($result));

【讨论】:

  • 您的示例在 vardump 上返回 null
  • 和我的完全不同,有什么想法吗?
  • 我会使用wireshark追踪流量
猜你喜欢
  • 2012-07-16
  • 2022-11-11
  • 2018-05-02
  • 2011-04-30
  • 1970-01-01
  • 2014-11-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多