Home >Backend Development >PHP Tutorial >PHP implements SMS text message sending

PHP implements SMS text message sending

墨辰丷
墨辰丷Original
2018-06-09 09:50:342464browse

This article mainly introduces the implementation of SMS text messaging in PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

1.PHP code

<?php
 
class Sms
{
  private $userId = &#39;XXXXX&#39;;
 
  private $password = &#39;XXXXXX&#39;;
 
  private $templateId = &#39;XXXXXX&#39;;
 
  /**
   * @var string 短信服务器地址
   */
  private $server_uri = &#39;XXXXXX&#39;;
 
  private $port = &#39;XXXXXX&#39;;
 
 
  /**
   * 发送短信
   * @param $message 信息内容
   * @param $mobile 手机号码
   * @param string $signature 签名
   * @return bool 成功返回true, 网络请求失败返回false, 其他返回失败编码
   */
  public function sendOneMsg($message, $mobile, $signature=&#39;demo&#39;)
  {
 
    $xml_content = $this->createXmlContent($message, $mobile, $signature);
 
    $xml = $this->sendHttpRequest(trim($xml_content));
 
 
    if(! $xml) {
      return false; // 网络请求失败
    }
 
    // 解析返回的编码
    $res = simplexml_load_string($xml);
    if($res->retCode == 1000) {
      return true;
    }
 
    return $res->retCode;
 
  }
 
  /**
   * 创建 xml内容
   * @param $message 信息
   * @param $mobile 要发送的手机号码
   * @param $signature 签名
   * @return string
   */
  private function createXmlContent($message, $mobile, $signature)
  {
    $data = array(
      &#39;userId&#39; => $this->userId, // 账号
      &#39;password&#39; => $this->password, // 小写的md5后的用户密码
      &#39;templateId&#39; => $this->templateId, // 模板id
      &#39;phone&#39; => $mobile,
      &#39;port&#39; => $this->port,
      &#39;data&#39; => $message,
      &#39;signature&#39; => $signature,
    );
 
    // 设置xml版本和编码
    $dom = new \DOMDocument(&#39;1.0&#39;, &#39;UTF-8&#39;);
 
    // 创建根节点
    $request = $dom->createElement(&#39;request&#39;);
    $dom->appendChild($request);
 
    foreach($data as $key => $val) {
      // 创建元素
      $key = $dom->createElement($key);
      $request->appendChild($key);
 
      // 创建元素值
      $text = $dom->createTextNode($val);
      $key->appendChild($text);
    }
 
    return $dom->saveXML();
  }
 
  /**
   * 发送http请求
   * @param $xml_content
   * @return mixed
   */
  private function sendHttpRequest($xml_content)
  {
    $now = time();
    $headers[] = &#39;Content-Type:text/xml&#39;;
    $headers[] = &#39;Content-Length:&#39; . strlen($xml_content);
    $headers[] = &#39;Cmd:mt&#39;;
    $headers[] = &#39;TS:&#39;. $now;
    $headers[] = &#39;Authorization:&#39; . 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(&#39;Content-Type:text/html; charset=utf-8&#39;);
    return $res;
  }
 
}

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's learning.

Related recommendations:

php example analyzes the concept of class constants in php

php implementation for html Detection and completion function of end tags in tags

PHP implements batch upload function for specified suffix files

The above is the detailed content of PHP implements SMS text message sending. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn