11 08 2015

为了使用任何的语言发送 SMS,需要一个 SMS gateway。大部分的 SMS 会提供一个 API,这里是使用MSG91 作为 SMS gateway。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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($charray(
    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 )。

语法:

?
1
2
3
4
5
<?php
$message "Hello World";
$mobile "918112998787";
send_sms($mobile,$message);
?>
发表评论