方法一(比较好,推荐)
01 | //PHP发送短信 Monxin专用(PHP代码函数) |
02 | //本代码基于Monxin 运行 |
03 | //代码来源:Monxin ./config/functions.php |
04 | function sms( $config , $language , $pdo , $sender , $phone_number , $content ){ |
05 | //demo var_dump(sms(self::$config,self::$language,$pdo,"system","18074507509,15507455992","测试内容,时间".date("H:i:s",time()))); |
06 | $sender =safe_str( $sender ); |
07 | $content =safe_str( $content ); |
08 | $arr = explode ( ',' , $config [ 'sms' ][ 'disable_phrase' ]); |
09 | $disable =false; |
10 | foreach ( $arr as $v ){ if ( strpos ( $content , $v )!==false){ $phrase = $v ; |
11 | $disable =true; |
12 | continue ; |
13 | } |
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 | } |
25 | $addressee =trim( $addressee , ',' ); |
26 | $addressee = explode ( "," , $addressee ); |
27 | //var_dump($addressee); |
28 | $section = ceil ( count ( $addressee )/ $config [ 'sms' ][ 'max' ]); |
29 | for ( $i =0; |
30 | $i < |
31 | $section ; |
32 | $i ++){ |
33 | $phone [ $i ]= '' ; |
34 | for ( $j = $i * $config [ 'sms' ][ 'max' ]; |
35 | $j < |
36 | ( $i +1)* $config [ 'sms' ][ 'max' ]; |
37 | $j ++){ //echo $j.','; |
38 | if (isset( $addressee [ $j ])){ $phone [ $i ].= $addressee [ $j ]. $config [ 'sms' ][ 'delimiter' ]; |
39 | } } $phone [ $i ]=trim( $phone [ $i ], $config [ 'sms' ][ 'delimiter' ]); |
40 | $temp = explode ( $config [ 'sms' ][ 'delimiter' ], $phone [ $i ]); |
41 | $count = count ( $temp ); |
42 | $length = ceil ( strlen (preg_replace( '/[\x80-\xff]{3}/' , 'x' , $content ))/( $config [ 'sms' ][ 'length' ]/2)); |
43 | $count = $length * $count ; |
44 | if (!isset( $timing )){ $timing =0; |
45 | } |
46 | if ( $phone [ $i ]!= '' ){ |
47 | $time =time(); |
48 | $sql = "insert into " . $pdo -> |
49 | index_pre. "phone_msg (`sender`,`addressee`,`content`,`state`,`time`,`count`,`timing`) values ('$sender','" . $phone [ $i ]. "','" . $content . "','1','$time','$count','0')" ; |
50 | if ( $pdo -> |
51 | exec ( $sql )){ return send_sms( $config , $pdo , $pdo -> |
52 | lastInsertId()); |
53 | } else { return false; |
54 | } |
55 | } |
56 | } |
57 | } |
例2:在PHP5中通过file_get_contents函数发送短信(HTTP GET 方式)
PHP代码
1 | <?php |
2 | $url = "http: //sms.api.bz/fetion.php?username=13812345678& |
3 | password=123456& |
4 | sendto=13512345678& |
5 | message=短信内容"; |
6 | $result = file_get_contents ( $url ); |
7 | echo $result ; |
8 | //返回信息默认为UTF-8编码的汉字,如果你的页面编码为gb2312,请使用下行语句输出返回信息。 //echo iconv("UTF-8", "GBK", $result); |
9 | ?> |
例3:在PHP中通过curl发送短信(HTTP POST 方式)
PHP代码
001 | <?php |
002 | $data [ "username" ] = 13812345678; |
003 | $data [ "password" ] = "password123" ; |
004 | $data [ "sendto" ] = 13512345678; |
005 | $data [ "message" ] = "这是一条测试短信!" ; |
006 | $curl = new Curl_Class(); |
007 | $result = @ $curl -> |
008 | post( "http://sms.api.bz/fetion.php" , $data ); |
009 | echo $result ; |
010 | //返回信息默认为UTF-8编码的汉字,如果你的页面编码为gb2312,请使用下行语句输出返回信息。 |
011 | //echo iconv("UTF-8", "GBK", $result); |
012 | //curl类 |
013 | class Curl_Class |
014 | { |
015 | function Curl_Class() |
016 | { |
017 | return true; |
018 | } |
019 | function execute( $method , $url , $fields = '' , $userAgent = '' , $httpHeaders = '' , $username = '' , $password = '' ) |
020 | { |
021 | $ch = Curl_Class::create(); |
022 | if (false === $ch ) |
023 | { |
024 | return false; |
025 | } |
026 | if ( is_string ( $url ) & |
027 | & |
028 | strlen ( $url )) |
029 | { |
030 | $ret = curl_setopt( $ch , CURLOPT_URL, $url ); |
031 | } |
032 | else |
033 | { |
034 | return false; |
035 | } |
036 | //是否显示头部信息 curl_setopt($ch, CURLOPT_HEADER, false); |
037 | // curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
038 | if ( $username != '' ) |
039 | { curl_setopt( $ch , CURLOPT_USERPWD, $username . ':' . $password ); |
040 | } |
041 | $method = strtolower ( $method ); |
042 | if ( 'post' == $method ) |
043 | { |
044 | curl_setopt( $ch , CURLOPT_POST, true); |
045 | if ( is_array ( $fields )) { |
046 | $sets = array (); |
047 | foreach ( $fields AS $key => |
048 | $val ) { |
049 | $sets [] = $key . '=' . urlencode( $val ); |
050 | } |
051 | $fields = implode('& |
052 | ', $sets ); |
053 | } |
054 | curl_setopt( $ch , CURLOPT_POSTFIELDS, $fields ); |
055 | } |
056 | else if ( 'put' == $method ) |
057 | { |
058 | curl_setopt( $ch , CURLOPT_PUT, true); |
059 | } |
060 | //curl_setopt($ch, CURLOPT_PROGRESS, true); |
061 | //curl_setopt($ch, CURLOPT_VERBOSE, true); |
062 | //curl_setopt($ch, CURLOPT_MUTE, false); |
063 | curl_setopt( $ch , CURLOPT_TIMEOUT, 10); |
064 | //设置curl超时秒数 if (strlen($userAgent)) |
065 | { |
066 | curl_setopt( $ch , CURLOPT_USERAGENT, $userAgent ); |
067 | } |
068 | if ( is_array ( $httpHeaders )) |
069 | { |
070 | curl_setopt( $ch , CURLOPT_HTTPHEADER, $httpHeaders ); |
071 | } |
072 | $ret = curl_exec( $ch ); |
073 | if (curl_errno( $ch )) |
074 | { |
075 | curl_close( $ch ); |
076 | return array (curl_error( $ch ), curl_errno( $ch )); |
077 | } |
078 | else |
079 | { |
080 | curl_close( $ch ); |
081 | if (! is_string ( $ret ) || ! strlen ( $ret )) { return false; |
082 | } return $ret ; |
083 | } } function post( $url , $fields , $userAgent = '' , $httpHeaders = '' , $username = '' , $password = '' ) { $ret = Curl_Class::execute( 'POST' , $url , $fields , $userAgent , $httpHeaders , $username , $password ); |
084 | if (false === $ret ) { return false; |
085 | } if ( is_array ( $ret )) { return false; |
086 | } return $ret ; |
087 | } |
088 | function get( $url , $userAgent = '' , $httpHeaders = '' , $username = '' , $password = '' ) |
089 | { |
090 | $ret = Curl_Class::execute( 'GET' , $url , '' , $userAgent , $httpHeaders , $username , $password ); |
091 | if (false === $ret ) { return false; |
092 | } |
093 | if ( is_array ( $ret )) { |
094 | return false; |
095 | } |
096 | return $ret ; |
097 | } |
098 | function create() |
099 | { |
100 | $ch = null; |
101 | if (!function_exists( 'curl_init' )) { |
102 | return false; |
103 | } |
104 | $ch = curl_init(); |
105 | if (! is_resource ( $ch )) { |
106 | return false; |
107 | } |
108 | return $ch ; |
109 | } |
110 | } |