随笔 · 2016/02/03 0

PHP发送 SMS

发送 SMS
在开发 Web 或者移动应用的时候,经常会遇到需要发送 SMS 给用户,或者因为登录原因,或者是为了发送信息。下面的 PHP 代码就实现了发送 SMS 的功能。
为了使用任何的语言发送 SMS,需要一个 SMS gateway。大部分的 SMS 会提供一个 API,这里是使用 MSG91 作为 SMS gateway。

function send_sms($mobile,$msg)
{
$authKey = "XXXXXXXXXXX";
date_default_timezone_set("Asia/Kolkata");
$date = strftime("%Y-%m-%d %H:%M:%S");
//Multiple mobiles numbers separated by comma
$mobileNumber = $mobile;
            
//Sender ID,While using route4 sender id should be 6 characters long.
$senderId = "IKOONK";
            
//Your message to send, Add URL encoding here.
$message = urlencode($msg);
            
//Define route
$route = "template";
//Prepare you post parameters
$postData = array(
    'authkey' => $authKey,
    'mobiles' => $mobileNumber,
    'message' => $message,
    'sender' => $senderId,
    'route' => $route
);
            
//API URL
$url="https://control.msg91.com/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);
}

其中“$authKey = “XXXXXXXXXXX”;”需要你输入你的密码,“$senderId = “IKOONK”;”需要你输入你的 SenderID。当输入移动号码的时候需要指定国家代码 (比如,美国是 1,印度是 91 )。

语法:

<?php
$message = "Hello World";
$mobile = "918112998787";
send_sms($mobile,$message);
?&gt;

 

0
希望看到您的想法,请您发表评论x