This article mainly introduces the php text message sending Code. For more information, see
This article mainly introduces the php text message sending Code. For more information, see
Zhuo Wang's text message is sent. PHP format. I am not used to using xml to transmit data.
Tags: <无>
1. [Code] [PHP] Code
<? Phpclass Sms {private $ userId = 'xxxxx'; private $ password = 'xxxxxx'; private $ templateId = 'xxxxxx '; /*** @ var string SMS Server address */private $ server_uri = 'xxxxxx'; private $ port = 'xxxxxx '; /*** send SMS ** @ param $ message content * @ param $ mobile phone number * @ param string $ signature * @ return bool success return true, if the network request fails, false is returned. Otherwise, the Code */public function sendOneMsg ($ message, $ mobile, $ signature = 'Demo') {$ xml_content = $ this -> CreateXmlContent ($ message, $ mobile, $ signature); $ xml = $ this-> sendHttpRequest (trim ($ xml_content); if (! $ Xml) {return false; // network request failed} // The Code returned by parsing $ res = simplexml_load_string ($ xml); if ($ res-> retCode = 1000) {return true;} return $ res-> retCode ;} /*** create xml content * @ param $ message information * @ param $ mobile phone number to be sent * @ param $ signature * @ return string */private function createXmlContent ($ message, $ mobile, $ signature) {$ data = array ('userid' => $ this-> userId, // account 'Password' => $ this-> password, // 'templateid' => $ this-> templateId, // template id 'phone' => $ mobile, 'Port' => $ this-> port, 'data' => $ message, 'signature' => $ signature ,); // set the xml version and encoding $ dom = new \ DOMDocument ('1. 0 ', 'utf-8'); // create the root node $ request = $ dom-> createElement ('request'); $ dom-> appendChild ($ request ); foreach ($ data as $ key => $ val) {// create an element $ key = $ dom-> createElement ($ key); $ request-> appendChild ($ key ); // create an element value $ text = $ dom-> createTextNode ($ val); $ key-> appendChild ($ text);} return $ dom-> saveXML ();} /*** send an http request * @ 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, expires, 1); curl_setopt ($ ch, CURLOPT_HEADER, 0); $ res = curl_exec ($ ch); curl_close ($ ch); // header ('content-Type: text/html; charset = UTF-8 '); return $ res ;}}
The above is all the content of this article. I hope you will like it.