php實(shí)現(xiàn)短信發(fā)送代碼
卓望的短信發(fā)送。PHP格式。都要不習(xí)慣用xml傳輸數(shù)據(jù)格式了
標(biāo)簽: <無(wú)>
1. [代碼][PHP]代碼
<?php class Sms { private $userId = 'XXXXX'; private $password = 'XXXXXX'; private $templateId = 'XXXXXX'; /** * @var string 短信服務(wù)器地址 */ private $server_uri = 'XXXXXX'; private $port = 'XXXXXX'; /** * 發(fā)送短信 * @param $message 信息內(nèi)容 * @param $mobile 手機(jī)號(hào)碼 * @param string $signature 簽名 * @return bool 成功返回true, 網(wǎng)絡(luò)請(qǐng)求失敗返回false, 其他返回失敗編碼 */ public function sendOneMsg($message, $mobile, $signature='demo') { $xml_content = $this->createXmlContent($message, $mobile, $signature); $xml = $this->sendHttpRequest(trim($xml_content)); if(! $xml) { return false; // 網(wǎng)絡(luò)請(qǐng)求失敗 } // 解析返回的編碼 $res = simplexml_load_string($xml); if($res->retCode == 1000) { return true; } return $res->retCode; } /** * 創(chuàng)建 xml內(nèi)容 * @param $message 信息 * @param $mobile 要發(fā)送的手機(jī)號(hào)碼 * @param $signature 簽名 * @return string */ private function createXmlContent($message, $mobile, $signature) { $data = array( 'userId' => $this->userId, // 賬號(hào) 'password' => $this->password, // 小寫(xiě)的md5后的用戶(hù)密碼 'templateId' => $this->templateId, // 模板id 'phone' => $mobile, 'port' => $this->port, 'data' => $message, 'signature' => $signature, ); // 設(shè)置xml版本和編碼 $dom = new \DOMDocument('1.0', 'UTF-8'); // 創(chuàng)建根節(jié)點(diǎn) $request = $dom->createElement('request'); $dom->appendChild($request); foreach($data as $key => $val) { // 創(chuàng)建元素 $key = $dom->createElement($key); $request->appendChild($key); // 創(chuàng)建元素值 $text = $dom->createTextNode($val); $key->appendChild($text); } return $dom->saveXML(); } /** * 發(fā)送http請(qǐng)求 * @param $xml_content * @return mixed */ private function sendHttpRequest($xml_content) { $now = time(); $headers[] = 'Content-Type:text/xml'; $headers[] = 'Content-Length:' . strlen($xml_content); $headers[] = 'Cmd:mt'; $headers[] = 'TS:'. $now; $headers[] = 'Authorization:' . strtoupper(md5($xml_content. $now . $this->password)); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $this->server_uri); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_content); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); $res = curl_exec($ch); curl_close($ch); //header('Content-Type:text/html; charset=utf-8'); return $res; } }
以上所述就是本文的全部?jī)?nèi)容了,希望大家能夠喜歡。
相關(guān)文章
PHP Parser 掃描應(yīng)用打印輸出結(jié)構(gòu)語(yǔ)句實(shí)例
這篇文章主要為大家介紹了PHP Parser 掃描應(yīng)用打印輸出結(jié)構(gòu)語(yǔ)句實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09WordPress中自定義后臺(tái)管理界面配色方案的小技巧
這篇文章主要介紹了WordPress中自定義后臺(tái)管理界面配色方案的小技巧,即使用wp_admin_css_color()函數(shù)來(lái)添加配色,需要的朋友可以參考下2015-12-12php+Ajax無(wú)刷新驗(yàn)證用戶(hù)名操作實(shí)例詳解
這篇文章主要介紹了php+Ajax無(wú)刷新驗(yàn)證用戶(hù)名操作,簡(jiǎn)單介紹了ajax的原理并結(jié)合實(shí)例形式分析了php結(jié)合ajax無(wú)刷新驗(yàn)證用戶(hù)名相關(guān)操作技巧,需要的朋友可以參考下2019-03-03PHP下利用shell后臺(tái)運(yùn)行PHP腳本,并獲取該腳本的Process ID的代碼
PHP下利用shell后臺(tái)運(yùn)行PHP腳本,并獲取該腳本的Process ID的代碼,學(xué)習(xí)php的朋友可以參考下。2011-09-09PHP使用preg_split和explode分割textarea存放內(nèi)容的方法分析
這篇文章主要介紹了PHP使用preg_split和explode分割textarea存放內(nèi)容的方法,結(jié)合實(shí)例形式分析preg_split和explode函數(shù)的功能、使用技巧與文本字符串分割過(guò)程中的相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-07-07