我的日常开发记录日志
首页
  • Laravel
  • Thinkphp
  • Swoole
  • Workman
  • php
  • HTML
  • CSS
  • JavaScript
  • Vue
  • ES6
  • 小程序
  • Mysql
  • Redis
  • Es
  • MongoDb
  • Git
  • Composer
  • Linux
  • Nginx
  • Docker
  • Vpn
  • 开发实战
  • 开发工具类
  • 友情链接
💖关于
💻收藏
  • 分类
  • 标签
  • 归档数据
GitHub (opens new window)

我的日常开发记录日志

never give up
首页
  • Laravel
  • Thinkphp
  • Swoole
  • Workman
  • php
  • HTML
  • CSS
  • JavaScript
  • Vue
  • ES6
  • 小程序
  • Mysql
  • Redis
  • Es
  • MongoDb
  • Git
  • Composer
  • Linux
  • Nginx
  • Docker
  • Vpn
  • 开发实战
  • 开发工具类
  • 友情链接
💖关于
💻收藏
  • 分类
  • 标签
  • 归档数据
GitHub (opens new window)
  • laravel

  • thinkphp

  • swoole

  • workman

  • php

    • php
    • phpstorm常用快捷键
    • new关键字和依赖注入对比
    • windows上php多版本存在
    • 封装一个wkhtmltopdf的扩展类
    • 请求第三方接口绕过白名单操作
    • 封装一个基础的openai请求类
    • 封装一个基础的请求第三方类
    • 支付宝支付使用证书方式进行支付的类
    • cookie通俗讲解
    • php-fpm服务的重启
    • guzzle中cookie使用
  • gpt

  • java

  • 后端
  • php
窝窝侠
2024-10-18

封装一个基础的请求第三方类

<?php

namespace App\Services\Api;

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

class ApiPaperService
{
    private static $instance;
    protected      $client;
    protected      $baseUrl;
    protected      $appKey;
    public         $getPaperListUrl         = '/paper/list';
    public         $getPaperQuestionListUrl = '/api/app/paper/quest_list';
    public         $getPaperQuestionInfotUrl = '/api/app/quest/info';

    public function __construct(array $config = [])
    {
        $this->baseUrl = $config['base_url'] ?? getenv('PAPER_BASE_URL');
        $this->appKey = $config['app_secret'] ?? getenv('PAPER_APP_SECRET');
    }

    /**
     * 实例化
     * @return static
     */
    public static function instance(array $config = []): ApiPaperService
    {
        if (is_null(self::$instance)) {
            self::$instance = new self($config);
        }

        return self::$instance;
    }

    /**
     *获取试卷列表
     * @param $page
     * @param int $pageSize
     * @param string $keyword
     * @return array
     * @throws \Exception
     */
    public function getPaperList($page, int $pageSize = 10, string $keyword = ''): array
    {
        return $this->getJson($this->getPaperListUrl, [
            'page'    => $page,
            'limit'   => $pageSize,
            'keyword' => $keyword
        ]);
    }

    /**
     * 创建 Guzzle 客户端
     * @return Client
     */
    protected function getClient(): Client
    {
        if (! $this->client) {
            $this->client = new Client([
                // 'base_uri' => $this->baseUrl,
                'timeout' => 30,
                'headers' => [
                    'Authorization' => "{$this->appKey}",
                ],
            ]);
        }

        return $this->client;
    }

    /**
     * POST 请求
     * @param string $url
     * @param array $data
     * @return array
     * @throws \Exception
     */
    public function postJson(string $url, array $data = []): array
    {
        return $this->request('POST', $url, [
            'json' => $data,
            'verify' => false
        ]);
    }

    /**
     * GET 请求
     * @param string $url
     * @param array $queryParams
     * @return array
     * @throws \Exception
     */
    public function getJson(string $url, array $queryParams = []): array
    {
        return $this->request('GET', $url, [
            'query' => $queryParams,
            'verify' => false
        ]);
    }

    /**
     * 发送请求
     * @param string $method
     * @param string $url
     * @param array $options
     * @return array
     * @throws \Exception
     */
    protected function request(string $method, string $url, array $options = []): array
    {
        try {
            if ($method == 'GET') {
                $response = $this->getClient()->get($this->baseUrl.$url, $options);
            } else {
                $response = $this->getClient()->post($this->baseUrl.$url, $options);
            }
            $body = $response->getBody()->getContents();

            return json_decode($body, true);

        } catch (RequestException $e) {

            log_message("error", "新题库接口请求失败,原因:".$e->getMessage().",file:".$e->getFile().",line:".$e->getLine());

            return ['error_code' => 1, 'error_msg' => $e->getMessage() ?? '新题库请求失败'];
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
在线编辑 (opens new window)
上次更新: 2025/02/25, 18:30:54
封装一个基础的openai请求类
支付宝支付使用证书方式进行支付的类

← 封装一个基础的openai请求类 支付宝支付使用证书方式进行支付的类→

最近更新
01
showprocess用法
04-29
02
vue3中尖括号和冒号的使用细则
04-29
03
sd使用
02-22
更多文章>
🖥️

© 2025窝窝侠 💌 豫ICP备20005263号-2 🛀 Theme by 💝 Vdoing && 小胖墩er

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×