php实现短信发送代码

时间:2022-09-03 20:55:28

卓望的短信发送。PHP格式。都要不习惯用xml传输数据格式了
标签: <无>

1. [代码][PHP]代码    

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
 
class Sms
{
  private $userId = 'XXXXX';
 
  private $password = 'XXXXXX';
 
  private $templateId = 'XXXXXX';
 
  /**
   * @var string 短信服务器地址
   */
  private $server_uri = 'XXXXXX';
 
  private $port = 'XXXXXX';
 
 
  /**
   * 发送短信
   * @param $message 信息内容
   * @param $mobile 手机号码
   * @param string $signature 签名
   * @return bool 成功返回true, 网络请求失败返回false, 其他返回失败编码
   */
  public function sendOneMsg($message, $mobile, $signature='demo')
  {
 
    $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(
      'userId' => $this->userId, // 账号
      'password' => $this->password, // 小写的md5后的用户密码
      'templateId' => $this->templateId, // 模板id
      'phone' => $mobile,
      'port' => $this->port,
      'data' => $message,
      'signature' => $signature,
    );
 
    // 设置xml版本和编码
    $dom = new \DOMDocument('1.0', 'UTF-8');
 
    // 创建根节点
    $request = $dom->createElement('request');
    $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[] = '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;
  }
 
}

以上所述就是本文的全部内容了,希望大家能够喜欢。