2

I am trying to send multiple SMS using loop, but its not working. If there is only one row to fetch then it works.

Code :

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 .= '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 (strlen($phones) == 10) {

        header('Location:http://bulksms.mysmsmantra.com:8080/WebSMS/SMSAPI.jsp?username=' . $username . '&password=' . $password . '&sendername=NETSMS&mobileno=' . $phones . '&message=' . $text . '');
    }
}
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
Ajay Krishna Dutta
  • 722
  • 2
  • 7
  • 21
  • 3
    The `header()` is causing your code to kick out of the loop when it loads the other URL. Use `file_get_contents()` or a `CURL` request to grab those URLs instead. – user2959229 Nov 06 '15 at 07:12

2 Answers2

4

use php file_get_contents

while($row = mysql_fetch_array($result)){
file_get_contents('http://bulksms.mysmsmantra.com:8080/WebSMS/SMSAPI.jsp?username='.$username.'&password='.$password.'&sendername=NETSMS&mobileno='.$phones.'&message='.$text.'');
}

header is exit your loop in first time call

Abhishek Sharma
  • 6,689
  • 1
  • 14
  • 20
0

You can make use of cURL

while($row = mysql_fetch_array($result))
{
    $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);
}

More examples of using cURL are given here: Techniques for Mastering cURL

Suyog
  • 2,472
  • 1
  • 14
  • 27