2016-02-06 21 views
2

我能夠通過PHP發送短信,但我不得不提到手機號碼,每次我想用相同的腳本我可以發送短信到存儲在我的數據庫中的條目。 這裏是我的代碼:我如何發送短信到我的數據庫條目

<?php 

//Your authentication key`enter code here` 
$authKey = "API key"; 

//Multiple mobiles numbers separated by comma 
$mobileNumber = "+919425386214"; 

//Sender ID,While using route4 sender id should be 6 characters long. 
$senderId = "Social"; 

//Your message to send, Add URL encoding here. 
$message = urlencode("Test message"); 

//Define route 
$route = "04"; 
//Prepare you post parameters 
$postData = array(
    'authkey' => $authKey, 
    'mobiles' => $mobileNumber, 
    'message' => $message, 
    'sender' => $senderId, 
    'route' => $route 
); 
if (isset($_POST['submit'])) 
{ 

//API URL 
$url="https://control.msg91.com/api/sendhttp.php"; 

// init the resource 
$ch = curl_init(); 
curl_setopt_array($ch, array(
    CURLOPT_URL => $url, 
    CURLOPT_RETURNTRANSFER => true, 
    CURLOPT_POST => true, 
    CURLOPT_POSTFIELDS => $postData 
    //,CURLOPT_FOLLOWLOCATION => true 
)); 


//Ignore SSL certificate verification 
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); 


//get response 
$output = curl_exec($ch); 

//Print error if any 
if(curl_errno($ch)) 
{ 
    echo 'error:' . curl_error($ch); 
} 

curl_close($ch); 

echo $output; 
} 
?> 

請幫助,這樣我可以發送短信給我的客戶接觸的人沒有。存儲在我的數據庫

+0

我想你想在這個變量'$ mobileNumber'的動態數據?是否在這個變量中用逗號分隔數字? –

+0

是的,我想要的,但這個變量的值將存儲在我的數據庫中的數字 –

+0

ISHAN感謝標記,我希望你的問題得到解決。 –

回答

1

你需要做一些查詢代碼做到這一點,你的幫助示例代碼: -

<?php 
error_reporting(E_ALL); 
ini_set('display_errors',1); 
$mobileNumber = ''; 
$conn = mysqli_connect('localhost','root','','database name') or die (mysqli_error()); // give connection credentials here 

if($conn){ 
$query = "Select mobile_number From User"; // change table name and column name accordingly. 

$result = mysqli_query($conn,$query) or die(mysqli_error($conn)); 
if(mysqli_num_rows($result) > 0){ 
    while($row = mysqli_fetch_assoc($result)){ 
     $mobileNumber .= $row['mobile_number'].','; 
    } 
} 

} 
$mobileNumber = substr($mobileNumber,0,strlen($mobileNumber)-1); // now you have all mobile numbers in , seperated form in this variable 

注: - 您可以在當前的PHP代碼添加此代碼,做相應的改變,你會得到你想要的輸出。

+0

如何限制手機號碼爲100,然後循環如果在數據庫中有10,000個手機號碼?使用@Anant代碼選擇所有數字。 –