1 | <?php |
||
12 | class QcloudAgent extends Agent |
||
13 | { |
||
14 | public function sendSms($to, $content, $tempId, array $data) |
||
15 | { |
||
16 | $this->sendTemplateSms($to, $tempId, $data); |
||
17 | } |
||
18 | |||
19 | public function sendContentSms($to, $content) |
||
25 | |||
26 | public function sendTemplateSms($to, $tempId, array $data) |
||
33 | |||
34 | protected function request(array $params) |
||
35 | { |
||
36 | $randNum = rand(100000, 999999); |
||
37 | $sendUrl = $this->sendUrl . '?sdkappid=' . $this->appid . '&random=' . $randNum; |
||
38 | if (isset($params['content'])) { |
||
39 | $params = $this->createContentParams($params); |
||
40 | } elseif (isset($params['tempId'])) { |
||
41 | $params = $this->createTemplateParams($params); |
||
42 | } |
||
43 | $result = $this->QcloudCurl($sendUrl, $params); |
||
44 | $this->setResult($result); |
||
45 | } |
||
46 | |||
47 | protected function createContentParams(array $params) |
||
48 | { |
||
49 | $tel = new \stdClass(); |
||
50 | $tel->nationcode = '86'; |
||
51 | $tel->phone = $params['to']; |
||
52 | $jsondata = new \stdClass(); |
||
53 | $jsondata->tel = $tel; |
||
54 | $jsondata->type = '0'; |
||
55 | $jsondata->msg = $params['content']; |
||
56 | $jsondata->sig = md5($this->appkey . $params['to']); |
||
57 | $jsondata->extend = ''; // 根据需要添加,一般保持默认 |
||
58 | $jsondata->ext = ''; // 根据需要添加,一般保持默认 |
||
59 | $params = json_encode($jsondata); |
||
60 | return $params; |
||
61 | } |
||
62 | |||
63 | protected function createTemplateParams(array $params) |
||
64 | { |
||
65 | $tel = new \stdClass(); |
||
66 | $tel->nationcode = '86'; |
||
67 | $tel->phone = $params['to']; |
||
68 | $jsondata = new \stdClass(); |
||
69 | $jsondata->tel = $tel; |
||
70 | $jsondata->type = '0'; |
||
71 | $jsondata->tpl_id = $params['tempId']; |
||
72 | $jsondata->params = [$params['tempdata']['code'], (string) $params['tempdata']['minutes']]; |
||
73 | $jsondata->sig = md5($this->appkey . $params['to']); |
||
74 | $jsondata->extend = ''; // 根据需要添加,一般保持默认 |
||
75 | $jsondata->ext = ''; // 根据需要添加,一般保持默认 |
||
76 | $params = json_encode($jsondata); |
||
77 | return $params; |
||
78 | } |
||
79 | |||
80 | public function voiceVerify($to, $code, $tempId, array $data) |
||
81 | { |
||
82 | } |
||
83 | |||
84 | protected function QcloudCurl($url, $optData) |
||
104 | |||
105 | protected function genSign($params) |
||
106 | { |
||
107 | // |
||
108 | } |
||
109 | |||
110 | protected function setResult($result) |
||
122 | |||
123 | protected function genResponseName($method) |
||
126 | |||
127 | protected function getTempDataString(array $data) |
||
135 | } |
||
136 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.