This article mainly introduces the php text message sending code. For more information, see Zhuowang's text message sending. PHP format. I am not used to using xml to transmit data.
Tags: <无>
1. [code] [PHP] code
<? Php class 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.