这是我某天突然想到的,python 的 requests 那么好用,为什么 php 要写的这么又臭又长呢?我就结合 claude code 写了一个 composer 包,完全仿照了 requests 模块。求轻喷,欢迎 star 、pr
github 地址 https://github.com/tg111/php-request
直接安装
composer require tg111/php-request
use PhpRequest\PhpRequest;
// 简单的 GET 请求
$response = PhpRequest::get('https://httpbin.org/get');
echo $response->text();
// 带参数的 GET 请求
$response = PhpRequest::get('https://httpbin.org/get', [
'key1' => 'value1',
'key2' => 'value2'
]);
// POST 请求
$response = PhpRequest::post('https://httpbin.org/post', [
'username' => 'user',
'password' => 'pass'
]);
// JSON POST 请求
$response = PhpRequest::post('https://httpbin.org/post', [
'name' => '张三',
'email' => '[email protected]'
], [
'headers' => ['Content-Type' => 'application/json']
]);
$response = requests_get('https://httpbin.org/get');
$response = requests_post('https://httpbin.org/post', ['key' => 'value']);
$response = PhpRequest::get('https://httpbin.org/cookies', [], [
'cookies' => [
'session_id' => 'abc123456789',
'user_preference' => 'dark_mode'
]
]);
# cookies
$response = PhpRequest::get('https://httpbin.org/cookies', [], [
'cookies' => [
'session_id' => 'abc123456789',
'user_preference' => 'dark_mode'
]
]);
会话允许在多个请求之间持久化 Cookie 、请求头和其他配置:
use PhpRequest\PhpRequest;
// 创建会话
$session = PhpRequest::session()
->setHeaders([
'Authorization' => 'Bearer token123',
'Accept' => 'application/json'
])
->setCookies([
'session_id' => 'session123'
])
->setTimeout(60);
// 使用会话进行多个请求
$profile = $session->get('/user/profile');
$settings = $session->get('/user/settings');
$updated = $session->post('/user/update', ['name' => '新名称']);
$response = PhpRequest::get('https://httpbin.org/json');
// 获取响应内容
$text = $response->text(); // 原始文本内容
$data = $response->json(); // 解析 JSON 响应
$code = $response->getStatusCode(); // HTTP 状态码
// 检查响应状态
$success = $response->ok(); // 2xx 状态码为 true
$isClientError = $response->isClientError(); // 4xx 状态码为 true
$isServerError = $response->isServerError(); // 5xx 状态码为 true
// 获取头部和元数据
$headers = $response->getHeaders();
$contentType = $response->getContentType();
$totalTime = $response->getTotalTime();
$url = $response->getUrl();
// 保存响应到文件
$response->save('/path/to/file.json');
![]() |
1
tlerbao 10 天前 ![]() 是 Guzzle 不好用吗?
|
![]() |
2
zhhqiang 10 天前 via Android
屠龙勇士终成恶龙
|
![]() |
3
dajj 10 天前
看起来不错
|
![]() |
4
GoogleQi 10 天前
了解一下 Guzzle ,你这个是有什么优势?
|
6
kakki 10 天前
你是不是在找 Guzzle? 写之前不先搜一下社区?
|
![]() |
10
hello333 10 天前
棒棒哒
|
![]() |
11
iamzcr 9 天前
php 的 http 请求,Guzzle 吊打一切
|
12
way2create 9 天前
哥们你是怎么做到帖子标题内容 代码 回复 3 个画风的
|
13
tg11 OP @way2create 大概是我表达能力比较欠缺,而且我比较心虚吧
|
![]() |
14
sunny1688 8 天前
PhpRequest::get PHP 应该全大写,PHPRequest::get
|