需要关于嵌入XML的php curl函数中的循环语句的帮助吗
我的短信网关提供了一个php curl脚本,使我能够通过xml发送短信 //////////////////////////////来自gateway的原始php curl xml代码需要关于嵌入XML的php curl函数中的循环语句的帮助吗,php,xml,loops,sms,Php,Xml,Loops,Sms,我的短信网关提供了一个php curl脚本,使我能够通过xml发送短信 //////////////////////////////来自gateway的原始php curl xml代码 <?php $user="smsgateway_user"; $pass="smsgateway_password"; $sender= "sendername"; $mobileno="2348034057037"; $message= "Your sms message goes here";
<?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、名称、mobileno)的mysql表来发送自定义的多条短信,例如我可以选择10个收件人并发送自定义消息,以便每个收件人都能收到相同的消息,其名称显示在消息中,例如“亲爱的(.$name),感谢您今天光临我们的店铺“
根据我所知道的小php,我相信我应该连接到数据库来选择我的收件人,然后编写一个“do或while循环”,使脚本能够重复或循环此函数,直到它成功地向所有收件人发送sms为止
我目前正忙于嵌入我的循环函数,如果有人能看看我到目前为止所做的事情并帮助我,我将非常高兴
我修改过的代码版本///////////////////////////////////////////
<?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变量名为$\u row;2) 当您循环查询返回时,应该使用标准模式
而($row=mysql\u fetch\u 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语句,只从数据库中获取尚未发送到的手机号码(发送状态为false)。
希望对你有所帮助@mazzzzz谢谢你的解释性回答,我试过了,效果很好。我只需要再做一些调整,比如引入一个名为“send_status”的新字段,并自动将表行更新为“sent”,以便跟踪成功发送的收件人。我将很高兴看到如何将此功能嵌入到your answer中。重新上传带有更改的代码。我建议您学习一些sql。PHP的数据库后端功能强大得多。@mazzzzz另一个非常感谢您的快速响应,我尝试了修改,但我注意到出现了以下错误显示:解析错误:语法错误,意外的T_-ENCAPSED_和_-WHITESPACE,第66行的/home/notifytx/public\u html/api/smsloop2.php中应为T_字符串、T_变量或T_NUM_字符串。我猜这个错误是由于$row['mobileno']引起的,所以我用$no替换了它。虽然这样做有效,但唯一的问题是它没有执行常规循环,而是只发送短信和更新了一条记录。我想知道为什么?我想这是因为我使用了错误的变量,因为我不能访问你的网关等等,我实际上无法检查语法(没有抛出curl错误)。我更新了它,希望它能起作用。另外,我设置了它,这样脚本将只循环通过尚未发送到的数字,发送状态设置为0(false)@mazzzzzz请您将您的电子邮件地址发送到tundeaguda[在]gmail.com给我。我遇到了更多的错误,但我想给你发一封带有附件的邮件,以便更好地解释。谢谢你到目前为止的帮助。