Files
kiri-wchat/wchat/wx/SecCheck.php
T

137 lines
3.9 KiB
PHP
Raw Normal View History

2019-12-19 15:00:25 +08:00
<?php
2020-03-05 12:41:49 +08:00
namespace wchat\wx;
2019-12-19 15:00:25 +08:00
2022-09-09 16:42:55 +08:00
use Kiri\Client;
2020-03-05 12:41:49 +08:00
use wchat\common\Result;
2019-12-19 15:00:25 +08:00
/**
* Class SecCheck
* @package wchat
*/
class SecCheck extends SmallProgram
{
2022-09-09 16:42:55 +08:00
private string $_url = '/wxa/img_sec_check?access_token=';
2019-12-19 15:00:25 +08:00
2022-09-09 16:42:55 +08:00
private string $_msgUrl = '/wxa/msg_sec_check?access_token=';
2019-12-19 15:00:25 +08:00
2022-09-09 16:42:55 +08:00
private string $_mediaCheckAsync = '/wxa/media_check_async?access_token=';
2019-12-19 15:00:25 +08:00
const MEDIA_VIDEO = 1;
const MEDIA_IMAGE = 1;
/**
2022-09-09 16:42:55 +08:00
* @param string $path
* @return Result
2019-12-19 15:00:25 +08:00
*/
2022-09-09 16:42:55 +08:00
public function image(string $path = ''): Result
2019-12-19 15:00:25 +08:00
{
if (!file_exists($path)) {
return $this->sendError('文件不存在', 404);
}
2022-09-09 16:42:55 +08:00
$access_token = $this->config->getAccessToken();
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'multipart/form-data']);
2023-11-07 15:13:11 +08:00
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
2022-09-09 16:42:55 +08:00
$client->upload($this->_url . '?access_token=' . $access_token, [
'media' => new \CURLFile($path)
]);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
2023-08-18 19:59:46 +08:00
return new Result(code: 505, message: $client->getBody());
2022-09-09 16:42:55 +08:00
}
$body = json_decode($client->getBody(), true);
if (isset($body['errcode']) && $body['errcode'] != 0) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
}
return new Result(code: 0, data: $body);
2019-12-19 15:00:25 +08:00
}
/**
2022-09-09 16:42:55 +08:00
* @param string $url
2019-12-19 15:00:25 +08:00
* @param int $type
* @return mixed
* @throws
*/
2022-09-09 16:42:55 +08:00
public function mediaAsync(string $url, int $type = SecCheck::MEDIA_IMAGE): Result
2019-12-19 15:00:25 +08:00
{
if (!in_array($type, [self::MEDIA_IMAGE, self::MEDIA_VIDEO])) {
throw new \Exception('暂不支持的文件类型');
}
2020-11-14 03:04:25 +08:00
$requestUrl = $this->_mediaCheckAsync . $this->config->getAccessToken();
2022-09-09 16:42:55 +08:00
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
2023-11-07 15:13:11 +08:00
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
2022-09-09 16:42:55 +08:00
$client->post($requestUrl, ['media_url' => $url, 'media_type' => $type]);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
2023-08-18 19:59:46 +08:00
return new Result(code: 505, message: $client->getBody());
2022-09-09 16:42:55 +08:00
}
$body = json_decode($client->getBody(), true);
if (isset($body['errcode']) && $body['errcode'] != 0) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
} else {
return new Result(code: 0, data: $body);
2019-12-19 15:00:25 +08:00
}
}
/**
* @param $params
* @return ContentAsyncCheck|null
*/
2022-09-09 16:42:55 +08:00
public function readByEvent($params): ?ContentAsyncCheck
2019-12-19 15:00:25 +08:00
{
return ContentAsyncCheck::instance($params);
}
/**
* @param $content
2022-09-09 16:42:55 +08:00
* @return Result
2019-12-19 15:00:25 +08:00
*/
2022-09-09 16:42:55 +08:00
public function text($content): Result
2019-12-19 15:00:25 +08:00
{
if (empty($content)) {
return $this->sendError('文件不存在', 404);
}
2022-09-09 16:42:55 +08:00
$requestUrl = $this->_msgUrl . $this->config->getAccessToken();
2020-11-14 03:04:25 +08:00
2022-09-09 16:42:55 +08:00
$client = new Client('api.weixin.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
2023-11-07 15:13:11 +08:00
$proxyHost = $this->getConfig()->getProxyHost();
$proxyPort = $this->getConfig()->getProxyPort();
if (!empty($proxyHost) && $proxyPort > 0) {
$client->withProxyHost($proxyHost)->withProxyPort($proxyPort);
}
2022-09-09 16:42:55 +08:00
$client->post($requestUrl, ['content' => $content]);
$client->close();
2020-11-14 03:04:25 +08:00
2022-09-09 16:42:55 +08:00
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
2023-08-18 19:59:46 +08:00
return new Result(code: 505, message: $client->getBody());
2022-09-09 16:42:55 +08:00
}
$body = json_decode($client->getBody(), true);
if (isset($body['errcode']) && $body['errcode'] != 0) {
return new Result(code: $body['errcode'], message: $body['errmsg']);
} else {
return new Result(code: 0, data: $body);
}
2019-12-19 15:00:25 +08:00
}
}