数组中的用户,向他们发送短信

时间:2014-09-09 07:40:31

标签: php arrays

此代码是我的API SMS PANEL:

    <?php
require_once('./api_send_sms.php');
if (($result = Send_SMS('USERNAME', 'PASSWORD', '100002972', 'USERNUMBER', 'Hello World!', 0, false)) === '0')
{
    echo 'SMS sent successfully.';
}
else if ($result !== '')
{
    echo "Error No: $result";
}
?>

这里我有这个用户代码:

$users = array(
  array('username' => 'user1','mobile' => '0000000000'),
  array('username' => 'user2','mobile' => '0000000000'),
);

我想通过这个消息“已发送用户名消息”向所有人发送短信,我该怎么办? 问候

4 个答案:

答案 0 :(得分:3)

<?php
require_once('./api_send_sms.php');
$users = array(
    array('username' => 'user1','mobile' => '0000000000'),
    array('username' => 'user2','mobile' => '0000000000'),
);

foreach($users as $value){
    $result = Send_SMS('USERNAME', 'PASSWORD', '100002972', $value['mobile'], 'Hello World!', 0, false) ;
    if($result === 0){
        echo 'SMS sent successfully.';
    }elseif ($result != ''){
        echo "Error No: $result";
    }
}

答案 1 :(得分:2)

使用foreach:

foreach($users as $key => $user){
//send sms script
}

由于缺乏信息,我无法给你一个恰当的例子,但我希望你能得到这个想法

答案 2 :(得分:1)

假设Send_SMS()方法在成功时返回0

<?php
require_once('./api_send_sms.php');
$users = array(
               array('username' => 'user1','mobile' => '0000000000'),
               array('username' => 'user2','mobile' => '0000000000')
              );
foreach($users as $u)
{
    if (Send_SMS('USERNAME', 'PASSWORD', '100002972', $u['mobile'], 'Hello World!', 0, false) == 0)
    {
        echo 'SMS sent successfully.';
    }
    else
    {
        echo "Error on sending SMS to the user:" . $u['username'];
    }
}
?>

答案 3 :(得分:1)

您可以尝试将您的sendms代码放入单个函数中并尝试使用for循环在每个循环中实例化发送sms函数,直到您在所有用户中发送了短信为止