lumen封装Client

封装使用Client作为请求体

写入日志这是之前封装的日志

<?php

namespace App\Common;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use App\Common\ApiLog as Log;

class HttpClient
{
    /**
     * @throws GuzzleException
     */
    public static function get($uri, array $queryOptions = [], $headers = []): array
    {
        $client = new Client(['base_uri' => $uri, 'timeout' => 5]);

        $requestOptions = [
            'query' => $queryOptions,
            'headers' => $headers
        ];

        if (empty($queryOptions) && empty($headers)) {
            $requestOptions = [];
        }

        $log = [
            'url' => $uri,
            'method' => 'GET',
            'remark' => 'HttpClient get',
            'request' => $requestOptions
        ];

        try {
            $response = $client->get($uri, $requestOptions);
            $content = json_decode($response->getBody()->getContents(), true);
            $status = $response->getStatusCode();
            $log['statusCode'] = $status;
            $log['body'] = $content;
            Log::debug($log);
            return [$status, $content];
        } catch (\Throwable $throwable) {
            $log['error'] = $throwable->getMessage();
            $log['line'] = $throwable->getLine();
            Log::error($log);
            return [HTTP_ERROR, array('errorMsg' => $throwable->getMessage())];
        }
    }

    public static function postForm($uri, array $formParams = [], array $headers = []): array
    {
        $requestOptions = [
            'form_params' => $formParams,
            'headers' => $headers
        ];
        return self::postAction($uri, $requestOptions);
    }

    public static function postJson($uri, $jsonParams = [], array $headers = []): array
    {
        $headers = array_merge(['Content-Type'  => 'application/json; charset=utf-8'], $headers);
        $requestOptions = [
            'json' => $jsonParams,
            'headers' => $headers
        ];
        return self::postAction($uri, $requestOptions);
    }

    private static function postAction($uri, array $options = []): array
    {
        $client = new Client(['base_uri' => $uri, 'timeout' => 20]);

        $log = [
            'url' => $uri,
            'method' => 'POST',
            'remark' => 'HttpClient postAction',
            'request' => $options
        ];

        try {
            $response = $client->post($uri, $options);
            $content = json_decode($response->getBody()->getContents(), true);
            $status = $response->getStatusCode();
            if (strpos($uri, "lumen") === false) {
                $log['statusCode'] = $status;
                $log['body'] = $content;
                Log::debug($log);
            }

            return [$status, $content];
        } catch (\Throwable $throwable) {
            $log['error'] = $throwable->getMessage();
            $log['line'] = $throwable->getLine();
            Log::error($log);
            return [HTTP_ERROR, array('errorMsg' => $throwable->getMessage())];
        }
    }


    public static function delete($uri, array $queryOptions = [], $headers = [], $body = []): array
    {
        $client = new Client(['base_uri' => $uri, 'timeout' => 5]);

        $requestOptions = [
            'query' => $queryOptions,
            'headers' => $headers,
        ];
        if (!empty($body)) {
            $requestOptions['json'] = $body;
        }

        $log = [
            'url' => $uri,
            'method' => 'DELETE',
            'remark' => 'HttpClient delete',
            'request' => $requestOptions,
        ];

        try {
            $response = $client->delete($uri, $requestOptions);
            $content = json_decode($response->getBody()->getContents(), true);
            $status = $response->getStatusCode();
            $log['statusCode'] = $status;
            $log['body'] = $content;
            Log::debug($log);
            return [$status, $content];
        } catch (\Throwable $throwable) {
            $log['error'] = $throwable->getMessage();
            $log['line'] = $throwable->getLine();
            Log::error($log);
            return [HTTP_ERROR, array('errorMsg' => $throwable->getMessage())];
        }
    }

    public static function isSuccess($statusCode): bool
    {
        if ($statusCode >= 200 && $statusCode < 300) {
            return true;
        }

        return false;
    }
}

简单的调取示例

private static function extracted($backUrl, $authorization, array $paramData)
    {
        $header = ["Authorization" => $authorization];
        list($status, $content) = HttpClient::postJson($backUrl, $paramData, $header);
        if (HttpClient::isSuccess($status) && isset($content['code']) && $content['code'] == 0) {
            return response(['retcode' => 0, 'retmsg' => 'OK']);
        }
        return response(['retcode' => -1, 'retmsg' => 'FAIL']);
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值