Files
huangjingfen/pro_v3.5.1/crmeb/services/wechat/SwooleResponse.php
panchengyong c1e74d8e68 chore(php): 统一 ScottPan 文件头与注释域名替换
- 按 docs/renew-code-comment.md 将 PHP 文件头改为带边框的 Author 注释\n- 注释中的 crmeb.com 替换为 uj345.cn(代码字符串中的外链未改)\n- 新增 docs/renew-code-comment.md 说明

Made-with: Cursor
2026-03-29 11:22:58 +08:00

156 lines
3.6 KiB
PHP

<?php
// +----------------------------------------------------------------------
// | Author: ScottPan Team
// +----------------------------------------------------------------------
namespace crmeb\services\wechat;
use Symfony\Component\HttpClient\Exception\ServerException;
use Symfony\Component\HttpClient\Exception\ClientException;
use Symfony\Component\HttpClient\Exception\RedirectionException;
use Symfony\Contracts\HttpClient\ResponseInterface;
use Swoole\Coroutine\Http\Client;
/**
* 携程请求返回资源
* Class SwooleResponse
* @author 等风来
* @email 136327134@qq.com
* @date 2024/1/25
* @package crmeb\services\wechat
*/
class SwooleResponse implements ResponseInterface
{
/**
* @var Client
*/
private $swooleResponse;
/**
* SwooleResponse constructor.
* @param Client $swooleResponse
*/
public function __construct(Client $swooleResponse)
{
$this->swooleResponse = $swooleResponse;
}
/**
* @param bool $throw
* @return string
* @author 等风来
* @email 136327134@qq.com
* @date 2024/1/25
*/
public function getContent(bool $throw = true): string
{
if ($throw) {
$this->checkStatusCode();
}
return $this->swooleResponse->getBody();
}
/**
* @return int
* @author 等风来
* @email 136327134@qq.com
* @date 2024/1/25
*/
public function getStatusCode(): int
{
return $this->swooleResponse->getStatusCode();
}
/**
* @param bool $throw
* @return array
* @author 等风来
* @email 136327134@qq.com
* @date 2024/1/25
*/
public function getHeaders(bool $throw = true): array
{
if ($throw) {
$this->checkStatusCode();
}
$headers = [];
foreach ($this->swooleResponse->getHeaders() as $name => $value) {
$headers[strtolower($name)][] = $value;
}
return $headers;
}
/**
* @param bool $throw
* @return array
* @author 等风来
* @email 136327134@qq.com
* @date 2024/1/25
*/
public function toArray(bool $throw = true): array
{
$content = $this->getContent($throw);
if ($content === '' && $throw) {
throw new \InvalidArgumentException('The content is not valid JSON.');
}
return json_decode($content, true);
}
/**
* @author 等风来
* @email 136327134@qq.com
* @date 2024/1/25
*/
public function cancel(): void
{
$this->swooleResponse->close();
}
/**
* @param string|null $type
* @return mixed
* @author 等风来
* @email 136327134@qq.com
* @date 2024/1/24
*/
public function getInfo(string $type = null): mixed
{
if ($type === null || $type === 'http_code') {
return $this->getStatusCode();
}
$httpResponse = [
'http_code' => $this->getStatusCode(),
'response_headers' => $this->swooleResponse->getHeaders(),
'url' => $this->swooleResponse->host,
];
return $type ? $httpResponse[$type] ?? null : $httpResponse;
}
/**
* @author 等风来
* @email 136327134@qq.com
* @date 2024/1/25
*/
private function checkStatusCode()
{
$code = $this->getInfo('http_code');
if (500 <= $code) {
throw new ServerException($this);
}
if (400 <= $code) {
throw new ClientException($this);
}
if (300 <= $code) {
throw new RedirectionException($this);
}
}
}