循环中的批量sms-php函数



我一直在使用下面的代码向客户发送短信,告诉他们在给定期限后的账户余额。到目前为止,该代码能够一次向一个人发送短信。我可以修改代码以将消息发送给所有客户吗。

<?php
function sendsms($sms_username,$sms_password,$sender_ID,$number,$message)
{
if(isset($sms_username) && isset($sms_password))
{
$username = trim($sms_username);//your sms user account
$password = trim($sms_password);//your sms password
$type = '0';//FOR FLASH MESSAGES SET type to 1
$dlr = '1';//request for delivery report
$destination = $number;;//phone number 
$source = urlencode($sender_ID);//This is the sender or label that will be received by client/user. MAXIMUM 11 CHARACTERS
$message = urlencode("$message");//message content to be sent to user/client
//Form url api
$url = "https://api.sendingthesms/bulksms/?username=" .$username."&password=".$password."&type=0&dlr=1&destination=".$destination."&source=".$source."&message=" . $message;
$scc = stream_context_create(array('http' => array('protocol_version' => 1.1)));//enforce http 1.1
$response = file_get_contents("$url",0,$scc);//fire/call nalo sms api
return $response;
}else
{
return "username cannot be empty";
}
}

echo sendsms("username","mypassword","$session_username",$mainphone3, $mysms);
header("location:index.php");
exit;
?>

有多种方法可以做到这一点。

(对我来说(好的做法是,将所有数字放在一个数组中,然后传递到上面指定的函数中。然后使用Foreach循环遍历所有数字。我在下面指定一个示例代码。希望你明白!


$numbers = ['1123234', '34343234', '12334234'];
function sendsms($sms_username,$sms_password,$sender_IDs,$numbers,$message)
{
if(isset($sms_username) && isset($sms_password))
{
$username = trim($sms_username);//your sms user account
$password = trim($sms_password);//your sms password
$type = '0';//FOR FLASH MESSAGES SET type to 1
$dlr = '1';//request for delivery report
foreach($numbers as $number){
$destination = $number;;//phone number 
$source = urlencode($sender_ID);
$message = urlencode("$message");
//Form url api
$url = "https://api.sendingthesms/bulksms/?username=" .$username."&password=".$password."&type=0&dlr=1&destination=".$destination."&source=".$source."&message=" . $message;
$scc = stream_context_create(array('http' => array('protocol_version' => 1.1)));//enforce http 1.1
$response = file_get_contents("$url",0,$scc);//fire/call nalo sms api

}
return $response;
}else
{
return "username cannot be empty";
}
}

谢谢@fahim152。我修改了我原来的问题,包括到mysql数据库的连接。我按照您的建议将联系人列转换为字符串数组。如果有人需要,我已经在下面发布了我的解决方案。

<?php 'include/dbconnect.php'; // This is for connection to the database
function sendsms($sms_username,$sms_password,$sender_ID,$number,$message) {
if(isset($sms_username) && isset($sms_password))
{ $username = trim($sms_username);//your sms user account
$password = trim($sms_password);//your sms password
$type = '0';//FOR FLASH MESSAGES SET type to 1
$dlr = '1';//request for delivery report
$destination = $number;//phone number 
$source = urlencode($sender_ID);//This is the sender or label that will be received by client/user. MAXIMUM 11 CHARACTERS
$message = urlencode("$message");//message content to be sent to user/client
//Form url api
$url = "https://api.mysmsserver.com/bulksms/?username=" .$username."&password=".$password."&type=0&dlr=1&destination=".$destination."&source=".$source."&message=" . $message;
$scc = stream_context_create(array('http' => array('protocol_version' => 1.1)));//enforce http 1.1
$response = file_get_contents("$url",0,$scc);//fire/call your bulk sms server's api
return $response;
}else
{
return "username cannot be empty";
}} $result = mysqli_query($conn, "SELECT phonenumber FROM users");
$phonenumbers=array();
while ($row = mysqli_fetch_row($result)) $phonenumbers[]=$row[0];
mysqli_free_result($result);
$result1 = mysqli_query($conn, "SELECT accountbalance FROM users");
$accountbalances=array();
while ($row = mysqli_fetch_row($result1)) $accountbalances[]=$row[0];
mysqli_free_result($result1);
foreach (array_combine($phonenumbers, $accountbalances) as $phonenumber => 
$accountbalance)
{
echo sendsms("username","mypassword","SendersName","$phonenumber", "$accountbalance");
}
?>

最新更新