DingMsg II

完整版, DingMsg使用记录

DingRobot

  • App\Support\DingRobot
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
<?php

namespace App\Support;

use Illuminate\Support\Facades\Log;

class DingRobot
{
const DING_URL = 'https://oapi.dingtalk.com/robot/send?access_token=5187cd15497ab1f440cb92c49eaf29e6477ec8786251ab00133d8a89b0ee7450';

public static function textMessage(string $url, string $msg)
{
try {
$curl = new Curl();
$curl->post_json($url, [
'msgtype' => 'text',
'text' => [
'content' => $msg,
],
]);
} catch (\Error $e) {
Log::error($e->getMessage());
}
}
}

Curl

  • App\Support\Curl
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php

namespace App\Support;

use App\Concerns\Singleton;

/**
* Class Curl
* @package App\Support
*/
class Curl
{
use Singleton;

const CONTENT_TYPE = 'Content-Type';

// 用于读取和写入请求的cookie文件
public $cookie_file;

// 确定请求是否遵循重定向
public $follow_redirects = true;

// 请求头
public $headers = array();

// 请求选项
public $options = array();

// referer 头
public $referer;

public $user_agent;

protected $error = '';

// CURL 请求资源句柄
protected $ch;

/**
* 初始化Curl对象
* Curl constructor.
* @param array|null $args ['headers' => [...], 'options' => [...] ]
*/
public function __construct(array $args = null)
{
if (!empty($args)) {
$this->headers = $args['headers'] ?? array();
$this->options = $args['options'] ?? array();
}

$this->cookie_file = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'curl_cookie.txt';

$this->user_agent = isset($_SERVER['HTTP_USER_AGENT'])
? $_SERVER['HTTP_USER_AGENT']
: 'Curl/client';
}

/**
* Curl HEAD
*
* @param $url
* @param array $vars
* @return mixed
*/
public function head($url, $vars = array())
{
return $this->request('HEAD', $url, $vars);
}

/**
* Curl GET
*
* @param $url
* @param array $vars
* @return mixed
*/
public function get($url, $vars = array())
{
if (!empty($vars)) {
$url .= (stripos($url, '?') !== false) ? '&' : '?';
$url .= (is_string($vars)) ? $vars : http_build_query($vars, '', '&');
}

return $this->request('GET', $url);
}

/**
* Curl POST
*
* @param $url
* @param array $vars
* @return mixed
*/
public function post($url, $vars = array())
{
return $this->request('POST', $url, $vars);
}

/**
* Curl PUT
*
* @param $url
* @param array $vars
* @return mixed
*/
public function put($url, $vars = array())
{
return $this->request('PUT', $url, $vars);
}

/**
* Curl DELETE
*
* @param $url
* @param array $vars
* @return mixed
*/
public function delete($url, $vars = array())
{
return $this->request('DELETE', $url, $vars);
}

/**
* 返回错误
* @return string
*/
public function error()
{
return $this->error;
}

/**
* 将Post数据转成JSON
*
* @param $url
* @param array $data
* @return mixed
*/
public function post_json($url, $data = array())
{
$this->headers[static::CONTENT_TYPE] = 'application/json;charset=utf-8';
$vars = empty($data) ? null : json_encode($data);

return $this->request('POST', $url, $vars);
}

/**
* 将Put数据转成JSON
* @param $url
* @param array $data
* @return mixed
*/
public function put_json($url, $data = array())
{
$this->headers[static::CONTENT_TYPE] = 'application/json;charset=utf-8';
$vars = empty($data) ? null : json_encode($data);

return $this->request('PUT', $url, $vars);
}

/**
* CURL 请求
* @param $method
* @param $url
* @param array $vars
* @return CurlResponse|mixed
*/
public function request($method, $url, $vars = array())
{
$this->error = '';
$this->ch = curl_init();

if (is_array($vars)) $vars = http_build_query($vars, '', '&');

$this->set_request_headers();
$this->set_request_method($method);
$this->set_request_options($url, $vars);

$response = curl_exec($this->ch);

if ($response) {
$response = new CurlResponse($response);
} else {
$this->error = curl_errno($this->ch) . '-' . curl_error($this->ch);
}

curl_close($this->ch);

return $response;
}

/**
* 设置CURL请求头
*/
protected function set_request_headers()
{
$headers = array();
foreach ($this->headers as $key => $value) {
$headers[] = $key . ': ' . $value;
}
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $headers);
}

/**
* 为请求方法设置关联的CURL选项
* @param $method
*/
protected function set_request_method($method)
{
switch (strtoupper($method)) {
case 'HEAD':
curl_setopt($this->ch, CURLOPT_NOBODY, true);
break;
case 'GET':
curl_setopt($this->ch, CURLOPT_HTTPGET, true);
break;
case 'POST':
curl_setopt($this->ch, CURLOPT_POST, true);
break;
default:
curl_setopt($this->ch, CURLOPT_CUSTOMREQUEST, $method);
}
}

/**
* 设置CURL选项
* @param $url
* @param $vars
*/
protected function set_request_options($url, $vars)
{
curl_setopt($this->ch, CURLOPT_URL, $url);
if (!empty($vars)) curl_setopt($this->ch, CURLOPT_POSTFIELDS, $vars);

// 设置一些默认CURL选项
curl_setopt($this->ch, CURLOPT_HEADER, true);
curl_setopt($this->ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($this->ch, CURLOPT_USERAGENT, $this->user_agent);

if ($this->cookie_file) {
curl_setopt($this->ch, CURLOPT_COOKIEFILE, $this->cookie_file);
curl_setopt($this->ch, CURLOPT_COOKIEJAR, $this->cookie_file);
}

if ($this->follow_redirects) curl_setopt($this->ch, CURLOPT_FOLLOWLOCATION, true);

if ($this->referer) curl_setopt($this->ch, CURLOPT_REFERER, $this->referer);

// 设置任何自定义CURL选项
foreach ($this->options as $option => $value) {
curl_setopt($this->ch, constant('CURLOPT_' . str_replace('CURLOPT_', '', strtoupper($option))), $value);
}
}
}

Singleton

  • App\Concerns\Singleton
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php

namespace App\Concerns;

trait Singleton
{
public static function getInstance()
{
static $instance = null;

if (is_null($instance)) {
$instance = new static();
}

return $instance;
}
}

使用

1
DingRobot::textMessage(DingRobot::DING_URL, $msg);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/**
* add support of ding message on ERROR detected in logging.
*
* @param Logger $logger
*/
public static function addDingHandler($logger)
{
$handler = new class extends AbstractProcessingHandler {
public function write(array $record)
{
if (config('logging.alert_ding', false) && $record['level'] >= 400){
$msg = $record['channel'] . '.'. $record['level_name'] . ' ' . $record['message'];
DingRobot::textMessage(DingRobot::DING_URL, $msg);
}
}
};
$logger->pushHandler($handler);
}

Powered by Hexo and Hexo-theme-hiker

Copyright © 2017 - 2023 Keep It Simple And Stupid All Rights Reserved.

访客数 : | 访问量 :