我有一個php curl腳本,我的短信網關提供了這個腳本,可以讓我通過xml發送短信。原始腳本就是我下面的內容。在嵌入XML的php curl函數中需要LOOP語句的幫助
//////////////////////////////原始PHP捲曲XML從柵極方式
<?php
$user="smsgateway_user";
$pass="smsgateway_password";
$sender= "sendername";
$mobileno="2348034057037";
$message= "Your sms message goes here";
?>
<?php
$postUrl = "http://www.infobip.com/AddOn/SMSService/XML/XMLInput.aspx";
// XML-formatted data
$xmlString =
"<SMS>
<authentification>
<username>$user</username>
<password>$pass</password>
</authentification>
<message>
<sender>$sender</sender>
<text>$message</text>
</message>
<recipients>
<gsm>$mobileno</gsm>
</recipients>
</SMS>";
// previously formatted XML data becomes value of 「XML」 POST variable
$fields = "XML=" . urlencode($xmlString);
// in this example, POST request was made using PHP’s CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
// response of the POST request
$response = curl_exec($ch);
// redirect the page upon successful sending
header("Location:customized/successfullysentbulksms.php");
curl_close($ch);
?>
/// code end
我代碼如何試圖調整代碼,以便使我能夠通過連接到具有以下字段(id,name,mobileno)的mysql表發送定製的多個短消息,例如,我可以選擇10個收件人併發送定製消息,以便每個收件人收到相同的消息與他的名字顯示在消息,如「親愛的(。$姓名),謝謝你今天訪問我們的商店」
從我知道的小php,我相信我想連接到數據庫選擇我的收件人,然後編寫一個「do或while循環」,使腳本能夠重複或循環此功能,直至它已成功向所有收件人發送短信。
我目前被困在嵌入我的循環功能,請我會很高興,如果有人可以看看我迄今爲止做了什麼,並幫助我。
我調整過的代碼版本//////////////////////////////////////// ///
<?php
$host="localhost"; // Host name
$username="user"; // Mysql username
$password="password"; // Mysql password
$db_name="db"; // Database name
$tbl_name="mysqltb"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Retrieve data from database
$sql="SELECT * FROM $tbl_name";
$result=mysql_query($sql);
// Start looping rows in mysql database.
$row=mysql_fetch_array($result);
?>
<?
mysql_close();
?>
<?php
$mobileno = $row['mobileno'];
$name = $_row['name'];
$user="smsgateway_user";
$pass="smsgateway_password";
$sender= "sendername";
?>
<?php
$message = "you have received a customized bulk sms that is suppose to display your name";
$message2= "Dear ".$name." ".$message ;
$postUrl = "http://www.infobip.com/AddOn/SMSService/XML/XMLInput.aspx";
// XML-formatted data
$xmlString =
"<SMS>
<authentification>
<username>$user</username>
<password>$pass</password>
</authentification>
<message>
<sender>$sender</sender>
<text>$message2</text>
</message>
<recipients>
<gsm>$no</gsm>
</recipients>
</SMS>";
// previously formatted XML data becomes value of 「XML」 POST variable
$fields = "XML=" . urlencode($xmlString);
// in this example, POST request was made using PHP’s CURL
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $postUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
// response of the POST request
$response = curl_exec($ch);
// redirect the page upon successful sending
header("Location:customized/successfullysentbulksms.php");
curl_close($ch);
?>
@mazzzzz感謝您的解釋很明確的答案,我試了一下,它工作得很好。我只需要做更多的調整,例如引入一個名爲「send_status」的新字段,並自動將我的表格行更新爲「已發送」,以便跟蹤已成功發送的收件人。我將很高興看到這個功能如何嵌入到你的答案中。 – tundewoods 2011-03-30 00:28:40
重新上傳包含更改的代碼。我會建議你學習一些SQL。數據庫後端PHP功能更強大。 – Ben 2011-03-30 01:00:53
@mazzzzz另一個非常感謝的快速響應,我試了修改,但我注意到我得到了以下錯誤顯示:解析錯誤:語法錯誤,意想不到的T_ENCAPSED_AND_WHITESPACE,希望T_STRING或T_VARIABLE或T_NUM_STRING在/ home/notifytx/public_html/api第66行的/smsloop2.php。我想這個錯誤是由於$ row ['mobileno']所以我用$ no替換它。這但是工作,但唯一的問題是,它沒有執行正則循環,而是隻發送和短信以及更新單個記錄。我想知道爲什麼 ? – tundewoods 2011-03-30 01:53:50