103 lines
2.5 KiB
PHP
103 lines
2.5 KiB
PHP
<?php
|
|
|
|
|
|
namespace wchat\wx;
|
|
|
|
|
|
use wchat\common\Result;
|
|
|
|
/**
|
|
* Class SecCheck
|
|
* @package wchat
|
|
*/
|
|
class SecCheck extends SmallProgram
|
|
{
|
|
|
|
private $_url = 'https://api.weixin.qq.com/wxa/img_sec_check?access_token=';
|
|
|
|
private $_msgUrl = '/wxa/msg_sec_check?access_token=';
|
|
|
|
private $_mediaCheckAsync = '/wxa/media_check_async?access_token=';
|
|
|
|
const MEDIA_VIDEO = 1;
|
|
const MEDIA_IMAGE = 1;
|
|
|
|
/**
|
|
* @param $path
|
|
* @return array|Result|mixed
|
|
*/
|
|
public function image($path = '')
|
|
{
|
|
if (!file_exists($path)) {
|
|
return $this->sendError('文件不存在', 404);
|
|
}
|
|
$this->request->setUseSwoole(false);
|
|
$this->request->setIsSSL(true);
|
|
$this->request->setHeader('Content-Type', 'multipart/form-data');
|
|
$this->request->setErrorField('errcode');
|
|
$this->request->setErrorMsgField('errmsg');
|
|
return $this->request->upload($this->_url . $this->config->getAccessToken(), ['media' => new \CURLFile($path)]);
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $url
|
|
* @param int $type
|
|
* @return mixed
|
|
* @throws
|
|
*/
|
|
public function mediaAsync($url, $type = SecCheck::MEDIA_IMAGE)
|
|
{
|
|
if (!in_array($type, [self::MEDIA_IMAGE, self::MEDIA_VIDEO])) {
|
|
throw new \Exception('暂不支持的文件类型');
|
|
}
|
|
$this->request->setIsSSL(true);
|
|
$this->request->setHost('api.weixin.qq.com');
|
|
$this->request->setHeader('Content-Type', 'application/json');
|
|
$this->request->setErrorField('errcode');
|
|
$this->request->setErrorMsgField('errmsg');
|
|
|
|
$requestUrl = $this->_mediaCheckAsync . $this->config->getAccessToken();
|
|
$response = $this->request->post($requestUrl, [
|
|
'media_url' => $url,
|
|
'media_type' => $type
|
|
]);
|
|
if (!$response->isResultsOK()) {
|
|
throw new \Exception($response->getMessage());
|
|
}
|
|
return $response->getData('trace_id');
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $params
|
|
* @return ContentAsyncCheck|null
|
|
*/
|
|
public function readByEvent($params)
|
|
{
|
|
return ContentAsyncCheck::instance($params);
|
|
}
|
|
|
|
|
|
/**
|
|
* @param $content
|
|
* @return array|Result|mixed
|
|
*/
|
|
public function text($content)
|
|
{
|
|
if (empty($content)) {
|
|
return $this->sendError('文件不存在', 404);
|
|
}
|
|
$this->request->setIsSSL(true);
|
|
$this->request->setHost('api.weixin.qq.com');
|
|
$this->request->setHeader('Content-Type', 'application/json');
|
|
$this->request->setErrorField('errcode');
|
|
$this->request->setErrorMsgField('errmsg');
|
|
|
|
$requestUrl = $this->_msgUrl . $this->config->getAccessToken();
|
|
|
|
return $this->request->post($requestUrl, ['content' => $content]);
|
|
}
|
|
|
|
}
|