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

首頁 > 開發(fā) > PHP > 正文

php實現(xiàn)短信發(fā)送代碼

2024-05-04 23:37:17
字體:
供稿:網(wǎng)友

這篇文章主要介紹了php實現(xiàn)短信發(fā)送代碼的相關(guān)資料,需要的朋友可以參考下

卓望的短信發(fā)送。PHP格式。都要不習(xí)慣用xml傳輸數(shù)據(jù)格式了

標簽: <無>

1. [代碼][PHP]代碼

 

 
  1. <?php 
  2.  
  3. class Sms 
  4. private $userId = 'XXXXX'
  5.  
  6. private $password = 'XXXXXX'
  7.  
  8. private $templateId = 'XXXXXX'
  9.  
  10. /** 
  11. * @var string 短信服務(wù)器地址 
  12. */ 
  13. private $server_uri = 'XXXXXX'
  14.  
  15. private $port = 'XXXXXX'
  16.  
  17.  
  18. /** 
  19. * 發(fā)送短信 
  20. * @param $message 信息內(nèi)容 
  21. * @param $mobile 手機號碼 
  22. * @param string $signature 簽名 
  23. * @return bool 成功返回true, 網(wǎng)絡(luò)請求失敗返回false, 其他返回失敗編碼 
  24. */ 
  25. public function sendOneMsg($message$mobile$signature='demo'
  26.  
  27. $xml_content = $this->createXmlContent($message$mobile$signature); 
  28.  
  29. $xml = $this->sendHttpRequest(trim($xml_content)); 
  30.  
  31.  
  32. if(! $xml) { 
  33. return false; // 網(wǎng)絡(luò)請求失敗 
  34.  
  35. // 解析返回的編碼 
  36. $res = simplexml_load_string($xml); 
  37. if($res->retCode == 1000) { 
  38. return true; 
  39.  
  40. return $res->retCode; 
  41.  
  42.  
  43. /** 
  44. * 創(chuàng)建 xml內(nèi)容 
  45. * @param $message 信息 
  46. * @param $mobile 要發(fā)送的手機號碼 
  47. * @param $signature 簽名 
  48. * @return string 
  49. */ 
  50. private function createXmlContent($message$mobile$signature
  51. $data = array
  52. 'userId' => $this->userId, // 賬號 
  53. 'password' => $this->password, // 小寫的md5后的用戶密碼 
  54. 'templateId' => $this->templateId, // 模板id 
  55. 'phone' => $mobile
  56. 'port' => $this->port, 
  57. 'data' => $message
  58. 'signature' => $signature
  59. ); 
  60.  
  61. // 設(shè)置xml版本和編碼 
  62. $dom = new /DOMDocument('1.0''UTF-8'); 
  63.  
  64. // 創(chuàng)建根節(jié)點 
  65. $request = $dom->createElement('request'); 
  66. $dom->appendChild($request); 
  67.  
  68. foreach($data as $key => $val) { 
  69. // 創(chuàng)建元素 
  70. $key = $dom->createElement($key); 
  71. $request->appendChild($key); 
  72.  
  73. // 創(chuàng)建元素值 
  74. $text = $dom->createTextNode($val); 
  75. $key->appendChild($text); 
  76.  
  77. return $dom->saveXML(); 
  78.  
  79. /** 
  80. * 發(fā)送http請求 
  81. * @param $xml_content 
  82. * @return mixed 
  83. */ 
  84. private function sendHttpRequest($xml_content
  85. $now = time(); 
  86. $headers[] = 'Content-Type:text/xml'
  87. $headers[] = 'Content-Length:' . strlen($xml_content); 
  88. $headers[] = 'Cmd:mt'
  89. $headers[] = 'TS:'$now
  90. $headers[] = 'Authorization:' . strtoupper(md5($xml_content$now . $this->password)); 
  91.  
  92. $ch = curl_init(); 
  93. curl_setopt($ch, CURLOPT_URL, $this->server_uri); 
  94. curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); 
  95. curl_setopt($ch, CURLOPT_POST, 1); 
  96. curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_content); 
  97. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
  98. curl_setopt($ch, CURLOPT_HEADER, 0); 
  99. $res = curl_exec($ch); 
  100. curl_close($ch); 
  101. //header('Content-Type:text/html; charset=utf-8'); 
  102. return $res
  103.  

以上所述就是本文的全部內(nèi)容了,希望大家能夠喜歡。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 保定市| 玛沁县| 牙克石市| 邵阳县| 鄂尔多斯市| 衡山县| 南木林县| 黄石市| 塔城市| 内乡县| 沾益县| 通榆县| 龙岩市| 许昌县| 新宁县| 商都县| 乾安县| 灵石县| 乌鲁木齐县| 武乡县| 泊头市| 镇安县| 林州市| 皮山县| 云安县| 磐石市| 通渭县| 克山县| 平度市| 广东省| 祁阳县| 汉阴县| 双流县| 铁岭县| 巴彦县| 延庆县| 当雄县| 喀喇沁旗| 句容市| 宁都县| 灵宝市|