如何将短信发送到我的数据库条目

时间:2016-02-06 06:12:52

标签: php mysql sms-gateway

我能够通过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;
}
?>

请帮忙,以便我可以发送短信给我的联系号码的客户。存储在我的数据库中

1 个答案:

答案 0 :(得分: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代码中添加此代码并相应地进行更改,您将获得所需的输出。