2011-03-29 11 views
0

我有一個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); 

?> 

回答

1

那麼當我查看錯誤的代碼時,我看到了一些內容。主要的事情是1)$行變量之一被命名爲$ _row; 2)當你循環查詢的返回時,你應該使用標準模式while ($row = mysql_fetch_array($result)),然後每一行都會循環遍歷,而不是隻讀取第一行。 這裏是你想要做

<?php 
$host  = "localhost"; // Host name 
$username = "user"; // Mysql username 
$password = "password"; // Mysql password 
$db_name = "db"; // Database name 
$tbl_name = "mysqltb"; // Table name 

$user  = "smsgateway_user"; //sms user 
$pass  = "smsgateway_password"; //sms password 
$sender = "sendername"; //sms sender name 

$postUrl = "http://www.infobip.com/AddOn/SMSService/XML/XMLInput.aspx"; //XML Post url 

// 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 WHERE `send_status`=0"; 
$result = mysql_query($sql); 

// Start looping rows in mysql database. 
$totalCount = mysql_num_rows($result); 
$successCount = 0; 
while ($row = mysql_fetch_array($result)) 
{ 
    $mobileno = $row['mobileno']; 
    $name = $row['name']; 

    $message = "Dear $name "; //Start message 
    $message .= "you have received a customized bulk sms that is suppose to display your name"; //append to message 


    // XML-formatted data 

    $xmlString = 
    "<SMS> 
    <authentification> 
    <username>$user</username> 
    <password>$pass</password> 
    </authentification> 
    <message> 
    <sender>$sender</sender> 
    <text>$message</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); 

    //Might want to check the response here, see if it gives a true, or a 1 to say the message was sent successfully 
    if ($response) 
    { 
     $sql = "UPDATE `$tbl_name` SET `send_status`=1 WHERE `mobileno`='$mobileno' LIMIT 1"; 
     $result = mysql_query($sql); 
     if (mysql_affected_rows($result) == 1) //success updating database 
     { 
      $successCount++; 
     } 
    } 
    // redirect the page upon successful sending 
    curl_close($ch); 
} 
if ($successCount == $totalcount) 
    header("Location:customized/successfullysentbulksms.php"); 
else 
    echo "Error Sending. $successCount out of $totalcount were successfully sent"; 
?> 

注意什麼一個例子:因爲這個使用您的數據庫和SMS提供,我一直沒能測試這個代碼。
如果您對其工作原理有任何疑問,我會很樂意回答。

編輯:
我已經更新了代碼,以包含一個mysql更新語句,在發送消息後將列'send_status'設置爲true。我還修改了mysql select語句以僅從數據庫中獲取尚未發送到的移動號碼(send_status爲false)。
希望有幫助

+0

@mazzzzz感謝您的解釋很明確的答案,我試了一下,它工作得很好。我只需要做更多的調整,例如引入一個名爲「send_status」的新字段,並自動將我的表格行更新爲「已發送」,以便跟蹤已成功發送的收件人。我將很高興看到這個功能如何嵌入到你的答案中。 – tundewoods 2011-03-30 00:28:40

+0

重新上傳包含更改的代碼。我會建議你學習一些SQL。數據庫後端PHP功能更強大。 – Ben 2011-03-30 01:00:53

+0

@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