2015-11-06 23 views
1

我想發送多個短信使用循環,但它不工作。如果只有一行可以讀取,那麼它就可以工作。如何在一個循環內發送短信php

代碼:

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 . ''); 
    } 
} 
+3

'header()'導致你的代碼在加載其他URL時跳出循環。使用'file_get_contents()'或'CURL'請求來獲取這些URL。 – user2959229

回答

3

使用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.''); 
} 

標題是第一次調用退出你的循環

+1

你應該解釋爲什麼這條線路正在工作,爲什麼它是使用'header'的錯誤。 – Amarnasan

+0

不工作@Rahautos –

+0

什麼問題???? @ AjayKrishnaDutta –

0

您可以使用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); 
} 

更多使用cURL的例子在這裏給出:Techniques for Mastering cURL

+0

內循環或外循環?不在循環內工作 –

+0

@AjayKrishnaDutta循環內 – Suyog

+0

你得到什麼錯誤/問題? – Suyog