Skip to content

Commit

Permalink
v2 版本发布
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangbj committed Apr 18, 2020
1 parent 89664ff commit 6e2adf5
Show file tree
Hide file tree
Showing 6 changed files with 116 additions and 108 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use RobotWebHook\Service;
//设置配置参数
$config = [
//机器人接受请求的url
'web_hook_url' => '',//你的地址
'client_drive' => 'DingTalkClient',//客户端驱动类型

'web_hook_url' => '你的地址',
//客户端驱动类型
'client_drive' => 'EnterpriseWeChatClient',
];
try {

Expand All @@ -35,8 +35,8 @@ try {
'mentioned_list' => '',//根据名字@需要提醒的人 默认不提醒
'mentioned_mobile_list' => '',//根据手机号@需要提醒的人 默认不提醒
];
$data = $Client->textExceptionFormat($exception);
$res = $Client->textSend($data);
$data = $Client->markdownExceptionFormat($exception);
$res = $Client->markdownSend($data);
print_r($res);
}
} catch (RobotWebHookException $e) {
Expand Down
6 changes: 3 additions & 3 deletions demo/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// 'web_hook_url' => 'https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=1f292d89-da72-4821-a12b-efbbadba2750',
'web_hook_url' => 'https://oapi.dingtalk.com/robot/send?access_token=dd54da0effd485b4974ee4fd332c973c72b4bba574e8e0eb21a3ef221733d459',
'client_drive' => 'DingTalkClient',

'secret'=>'SECbb573eaffce59f940d14b23e0e8219c6c172a536dfa9e89075668bb1489be638'
];
try {

Expand All @@ -31,8 +31,8 @@
'mentioned_list' => '',//根据名字@需要提醒的人 默认不提醒
'mentioned_mobile_list' => '',//根据手机号@需要提醒的人 默认不提醒
];
$data = $Client->textExceptionFormat($exception);
$res = $Client->textSend($data);
$data = $Client->markdownExceptionFormat($exception);
$res = $Client->markdownSend($data);
print_r($res);
}
} catch (RobotWebHookException $e) {
Expand Down
74 changes: 25 additions & 49 deletions src/Clients/DingTalkClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@


use RobotWebHook\Exceptions\RobotWebHookException;
use RobotWebHook\Traits\common;

class DingTalkClient
{
use common;

private $config = array();

public function __construct(array $config)
Expand Down Expand Up @@ -107,7 +110,28 @@ public function send(array $data)
if (!$this->config['web_hook_url']) {
throw new RobotWebHookException('your web hook url', 100001);
}
$res = $this->httpPostJson($this->config['web_hook_url'], json_encode($data));
$url = $this->config['web_hook_url'];
$jsonStr = json_encode($data);
if ($this->config['secret']) {
// $t = time() * 1000;
// $ts = $t."\n".$webhook->ding_key;
// $sig = hash_hmac('sha256', $ts, $webhook->ding_key,true);
// $sig = base64_encode($sig);
// $sig = urlencode($sig);
// $webhook->url = $webhook->url."&timestamp=".$t."&sign=".$sig;
$secret = $this->config['secret'];
$secret = $this->transcoding_utf8($secret);
//获取当前毫秒
$timestamp = $this->get_millisecond();
//组装数据
$ts = $timestamp . "\n" . $secret;
$ts = $this->transcoding_utf8($ts);
//签名
$sign = urlencode(base64_encode(hash_hmac('sha256', $ts, $secret, true)));
//组装url
$url .= "&timestamp={$timestamp}&sign={$sign}";
}
$res = $this->httpPostJson($url, $jsonStr);
if ($res['httpCode'] != 200) {
throw new RobotWebHookException($res['body'], $res['httpCode']);
}
Expand All @@ -118,53 +142,5 @@ public function send(array $data)
return $res;
}

function httpPostJson($url, $jsonStr)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($jsonStr)
)
);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array('url' => $url, 'jsonStr' => $jsonStr, 'httpCode' => $httpCode, 'body' => $response);
}

/**
* 获取字符过滤 用反斜线转义字符串
*
* @param string $str
* @return string
* @author carlo<[email protected]>
*/
function getFormatString($str)
{
if (empty($str)) {
return null;
}

if (is_numeric($str)) {
$str = trim($str);

return $str;
}

if (is_string($str)) {
if (!is_null(json_decode($str))) {
return $str;
}

return addslashes(strip_tags(trim($str)));
}

return $str;
}
}
53 changes: 3 additions & 50 deletions src/Clients/EnterpriseWeChatClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@


use RobotWebHook\Exceptions\RobotWebHookException;
use RobotWebHook\Traits\common;

class EnterpriseWeChatClient
{
use common;

private $config = array();

public function __construct(array $config)
Expand Down Expand Up @@ -104,54 +107,4 @@ public function send(array $data)
}
return $res;
}

function httpPostJson($url, $jsonStr)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($jsonStr)
)
);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array('url' => $url, 'jsonStr' => $jsonStr, 'httpCode' => $httpCode, 'body' => $response);
}

/**
* 获取字符过滤 用反斜线转义字符串
*
* @param string $str
* @return string
* @author carlo<[email protected]>
*/
function getFormatString($str)
{
if (empty($str)) {
return null;
}

if (is_numeric($str)) {
$str = trim($str);

return $str;
}

if (is_string($str)) {
if (!is_null(json_decode($str))) {
return $str;
}

return addslashes(strip_tags(trim($str)));
}

return $str;
}
}
2 changes: 1 addition & 1 deletion src/Config/config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
return array(
'web_hook_url' => '',
'client_drive' => 'EnterpriseWeChatClient',
'client_drive' => '',
);
79 changes: 79 additions & 0 deletions src/Traits/common.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,84 @@

trait common
{
/**
* 返回当前的毫秒时间戳
* @return float
*/
public function get_millisecond()
{
list($millisecond, $second) = explode(' ', microtime());
return (float)sprintf('%.0f', (floatval($millisecond) + floatval($second)) * 1000);
}


/**
* 获取字符过滤 用反斜线转义字符串
*
* @param string $str
* @return string
* @author carlo<[email protected]>
*/
function getFormatString($str)
{
if (empty($str)) {
return null;
}

if (is_numeric($str)) {
$str = trim($str);

return $str;
}

if (is_string($str)) {
if (!is_null(json_decode($str))) {
return $str;
}

return addslashes(strip_tags(trim($str)));
}

return $str;
}

/**
* 将编码转为UTF-8
* @param $str
* @return string
*/
function transcoding_utf8($str){
//转码
$encoding=mb_detect_encoding($str,array("ASCII","UTF-8","GB2312","GBK","BIG5"));
if($encoding!='UTF-8'){
$str = mb_convert_encoding($str, 'utf-8',$encoding);
}
return $str;
}

/**
* 发送post json请求
* @param $url
* @param $jsonStr
* @return array
*/
function httpPostJson($url, $jsonStr)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonStr);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//绕过ssl验证
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'Content-Length: ' . strlen($jsonStr)
)
);
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return array('url' => $url, 'jsonStr' => $jsonStr, 'httpCode' => $httpCode, 'body' => $response);
}
}

0 comments on commit 6e2adf5

Please sign in to comment.