我有一个用于发送短信的功能,效果很好。这是我使用的功能:
function send_sms() {
$acc = $this->input->post('acc');
$key = $this->input->post('key');
$to = $this->input->post('to');
$sms = $this->input->post('sms');
$url = "http://host.com/public/sms.api?acc=". $acc ."&key=". $key ."&to=". $to ."&sms=" . $sms;
$this->load->helper('html');
echo link_tag($url);
echo 'SMS Sent, thank you';
}
现在,我想向多个号码发送短信。这些数字来自我的数据库。但是这个消息并没有到达我的号码。
这是我发送批量短信的功能:
function send_bulk_sms() {
$search_by = $this->input->post('search_by');
if ($search_by == 'prefer_location') {
$location = $this->input->post('search_field');
$recipients_array = $this->company_model->search_by_location($location);
$recipients = array();
foreach ($recipients_array as $key => $value) {
$recipients[] = $value['phone'];
}
} elseif ($search_by == 'sector') {
$sector = $this->input->post('search_field');
$recipients_array = $this->company_model->search_by_sector($sector);
$recipients = array();
foreach ($recipients_array as $key => $value) {
$recipients[] = $value['phone'];
}
}
$acc = $this->input->post('acc');
$key = $this->input->post('key');
$to = $recipients;
$sms = $this->input->post('sms');
$url = "http://host.com/public/sms.api?acc=" . $acc . "&key=" . $key . "&to=" . $to . "&sms=" . $sms;
$this->load->helper('html');
echo link_tag($url);
echo 'SMS Sent, thank you';
}
数据库中的$recipients
值如下所示:
数组([0] => 03-56122223 [1] => 02158745)
我试图像这样更改$to
值:
$to = array(
'02158745',
'02158745'
);
或者像这样
$to = array(
02158745,
02158745
);
他们两个都没有工作。当我尝试像这样更改$to
值时,它会起作用:
$to = '02158745';
有没有解决方法将数组值拆分为字符串?
或者如何逐个发送短信,号码来自$recipients
?
谢谢。
答案 0 :(得分:0)
尝试数组行走功能 - Apply a user function to every member of an array
$to = array(
02158745,
02158745
);
array_walk($to, 'send_sms');
function send_sms($to,$key) {
$acc = $this->input->post('acc');
$key = $this->input->post('key');
$to = $this->input->post('to');
$sms = $this->input->post('sms');
$url = "http://host.com/public/sms.api?acc=". $acc ."&key=". $key ."&to=". $to ."&sms=" . $sms;
$this->load->helper('html');
echo link_tag($url);
}