如何使用curl将其合并到我的脚本中?

时间:2013-02-25 02:19:03

标签: php curl

我被建议在我的脚本中使用它

        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($输出);  返回:bool(false)

的var_dump($信息);  返回:

  

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”] =>浮动(0)   [ “CONNECT_TIME”] => float(0.093)[“pretransfer_time”] =>浮动(0)   [ “size_upload”] => float(0)[“size_download”] =>浮动(0)   [ “speed_download”] => float(0)[“speed_upload”] =>浮动(0)   [ “download_content_length”] => float(-1)[“upload_content_length”] =>   float(-1)[“starttransfer_time”] => float(0)[“redirect_time”] =>   float(0)[“redirect_url”] => string(0)“”[“primary_ip”] =>串(15)   “174.129.254.101”[“certinfo”] => array(0){} [“primary_port”] =>   int(443)[“local_ip”] => string(11)“192.168.0.2”[“local_port”] =>   int(28469)}

2 个答案:

答案 0 :(得分:1)

如果你想在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中使用curls功能,更好的方法是使用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));

答案 1 :(得分: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);