【问题标题】:How To send multiple SMS using php loop如何使用php循环发送多条短信
【发布时间】:2015-11-07 13:58:10
【问题描述】:

使用循环发送多条短信时出现问题,无法正常工作..

代码是:

 while ($row = mysql_fetch_array($result)) {

        $dealer_name = $row['dealer_name'];
        $dealer_contact_no = $row['contact_no'];

        $date = new DateTime($row['date']);
        $date = $date->format('d-M-y');
        $due_date = new DateTime($row['due_date']);
        $due_date = $due_date->format('d-M-y');

        //////////////////sms body 
        $msg = '';
        $msg .= 'Bill Payable-' . "%0A";
        $msg .= 'Bill No:' . $row['ref_no'] . "%0A";
        $msg .= 'Date:' . $date . "%0A";
        $msg .= 'Total Amt:' . $row['total_amount'] . "%0A";
        $msg .= 'Pending Amt:' . $row['pending_amount'] . "%0A";
        $msg .= 'Due Date:' . $due_date . "%0A";
        $msg .= 'Days:' . $row['days'] . "%0A";
        $msg .= '-' . $sender_name;

        $username = "*********";
        $password = "*********";
        $text = $msg;
        $phones = $dealer_contact_no;

        if (strlen($phones) == 10) {
       $url = 'http://bulksms.mysmsmantra.com:8080/WebSMS/SMSAPI.jsp?username=' . $username . '&password=' . $password . '&sendername=NETSMS&mobileno=' . $phones . '&message=' . $text;

        $ch = curl_init(); 
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        $output = curl_exec($ch);
        curl_close($ch);
        }
    }

如何一次次执行url发送多条短信.. 请帮忙..之前我使用了 header() 函数,但它只适用于获取的单行..

【问题讨论】:

  • 在此行之前设置$msg = ''; $msg .= 'Bill Payable-' 。 "%0A";
  • 您已经有一个循环可以多次运行它。你的 sql 查询是什么?
  • 你在你的问题中提到了There is a problem ....但是等等!有什么问题?

标签: php bulksms


【解决方案1】:

代码看起来不错。只需检查 create 函数,从 while 中挑选一段代码并调用它。

函数如下:

//This function used to send SMS 
function sendSMS($username, $password, $phones, $text){

   $url = 'http://bulksms.mysmsmantra.com:8080/WebSMS/SMSAPI.jsp?username=' . $username . '&password=' . $password . '&sendername=NETSMS&mobileno=' . $phones . '&message=' . $text;

    $ch = curl_init(); 
    curl_setopt($ch,CURLOPT_URL,$url);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    $output = curl_exec($ch);
    curl_close($ch);
}

并从 while 中删除相同的代码并调用此函数。

循环就像:

while ($row = mysql_fetch_array($result)) {

    $dealer_name = $row['dealer_name'];
    $dealer_contact_no = $row['contact_no'];

    $date = new DateTime($row['date']);
    $date = $date->format('d-M-y');
    $due_date = new DateTime($row['due_date']);
    $due_date = $due_date->format('d-M-y');

    //////////////////sms body 
    $msg = '';//Variable initialize with blank...
    $msg .= 'Bill Payable-' . "%0A";
    $msg .= 'Bill No:' . $row['ref_no'] . "%0A";
    $msg .= 'Date:' . $date . "%0A";
    $msg .= 'Total Amt:' . $row['total_amount'] . "%0A";
    $msg .= 'Pending Amt:' . $row['pending_amount'] . "%0A";
    $msg .= 'Due Date:' . $due_date . "%0A";
    $msg .= 'Days:' . $row['days'] . "%0A";
    $msg .= '-' . $sender_name;

    $username = "abc";
    $password = "1922345418";
    $text = $msg;
    $phones = $dealer_contact_no;

    //If Phone number length equals 10 then call send SMS functionality...
    if (strlen($phones) == 10) {
        //Send SMS function calling...
        sendSMS($username, $password, $phones, $text);
    }
}

如果有任何问题/疑问,请告诉我。

【讨论】:

  • 结果如何?任何错误,通知,与它相关的东西?你能用exit()试试吗?过了一阵子?请检查它循环了多少次。
  • 我认为 curl 不起作用..我以前没用过这个..需要一些帮助文件吗??
  • 不,这是 PHP 函数,不需要帮助文件。见:php.net/manual/en/curl.examples.php
  • 如果我回显 $url 然后我得到 3 行意味着循环正在工作.. 但没有短信发送
  • 所以我们有最后的选择来检查 cURL 是否工作正常?为此请使用: if($output){ // ?? - 如果请求和数据被完全接收 print(' :: Message send for '.$phones .':: '); } 如果需要,请告诉我。
猜你喜欢
  • 1970-01-01
  • 2018-11-02
  • 1970-01-01
  • 1970-01-01
  • 2011-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多