2013-02-25 29 views
0

我已被告知在我的劇本如何使用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($ output); 返回:bool(false)

var_dump($ info); 返回:

陣列(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」 ] => float(0) [「total_time」] => float (0) [「size_upload」] => float(0)[「size_download」] => float(0) [「speed_download」] => float(0)[「speed_upload」] => float(0) [「downl float(0)[「redirect_url」] =>浮點數(-1)[「upload_content_length」] => float(-1)[「starttransfer_time」] =>浮點數(0)[「redirect_time」] => float ] => string(0)「」[「primary_ip」] => string(15) 「174.129.254.101」[「certinfo」] => array(0){} [「primary_port」] => int(443 )[ 「local_ip」] =>串(11) 「192.168.0.2」[ 「LOCAL_PORT」] => INT如果想要從PHP腳本內部shell命令exec(28469)}

+1

你看[在手動](http://php.net/manual/en/book.curl.php)? – 2013-02-25 02:21:41

+0

看看我給出的答案,[這裏](http://stackoverflow.com/questions/11572696/how-to-call-a-website-service-from-php/11572742#11572742) – Killrawr 2013-02-25 02:24:49

回答

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); 
+0

我發送數據到Web服務不拉數據 – user1968541 2013-02-25 02:28:39

+1

發送數據遵循相同的路徑,你只需將它改爲* Post *例如。在這裏尋找更多細節。你仍然會使用同一個庫...我正在更新我的答案和細節。 – 2013-02-25 02:33:29

+1

這應該根據你的例子,基於api需要的輸入,你可能只需要json_encode數組,然後再將它添加到curl_setopt – 2013-02-25 02:35:47

1

你」將不得不使用exec,shell_exec,systemproc_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擴展。這裏談到一個例子:

<?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)); 
+0

你的例子返回null vardump – user1968541 2013-02-25 02:42:46

+0

我得到http://pastebin.com/fDhPgnkZ – hek2mgl 2013-02-25 02:44:59

+0

與我的完全不同,有什麼想法? – user1968541 2013-02-25 02:57:31

相關問題