PHP发送短信如何实现?

最近要用php发送和接收短信,用户订单要用短信通知一类的功能,网上看了好多短信平台感觉都不靠谱。

也测试了很多代码,下面把几款PHP发送短信好用的分享给大家:

PHP发送短信方法一(比较好,推荐)

 1 //PHP发送短信 Monxin专用(PHP代码函数)
 2 //本代码基于Monxin 运行
 3 //代码来源:Monxin ./config/functions.php
 4   
 5 function sms($config,$language,$pdo,$sender,$phone_number,$content){
 6     
 7   //demo var_dump(sms(self::$config,self::$language,$pdo,"system","18074507509,15507455992","测试内容,时间".date("H:i:s",time())));
 8   $sender=safe_str($sender);
 9   $content=safe_str($content);
10   $arr=explode(',',$config['sms']['disable_phrase']);
11   $disable=false;
12   foreach($arr as $v){
13     if(strpos($content,$v)!==false){$phrase=$v;$disable=true;continue;}
14   }
15   if($disable){return $language['exist_disable_phrase']." ".$phrase;}
16     
17   $phone_number=explode(',',$phone_number);
18   $phone_number=array_unique($phone_number);
19   $addressee='';
20   $count=0;
21   foreach($phone_number as $v){
22     if(preg_match($config['other']['reg_phone'],$v)){$addressee.=$v.',';}
23   }
24   $addressee=trim($addressee,',');
25   $addressee=explode(",",$addressee);
26   //var_dump($addressee);
27   $section=ceil(count($addressee)/$config['sms']['max']);
28     
29   for($i=0;$i<$section;$i++){
30     $phone[$i]='';
31     for($j=$i*$config['sms']['max'];$j<($i+1)*$config['sms']['max'];$j++){
32       //echo $j.',';
33       if(isset($addressee[$j])){$phone[$i].=$addressee[$j].$config['sms']['delimiter'];}
34     }
35     $phone[$i]=trim($phone[$i],$config['sms']['delimiter']);
36     $temp=explode($config['sms']['delimiter'],$phone[$i]);
37     $count=count($temp);
38     $length=ceil(strlen(preg_replace('/[x80-xff]{3}/','x',$content))/($config['sms']['length']/2));
39     $count=$length*$count;
40     if(!isset($timing)){$timing=0;}
41     if($phone[$i]!=''){
42       $time=time();
43       $sql="insert into ".$pdo->index_pre."phone_msg (`sender`,`addressee`,`content`,`state`,`time`,`count`,`timing`) values ('$sender','".$phone[$i]."','".$content."','1','$time','$count','0')";  
44       if($pdo->exec($sql)){
45         return send_sms($config,$pdo,$pdo->lastInsertId());
46       }else{
47         return false;
48       }
49     }
50   }
51   
52 }

例2:在PHP5中通过file_get_contents函数发送短信(HTTP GET 方式)

1 <?php   
2 $url = "http://sms.api.bz/fetion.php?username=13812345678&password=123456&sendto=13512345678&message=短信内容";   
3 $result = file_get_contents($url);   
4 echo $result; //返回信息默认为UTF-8编码的汉字,如果你的页面编码为gb2312,请使用下行语句输出返回信息。   
5 //echo iconv("UTF-8", "GBK", $result);   
6 ?> 

例3:在PHP中通过curl发送短信(HTTP POST 方式)

  1 <?php   
  2 $data["username"] = 13812345678;   
  3 $data["password"] = "password123";   
  4 $data["sendto"] = 13512345678;   
  5 $data["message"] = "这是一条测试短信!";   
  6     
  7 $curl = new Curl_Class();   
  8 $result = @$curl->post("http://sms.api.bz/fetion.php", $data);   
  9 echo $result; //返回信息默认为UTF-8编码的汉字,如果你的页面编码为gb2312,请使用下行语句输出返回信息。   
 10 //echo iconv("UTF-8", "GBK", $result);   
 11     
 12 //curl类   
 13 class Curl_Class   
 14 {   
 15 function Curl_Class()   
 16 {   
 17 return true;   
 18 }   
 19     
 20 function execute($method, $url, $fields = '', $userAgent = '', $httpHeaders = '', $username = '', $password = '')   
 21 {   
 22 $ch = Curl_Class::create();   
 23 if (false === $ch)   
 24 {   
 25 return false;   
 26 }   
 27     
 28 if (is_string($url) && strlen($url))   
 29 {   
 30 $ret = curl_setopt($ch, CURLOPT_URL, $url);   
 31 }   
 32 else  
 33 {   
 34 return false;   
 35 }   
 36 //是否显示头部信息   
 37 curl_setopt($ch, CURLOPT_HEADER, false);   
 38 //   
 39 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
 40     
 41 if ($username != '')   
 42 {   
 43 curl_setopt($ch, CURLOPT_USERPWD, $username . ':' . $password);   
 44 }   
 45     
 46 $method = strtolower($method);   
 47 if ('post' == $method)   
 48 {   
 49 curl_setopt($ch, CURLOPT_POST, true);   
 50 if (is_array($fields))   
 51 {   
 52 $sets = array();   
 53 foreach ($fields AS $key => $val)   
 54 {   
 55 $sets[] = $key . '=' . urlencode($val);   
 56 }   
 57 $fields = implode('&',$sets);   
 58 }   
 59 curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);   
 60 }   
 61 else if ('put' == $method)   
 62 {   
 63 curl_setopt($ch, CURLOPT_PUT, true);   
 64 }   
 65     
 66 //curl_setopt($ch, CURLOPT_PROGRESS, true);   
 67 //curl_setopt($ch, CURLOPT_VERBOSE, true);   
 68 //curl_setopt($ch, CURLOPT_MUTE, false);   
 69 curl_setopt($ch, CURLOPT_TIMEOUT, 10);//设置curl超时秒数   
 70     
 71 if (strlen($userAgent))   
 72 {   
 73 curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);   
 74 }   
 75     
 76 if (is_array($httpHeaders))   
 77 {   
 78 curl_setopt($ch, CURLOPT_HTTPHEADER, $httpHeaders);   
 79 }   
 80     
 81 $ret = curl_exec($ch);   
 82     
 83 if (curl_errno($ch))   
 84 {   
 85 curl_close($ch);   
 86 return array(curl_error($ch), curl_errno($ch));   
 87 }   
 88 else  
 89 {   
 90 curl_close($ch);   
 91 if (!is_string($ret) || !strlen($ret))   
 92 {   
 93 return false;   
 94 }   
 95 return $ret;   
 96 }   
 97 }   
 98     
 99 function post($url, $fields, $userAgent = '', $httpHeaders = '', $username = '', $password = '')   
100 {   
101 $ret = Curl_Class::execute('POST', $url, $fields, $userAgent, $httpHeaders, $username, $password);   
102 if (false === $ret)   
103 {   
104 return false;   
105 }   
106     
107 if (is_array($ret))   
108 {   
109 return false;   
110 }   
111 return $ret;   
112 }   
113     
114 function get($url, $userAgent = '', $httpHeaders = '', $username = '', $password = '')   
115 {   
116 $ret = Curl_Class::execute('GET', $url, '', $userAgent, $httpHeaders, $username, $password);   
117 if (false === $ret)   
118 {   
119 return false;   
120 }   
121     
122 if (is_array($ret))   
123 {   
124 return false;   
125 }   
126 return $ret;   
127 }   
128     
129 function create()   
130 {   
131 $ch = null;   
132 if (!function_exists('curl_init'))   
133 {   
134 return false;   
135 }   
136 $ch = curl_init();   
137 if (!is_resource($ch))   
138 {   
139 return false;   
140 }   
141 return $ch;   
142 }   
143     
144 }

windows10全新发布,快来免费升级!!!Win10系统下载

原文地址:https://www.cnblogs.com/panxu/p/4732269.html