Add json request & response. Perform async service via json request.
This commit is contained in:
joyqi 2022-04-02 15:33:17 +08:00
parent d77a1ecad7
commit d4068b1661
4 changed files with 59 additions and 21 deletions

View File

@ -151,6 +151,22 @@ class Client
return $this;
}
/**
* 设置需要请求的Json数据
*
* @param $data
* @param string $method
* @return $this
*/
public function setJson($data, string $method = self::METHOD_POST): Client
{
$this->setData(json_encode($data), $method)
->setMultipart(true)
->setHeader('Content-Type', 'application/json');
return $this;
}
/**
* 设置方法名
*

View File

@ -149,6 +149,18 @@ class Request
$exists = false;
}
break;
case $key === '@json':
$exists = false;
if ($this->isJson()) {
$body = file_get_contents('php://input');
if (false !== $body) {
$exists = true;
$value = json_decode($body, true, 16);
$default = $default ?? $value;
}
}
break;
case isset($_GET[$key]):
$value = $_GET[$key];
break;
@ -472,6 +484,16 @@ class Request
return 'XMLHttpRequest' == $this->getHeader('X-Requested-With');
}
/**
* 判断是否为Json请求
*
* @return bool
*/
public function isJson(): bool
{
return !!preg_match("/^\s*application\/json(;|$)/i", $this->getHeader('Content-Type', ''));
}
/**
* 判断输入是否满足要求
*

View File

@ -288,7 +288,7 @@ class Edit extends Contents implements ActionInterface
self::pluginHandle()->finishPublish($contents, $this);
/** 发送ping */
$trackback = array_unique(preg_split("/(\r|\n|\r\n)/", trim($this->request->trackback)));
$trackback = array_filter(array_unique(preg_split("/(\r|\n|\r\n)/", trim($this->request->trackback))));
Service::alloc()->sendPing($this, $trackback);
/** 设置提示信息 */

View File

@ -37,10 +37,11 @@ class Service extends BaseOptions implements ActionInterface
public function sendPingHandle()
{
/** 验证权限 */
$token = $this->request->get('token');
$permalink = $this->request->get('permalink');
$title = $this->request->get('title');
$excerpt = $this->request->get('excerpt');
$data = $this->request->get('@json');
$token = $data['token'] ?? '';
$permalink = $data['permalink'];
$title = $data['title'];
$excerpt = $data['excerpt'];
$response = ['trackback' => [], 'pingback' => []];
@ -57,8 +58,8 @@ class Service extends BaseOptions implements ActionInterface
set_time_limit(30);
}
if (!empty($this->request->pingback)) {
$links = $this->request->getArray('pingback');
if (!empty($data['pingback'])) {
$links = $data['pingback'];
$permalinkPart = parse_url($permalink);
/** 发送pingback */
@ -114,8 +115,8 @@ class Service extends BaseOptions implements ActionInterface
}
/** 发送trackback */
if (!empty($this->request->trackback)) {
$links = $this->request->getArray('trackback');
if (!empty($data['trackback'])) {
$links = $data['trackback'];
foreach ($links as $url) {
$client = Client::get();
@ -179,9 +180,8 @@ class Service extends BaseOptions implements ActionInterface
$client->setHeader('User-Agent', $this->options->generator)
->setTimeout(2)
->setData($input)
->setMethod(Client::METHOD_POST)
->send($this->getServiceUrl());
->setJson($input)
->send($this->getServiceUrl('ping'));
} catch (Client\Exception $e) {
return;
}
@ -191,9 +191,10 @@ class Service extends BaseOptions implements ActionInterface
/**
* 获取真实的 URL
*
* @param string $do 动作名
* @return string
*/
private function getServiceUrl(): string
private function getServiceUrl(string $do): string
{
$url = Common::url('/action/service', $this->options->index);
@ -214,7 +215,7 @@ class Service extends BaseOptions implements ActionInterface
$url = Common::buildUrl($parts);
}
return $url;
return $url . '?do=' . $do;
}
/**
@ -233,13 +234,11 @@ class Service extends BaseOptions implements ActionInterface
try {
$client->setHeader('User-Agent', $this->options->generator)
->setTimeout(2)
->setData([
'do' => 'async',
'requests' => json_encode($this->asyncRequests),
->setJson([
'requests' => $this->asyncRequests,
'token' => Common::timeToken($this->options->secret)
])
->setMethod(Client::METHOD_POST)
->send($this->getServiceUrl());
->send($this->getServiceUrl('async'));
} catch (Client\Exception $e) {
return;
}
@ -260,7 +259,8 @@ class Service extends BaseOptions implements ActionInterface
public function asyncHandle()
{
/** 验证权限 */
$token = $this->request->token;
$data = $this->request->get('@json');
$token = $data['token'] ?? '';
if (!Common::timeTokenValidate($token, $this->options->secret, 3)) {
throw new Exception(_t('禁止访问'), 403);
@ -275,7 +275,7 @@ class Service extends BaseOptions implements ActionInterface
set_time_limit(30);
}
$requests = json_decode($this->request->requests, true);
$requests = $data['requests'] ?? null;
$plugin = self::pluginHandle();
if (!empty($requests)) {