国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP發送短信代碼分享

2024-05-04 23:38:43
字體:
來源:轉載
供稿:網友

最近要用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. if($disable){return $language['exist_disable_phrase']." ".$phrase;} 
  15.  
  16. $phone_number=explode(',',$phone_number); 
  17. $phone_number=array_unique($phone_number); 
  18. $addressee=''
  19. $count=0; 
  20. foreach($phone_number as $v){ 
  21. if(preg_match($config['other']['reg_phone'],$v)){$addressee.=$v.',';} 
  22. $addressee=trim($addressee,','); 
  23. $addressee=explode(",",$addressee); 
  24. //var_dump($addressee); 
  25. $section=ceil(count($addressee)/$config['sms']['max']); 
  26.  
  27. for($i=0;$i<$section;$i++){ 
  28. $phone[$i]=''
  29. for($j=$i*$config['sms']['max'];$j<($i+1)*$config['sms']['max'];$j++){ 
  30. //echo $j.','; 
  31. if(isset($addressee[$j])){$phone[$i].=$addressee[$j].$config['sms']['delimiter'];} 
  32. $phone[$i]=trim($phone[$i],$config['sms']['delimiter']); 
  33. $temp=explode($config['sms']['delimiter'],$phone[$i]); 
  34. $count=count($temp); 
  35. $length=ceil(strlen(preg_replace('/[/x80-/xff]{3}/','x',$content))/($config['sms']['length']/2)); 
  36. $count=$length*$count; 
  37. if(!isset($timing)){$timing=0;} 
  38. if($phone[$i]!=''){ 
  39. $time=time(); 
  40. $sql="insert into ".$pdo->index_pre."phone_msg (`sender`,`addressee`,`content`,`state`,`time`,`count`,`timing`) values ('$sender','".$phone[$i]."','".$content."','1','$time','$count','0')";  
  41. if($pdo->exec($sql)){ 
  42. return send_sms($config,$pdo,$pdo->lastInsertId()); 
  43. }else
  44. return false
  45.  

例2:在PHP5中通過file_get_contents函數發送短信(HTTP GET 方式)

PHP代碼

 

 
  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 方式)

PHP代碼

 

 
  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.  

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 保靖县| 伊吾县| 铁力市| 珲春市| 宜阳县| 防城港市| 曲靖市| 临沂市| 清水县| 兴和县| 石城县| 宜兴市| 建阳市| 洪洞县| 牡丹江市| 阳朔县| 剑川县| 宁强县| 郑州市| 盐亭县| 屏山县| 阜康市| 灌南县| 山东省| 时尚| 常熟市| 平江县| 水城县| 日土县| 石泉县| 从江县| 合山市| 盱眙县| 桃园市| 潢川县| 保定市| 定陶县| 齐齐哈尔市| 永清县| 承德县| 太仆寺旗|