封装一个基础的请求第三方类
<?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
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