Php code for sending text messages

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.