【发布时间】:2011-03-29 05:51:40
【问题描述】:
我有一个 php curl 脚本,我的 sms 网关提供了一个 php curl 脚本,可以让我通过 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 个收件人并发送一个自定义消息,以便每个收件人收到相同的消息,消息中显示他的姓名,例如“亲爱的(.$name),感谢您今天访问我们的商店”
从我所知道的小 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);
?>
【问题讨论】: