Files
kiri-wchat/wchat/qq/SecCheck.php
T
2022-09-09 16:42:55 +08:00

82 lines
2.1 KiB
PHP

<?php
namespace wchat\qq;
use Kiri\Client;
use wchat\common\Result;
/**
* Class SecCheck
* @package qq
*/
class SecCheck extends SmallProgram
{
private string $_url = '/api/json/security/ImgSecCheck?access_token=';
private string $_msgUrl = '/api/json/security/MsgSecCheck?access_token=';
/**
* @param string $path
* @return Result
*/
public function image(string $path = ''): Result
{
if (!file_exists($path)) {
return $this->sendError('文件不存在', 404);
}
$real_path = new \CURLFile($path);
$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']);
$client->upload($path, $data);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: 'network error.');
}
$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);
}
}
/**
* @param string $content
* @return Result
*/
public function text(string $content): Result
{
if (empty($content)) {
return $this->sendError('文件不存在', 404);
}
$url = '/' . ltrim($this->_url, '/') . $this->config->getAccessToken();
$client = new Client('api.q.qq.com', 443, true);
$client->withHeader(['Content-Type' => 'application/json']);
$client->post($url, ['appid' => $this->config->getAppid(), 'content' => $content]);
$client->close();
if (!in_array($client->getStatusCode(), [101, 200, 201])) {
return new Result(code: 505, message: 'network error.');
}
$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);
}
}
}