-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
jiangbj
committed
Apr 18, 2020
1 parent
89664ff
commit 6e2adf5
Showing
6 changed files
with
116 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,12 @@ | |
|
||
|
||
use RobotWebHook\Exceptions\RobotWebHookException; | ||
use RobotWebHook\Traits\common; | ||
|
||
class DingTalkClient | ||
{ | ||
use common; | ||
|
||
private $config = array(); | ||
|
||
public function __construct(array $config) | ||
|
@@ -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."×tamp=".$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 .= "×tamp={$timestamp}&sign={$sign}"; | ||
} | ||
$res = $this->httpPostJson($url, $jsonStr); | ||
if ($res['httpCode'] != 200) { | ||
throw new RobotWebHookException($res['body'], $res['httpCode']); | ||
} | ||
|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,9 +5,12 @@ | |
|
||
|
||
use RobotWebHook\Exceptions\RobotWebHookException; | ||
use RobotWebHook\Traits\common; | ||
|
||
class EnterpriseWeChatClient | ||
{ | ||
use common; | ||
|
||
private $config = array(); | ||
|
||
public function __construct(array $config) | ||
|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' => '', | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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); | ||
} | ||
} |