2013-02-11 27 views
1

我已經註冊了基於Web的短消息服務,以發送短信以確認Webform提交。我使用的捲曲和我的PHP代碼如下使用捲曲將名稱值對發佈到URL

$url = "http://www.mysmservice.co.uk/smsgateway/sendmsg.aspx?"; 
$param = "username=" . $username . "&password=" . $password . "&to=" . $diner_mobile . "&text="; 
$smsmessage = "Hello, your table booking for " . $bookingdate . " at " . $booking_time . " is confirmed " , " Myrestaurant"; 

$ch = curl_init() or die(curl_error()); 
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS,$param); 
curl_setopt($ch, CURLOPT_PORT, 80); 
curl_setopt($ch, CURLOPT_URL,$url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$data1=curl_exec($ch) or die(curl_error()); 
curl_close($ch); 

但它似乎並沒有張貼任何內容到URL(mysmsservice告訴我,該日誌不顯示任何傳入的請求)。然而,如果我訪問以下URL並替換適當的變量,服務工作。

http://www.mysmsservice.co.uk/smsgateway/sendmsg.aspx?username=MyUsername &密碼= MyPassword輸入&爲= 44771,44771054321&文本= TheMessage

不知道如果我使用捲曲調用正確。任何幫助將不勝感激。提前致謝。

回答

0

如果你是說,如果您訪問的頁面的所有參數直接在地址欄中輸入(如GET參數)的作品那麼就意味着你不不需要進行POST調用。

在這種情況下,你甚至不需要使用捲曲:

$base = 'http://www.mysmservice.co.uk/smsgateway/sendmsg.aspx'; 
$params = array(
    'username' => $username, 
    'password' => $password, 
    'to'  => $diner_mobile, 
    'text'  => 'Your booking has been confirmed', 
); 
$url = sprintf('%s?%s', $base, http_build_query($params)); 
$response = file_get_contents($url); 

如果你這樣做,不過,需要使用POST,這應該工作:

$curl = curl_init(); 
curl_setopt_array($curl, array(
    CURLOPT_URL   => $base, 
    CURLOPT_POST   => 1, 
    CURLOPT_POSTFIELDS  => $params, 
    CURLOPT_RETURNTRANSFER => 1, 
    CURLOPT_SSL_VERIFYHOST => 0, // to avoid SSL issues if you need to fetch from https 
    CURLOPT_SSL_VERIFYPEER => 0, // same^
)); 
$response = curl_exec($curl); 

注:我沒有明確地測試代碼,但這是我通常做cURL請求的方式。

+0

謝謝,讓我檢查這個片段。 – 2013-02-12 01:43:31

0

也許SMS服務表示消息丟失時沒有有效的請求。如果你看看你的代碼:

$param = "username=" . $username . "&password=" . $password . "&to=" . $diner_mobile . "&text="; 

你永遠不會把消息添加到$參數。不過,您可以在變量$ smsmessage中構建它。你應該修改你的代碼是這樣的:

$smsmessage = "Hello, your table booking for " . $bookingdate . " at " . $booking_time . " is confirmed, " . " Myrestaurant"; 
$param = "username=" . $username . "&password=" . $password . "&to=" . $diner_mobile . "&text=" . $smsmessage; 
+0

是的,你的網址不正確,缺少某些參數。 – Husman 2013-02-11 16:40:28

+0

對不起,在我的代碼$ param = $ param中錯過了這行。 $ smsmessage; – 2013-02-11 16:55:41