惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

I
InfoQ
G
Google Developers Blog
Engineering at Meta
Engineering at Meta
月光博客
月光博客
博客园 - 聂微东
博客园 - Franky
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
有赞技术团队
有赞技术团队
A
About on SuperTechFans
Microsoft Azure Blog
Microsoft Azure Blog
Blog — PlanetScale
Blog — PlanetScale
U
Unit 42
T
Tailwind CSS Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
云风的 BLOG
云风的 BLOG
S
SegmentFault 最新的问题
F
Fortinet All Blogs
H
Help Net Security
J
Java Code Geeks
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 叶小钗
L
LangChain Blog
Martin Fowler
Martin Fowler
N
Netflix TechBlog - Medium

PHP

PHP 是不已经没有公司实际在招了 - V2EX 刚哥开发新的编程语言 TypePHP,周三放到 Github , 3 天 star 就快 900 个了,https://github.com/swoole/typephp,大家用 AI 体验下编程 - V2EX 极致精简,已经优化为仅 100 多 KB!单文件 PHP 论坛! - V2EX 新版六国语言小额 JieDai 系统源码 - V2EX 免费虚拟主机空间安装的一个 PHP 文件论坛 有没有什么好用的免费开源 CRM 系统 2026 年 PHP 路在何方 2026 年,学 laravel 有没有前途: 有没有这样的 PHP 框架 PHP 是不是快死了 php74-fpm 加 nginx, PHP 文件只要不是 root /var/www/html 就提示 No input file specified.求大佬看看 PHP framewok 框架推荐 基于 yii2 框架开发的后台管理软件,免费 「一键部署你的专属服务器」——WNMP 一键包,让 Web 环境搭建回归简单 完蛋了,为什么我感觉 PHP 的语法这么优雅呢 未来属于 PHP 记一次微信 access_token invalid credential, access_token is invalid or not latest 重造 PHP -HTTP 性能检测,新增 List<int>、HashMap<K, V> 求助: PHP 错误,请高手帮我改写下面的 PHP 代码 为什么现在 WEB3、区块链、钱包之类开始用 PHP 了 PHP 8.5 加入了 pipe 语法 大家好,我又來了,新作品 https://www.freetalkhub.com, PHP 开发 laravel 和 thinkphp 选择哪个? 202505 最新调研: PHP Opcode 加密混淆哪家强? 有做过基于 webrtc 技术的多人视频会议系统的吗 2025 年 PHP 路在何方 求助 CodeIgniter 输出的 html 格式很乱,缩进乱七八糟的。。。 有个关于 PHP 的小疑惑 workman 与阿里云实时语音通信的问题。 目前 PHP 中比较好用的工作流引擎有哪些?
推荐一个优雅的 PHP http 请求工具,仿照了 py 的 requests
tg11 · 2025-08-25 · via PHP

这是我某天突然想到的,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'
    ]
]);

session 会话管理

会话允许在多个请求之间持久化 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');