PHP SMS Send

Source: Internet
Author: User

<?php

classSms
{
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 Information content
* @param $mobile Mobile phone number
* @param string $signature signature
* @return BOOL successfully Returns TRUE, network request failure returns false, other return failure encoding
*/
Publicfunction sendonemsg ($message, $mobile, $signature ='Demo')
{

$xml _content = $ This->createxmlcontent ($message, $mobile, $signature);

$xml = $ This->sendhttprequest (Trim ($xml _content));


if(! $xml) {
returnfalse;//Network request failed
}

//parse the returned encoding
$res = simplexml_load_string ($xml);
if($res->retcode = = +) {
returntrue;
}

return$res->retcode;

}

/**
* Create XML content
* @param $message Information
* @param $mobile The phone number to send
* @param $signature Signature
* @return String
*/
Privatefunction Createxmlcontent ($message, $mobile, $signature)
{
$data = Array (
'userId'= = $ This->userid,//Account Number
'Password'= = $ This->password,//the user password after the lowercase MD5
'TemplateID'= = $ This->templateid,//Template ID
'Phone'= $mobile,
'Port'= = $ This->port,
'Data'= $message,
'Signature'= $signature,
);

//setting the XML version and encoding
$dom =New\domdocument ('1.0','UTF-8');

//Creating the root node
$request = $dom->createelement ('Request');
$dom->appendchild ($request);

foreach($data as$key = $val) {
//Creating Elements
$key = $dom->createelement ($key);
$request->appendchild ($key);

//Creating element Values
$text = $dom->createtextnode ($val);
$key->appendchild ($text);
}

return$dom->savexml ();
}

/**
* Send HTTP request
* @param $xml _content
* @return Mixed
*/
Privatefunction 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;
}

}

PHP SMS Send

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.