mirror of
http://124.126.16.154:8888/singularity/HyperfDevelopmentKitCore.git
synced 2026-01-15 06:15:10 +08:00
feat: 添加了 http 请求服务
This commit is contained in:
9
.idea/HDK-Core.iml
generated
9
.idea/HDK-Core.iml
generated
@@ -64,8 +64,6 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/http-message" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/exception-handler" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/macroable" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/resource" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/paginator" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/engine" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/pool" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/event" />
|
||||
@@ -135,14 +133,17 @@
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/nunomaduro/collision" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/pestphp/pest" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/symfony/stopwatch" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/spatie/test-time" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/phpstan/phpstan" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/pestphp/pest-plugin" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/friendsofphp/php-cs-fixer" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/spatie/pest-plugin-test-time" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/filp/whoops" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/doctrine/deprecations" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/paginator" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/spatie/pest-plugin-test-time" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/coordinator" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/resource" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/spatie/test-time" />
|
||||
<excludeFolder url="file://$MODULE_DIR$/vendor/hyperf/guzzle" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
|
||||
1
.idea/php.xml
generated
1
.idea/php.xml
generated
@@ -158,6 +158,7 @@
|
||||
<path value="$PROJECT_DIR$/vendor/phpstan/phpstan" />
|
||||
<path value="$PROJECT_DIR$/vendor/doctrine/deprecations" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/coordinator" />
|
||||
<path value="$PROJECT_DIR$/vendor/hyperf/guzzle" />
|
||||
</include_path>
|
||||
</component>
|
||||
<component name="PhpProjectSharedConfiguration" php_language_level="8.0" />
|
||||
|
||||
@@ -43,6 +43,7 @@
|
||||
"firebase/php-jwt": "^6.1",
|
||||
"friendsofphp/php-cs-fixer": "^3.13",
|
||||
"guzzlehttp/guzzle": "^7.5",
|
||||
"hyperf/guzzle": "^3.0",
|
||||
"hyperf/session": "3.0.*",
|
||||
"hyperf/validation": "3.0.*",
|
||||
"pestphp/pest": "^1.22",
|
||||
@@ -57,7 +58,7 @@
|
||||
"firebase/php-jwt": "JWT 鉴权必需",
|
||||
"hyperf/session": "Session 鉴权必需",
|
||||
"symfony/mailer": "用于发送电子邮件",
|
||||
"guzzlehttp/guzzle": "需要发起 http 请求时必需",
|
||||
"hyperf/guzzle": "需要发起 http 请求时必需",
|
||||
"alibabacloud/dysmsapi-20170525": "阿里云短信服务必需"
|
||||
},
|
||||
"config": {
|
||||
|
||||
49
composer.lock
generated
49
composer.lock
generated
@@ -4,7 +4,7 @@
|
||||
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
|
||||
"This file is @generated automatically"
|
||||
],
|
||||
"content-hash": "b6e95f784b6b427fe9932b65640473b8",
|
||||
"content-hash": "8fb38987fde57f32e3dba89caca71823",
|
||||
"packages": [
|
||||
{
|
||||
"name": "composer/ca-bundle",
|
||||
@@ -4791,6 +4791,51 @@
|
||||
],
|
||||
"time": "2022-10-26T14:07:24+00:00"
|
||||
},
|
||||
{
|
||||
"name": "hyperf/guzzle",
|
||||
"version": "v3.0.10",
|
||||
"dist": {
|
||||
"type": "zip",
|
||||
"url": "https://mirrors.tencent.com/repository/composer/hyperf/guzzle/v3.0.10/hyperf-guzzle-v3.0.10.zip",
|
||||
"reference": "6ed6f64f21c7a1f000502881d6af6492f54d6dec",
|
||||
"shasum": ""
|
||||
},
|
||||
"require": {
|
||||
"guzzlehttp/guzzle": "^6.3|^7.0",
|
||||
"hyperf/utils": "~3.0.0",
|
||||
"php": ">=8.0",
|
||||
"psr/container": "^1.0|^2.0",
|
||||
"psr/http-message": "^1.0"
|
||||
},
|
||||
"suggest": {
|
||||
"hyperf/pool": "Required to use pool handler."
|
||||
},
|
||||
"type": "library",
|
||||
"extra": {
|
||||
"branch-alias": {
|
||||
"dev-master": "3.0-dev"
|
||||
},
|
||||
"hyperf": {
|
||||
"config": "Hyperf\\Guzzle\\ConfigProvider"
|
||||
}
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Hyperf\\Guzzle\\": "src/"
|
||||
}
|
||||
},
|
||||
"license": [
|
||||
"MIT"
|
||||
],
|
||||
"description": "Swoole coroutine handler for guzzle",
|
||||
"keywords": [
|
||||
"Guzzle",
|
||||
"handler",
|
||||
"php",
|
||||
"swoole"
|
||||
],
|
||||
"time": "2023-03-10T04:54:20+00:00"
|
||||
},
|
||||
{
|
||||
"name": "hyperf/session",
|
||||
"version": "v3.0.0",
|
||||
@@ -7631,5 +7676,5 @@
|
||||
"ext-swoole": "*"
|
||||
},
|
||||
"platform-dev": [],
|
||||
"plugin-api-version": "2.1.0"
|
||||
"plugin-api-version": "2.3.0"
|
||||
}
|
||||
|
||||
119
src/Service/HttpRequestService.php
Normal file
119
src/Service/HttpRequestService.php
Normal file
@@ -0,0 +1,119 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Singularity\HDK\Core\Service;
|
||||
|
||||
use Ergebnis\Http\Method;
|
||||
use GuzzleHttp\Client;
|
||||
use GuzzleHttp\Exception\BadResponseException;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use GuzzleHttp\Exception\ServerException;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use GuzzleHttp\Psr7\Utils;
|
||||
use Hyperf\Di\Annotation\Inject;
|
||||
use Hyperf\Guzzle\ClientFactory;
|
||||
use Hyperf\Utils\Codec\Json;
|
||||
use Psr\Http\Client\ClientExceptionInterface;
|
||||
use Psr\Http\Message\RequestInterface;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
|
||||
/**
|
||||
* 发起 Http 请求的类
|
||||
*/
|
||||
class HttpRequestService
|
||||
{
|
||||
public const TIMEOUT = 20;
|
||||
|
||||
/**
|
||||
* @Inject()
|
||||
* @var ClientFactory
|
||||
*/
|
||||
private ClientFactory $client;
|
||||
|
||||
/**
|
||||
* @var array<literal-string, mixed>
|
||||
*/
|
||||
private array $options = [];
|
||||
|
||||
/**
|
||||
* @throws GuzzleException
|
||||
* @throws ClientExceptionInterface
|
||||
*/
|
||||
public function requestGet(string $url, array $params = []): ResponseInterface
|
||||
{
|
||||
$request = new Request(Method\Rfc\Rfc7231::GET, $url);
|
||||
return $this->getClient()->send($request, [
|
||||
'params' => $params,
|
||||
]);
|
||||
}
|
||||
|
||||
private function getClient(): Client
|
||||
{
|
||||
return $this->client->create($this->options);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param array $data
|
||||
* @param array $params
|
||||
* @return ResponseInterface
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function requestPost(string $url, string $data = '', array $params = []): ResponseInterface
|
||||
{
|
||||
$request = new Request('post', $url, [], $data);
|
||||
return $this->getClient()->send($request, [
|
||||
'params' => $params,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $url
|
||||
* @param array $data
|
||||
* @param array $params
|
||||
* @return ResponseInterface
|
||||
* @throws GuzzleException
|
||||
*/
|
||||
public function requestPut(string $url, array $data = [], array $params = []): ResponseInterface
|
||||
{
|
||||
$data = Json::encode($data);
|
||||
$request = new Request('put', $url, ['Content-Type' => 'application/json'], Utils::streamFor($data));
|
||||
$result = $this->getClient()->send($request, [
|
||||
'params' => $params,
|
||||
]);
|
||||
$json = Json::decode($result->getBody()->getContents());
|
||||
if ($json['code'] !== 200) {
|
||||
throw new ServerException($json['message'], $request, $result);
|
||||
}
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 定制 options
|
||||
* @param array $options
|
||||
* @return $this
|
||||
*/
|
||||
public function setOptions(array $options): HttpRequestService
|
||||
{
|
||||
$this->options = array_replace_recursive($this->options, $options);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param ResponseInterface $response
|
||||
* @param RequestInterface $request
|
||||
* @return ResponseInterface
|
||||
*/
|
||||
private function parseResponse(
|
||||
ResponseInterface $response,
|
||||
RequestInterface $request
|
||||
): ResponseInterface {
|
||||
if ($response->getStatusCode() >= 200 && $response->getStatusCode() < 300) {
|
||||
return $response;
|
||||
} else {
|
||||
throw new BadResponseException($response->getReasonPhrase(), $request, $response);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user