簡體   English   中英

在嵌入XML的php curl函數中需要LOOP語句的幫助

[英]Need help with a LOOP statement in a php curl function embedded with XML

我的短信網關提供了一個PHP curl腳本,使我可以通過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個收件人並發送自定義的消息,因此每個收件人都會收到相同的消息,並在消息中顯示他的名字,例如“ Dear(。$ name),謝謝您今天訪問我們的商店”

從我所知道的小PHP中,我相信我想連接到數據庫以選擇我的收件人,然后編寫“ do or while loop”,這將使腳本可以重復或循環執行此功能,直到成功發送短信為止給所有收件人。

我目前堅持嵌入我的循環函數,如果有人可以看看我到目前為止所做的事情並為我提供幫助,我將很高興。

我的代碼調整版本//////////////////////////////////////////

<?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)$ row變量之一被命名為$ _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)。
希望能有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM