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

92 lines
2.6 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\qq;
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 qq
*/
class SecCheck extends SmallProgram
{
2022-09-09 16:42:55 +08:00
private string $_url = '/api/json/security/ImgSecCheck?access_token=';
2019-12-19 15:00:25 +08:00
2022-09-09 16:42:55 +08:00
private string $_msgUrl = '/api/json/security/MsgSecCheck?access_token=';
2019-12-19 15:00:25 +08:00
/**
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);
}
2020-08-24 11:49:25 +08:00
$real_path = new \CURLFile($path);
2022-09-09 16:42:55 +08:00
$data = [
'appid' => $this->config->getAppid(),
'media' => $real_path,
'form-data[filename]' => $path,
'form-data[content-type]' => $real_path->getMimeType()
];
$client = new Client('api.q.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($path, $data);
$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
}
/**
2022-09-09 16:42:55 +08:00
* @param string $content
* @return Result
2019-12-19 15:00:25 +08:00
*/
2022-09-09 16:42:55 +08:00
public function text(string $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
$url = '/' . ltrim($this->_url, '/') . $this->config->getAccessToken();
2020-04-03 17:53:17 +08:00
2022-09-09 16:42:55 +08:00
$client = new Client('api.q.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($url, ['appid' => $this->config->getAppid(), 'content' => $content]);
$client->close();
2019-12-19 15:00: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
}
}