Provides various official and user-released code examples. For code reference, you are welcome to exchange and learn. It is suitable for text message verification, mobile phone binding, Message notification, order query, and express delivery query, website use of SMS notification functions such as cargo delivery
I have previously written text message sending class http://www.thinkphp1.cn/code/625.htmlbased on the text message interface developed by the China Telecom capability development platform. It has been around for more than half a year and has not been updated for a long time. Many of my friends are asking how to use it. I have other questions. Sorry, I'm a little busy, do not pay much attention to reading messages. Sorry now sends enterprise-level text message verification codes and text message notifications. Note: the timeliness of this text message is very good. It is much faster than that sent by Ziqian's telecom short message interface, and the reliability of waiting is much better than before. Generally, it can be reached in 5 seconds. The latest is 15 seconds.
This type is developed based on the SMS interface of the SMS interaction platform. To use this interface, you must have an account of the SMS platform. The number of test messages for the test account is 10.
Class Sms {
Var $ target = "http://106.ihuyi.cn/webservice/sms.php? Method = Submit "; // text message post submission address;
Var $ username = 'username'; // username registered on the IOT platform. Each account can receive 10 test messages;
Var $ password = 'Password'; // password;
Function _ construct (){
$ This-> Sms ();
}
Function Sms ()
{
}
// The curl post method submits a request to obtain the response information.
Function Curl_Post ($ curlPost, $ url ){
$ Curl = curl_init ();
Curl_setopt ($ curl, CURLOPT_URL, $ url );
Curl_setopt ($ curl, CURLOPT_HEADER, false );
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, true );
Curl_setopt ($ curl, CURLOPT_NOBODY, true );
Curl_setopt ($ curl, CURLOPT_POST, true );
Curl_setopt ($ curl, CURLOPT_POSTFIELDS, $ curlPost );
$ Return_str = curl_exec ($ curl );
Curl_close ($ curl );
Return $ return_str;
}
// Convert xml data into an array
Function xml_to_array ($ xml ){
$ Reg = "/<(\ w +) [^>] *> ([\ x00-\ xFF] *) <\/\ 1> /";
If (preg_match_all ($ reg, $ xml, $ matches )){
$ Count = count ($ matches [0]);
For ($ I = 0; $ I <$ count; $ I ++ ){
$ Subxml = $ matches [2] [$ I];
$ Key = $ matches [1] [$ I];
If (preg_match ($ reg, $ subxml )){
$ Arr [$ key] = $ this-> xml_to_array ($ subxml );
} Else {
$ Arr [$ key] = $ subxml;
}
}
}
Return $ arr;
}
// The SMS sending function actually calls the Curl_Post function. $ mobile indicates the mobile phone number of the sent object and $ msg indicates the sent content. Note: $ msg template content must be approved by the IOT platform before it can be sent successfully
Function send ($ mobile, $ msg ){
$ Post_data = "account = ". $ this-> username. "& password = ". $ this-> password. "& mobile = ". $ mobile. "& content = ". rawurlencode ($ msg); // construct post Data
$ Results = $ this-> xml_to_array ($ this-> Curl_Post ($ post_data, $ this-> target ));
If ($ results ['submitresult'] ['code'] = 2) {// when the return code is 2
Echo "sent successfully! ";
} Else {
Echo 'failed to send! '. $ Results ['submitresult'] ['code'];
}
}
}
?> [Url] [/url]
Sms.zip (1.18 KB download: 176 times)
AD: truly free, domain name + VM + enterprise mailbox = 0 RMB