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

推荐订阅源

雷峰网
雷峰网
T
The Blog of Author Tim Ferriss
WordPress大学
WordPress大学
V
V2EX
Jina AI
Jina AI
S
Schneier on Security
Cyberwarzone
Cyberwarzone
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
PCI Perspectives
PCI Perspectives
美团技术团队
小众软件
小众软件
L
LangChain Blog
N
Netflix TechBlog - Medium
大猫的无限游戏
大猫的无限游戏
T
Threatpost
T
Tor Project blog
K
Kaspersky official blog
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Security Latest
Security Latest
H
Heimdal Security Blog
N
News and Events Feed by Topic
T
Threat Research - Cisco Blogs
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
Tailwind CSS Blog
C
CXSECURITY Database RSS Feed - CXSecurity.com
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
M
MIT News - Artificial intelligence
Apple Machine Learning Research
Apple Machine Learning Research
N
News | PayPal Newsroom
I
Intezer
博客园 - 聂微东
U
Unit 42
Cisco Talos Blog
Cisco Talos Blog
量子位
T
The Exploit Database - CXSecurity.com
Last Week in AI
Last Week in AI
博客园_首页
月光博客
月光博客
Webroot Blog
Webroot Blog
I
InfoQ
The Cloudflare Blog
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
Project Zero
Project Zero
Hacker News: Ask HN
Hacker News: Ask HN
IT之家
IT之家
Google DeepMind News
Google DeepMind News
C
Cisco Blogs

PHP

免费虚拟主机空间安装的一个 PHP 文件论坛 - V2EX 有没有什么好用的免费开源 CRM 系统 - V2EX 2026 年 PHP 路在何方 - V2EX 2026 年,学 laravel 有没有前途: - V2EX 有没有这样的 PHP 框架 - V2EX PHP 是不是快死了 - V2EX php74-fpm 加 nginx, PHP 文件只要不是 root /var/www/html 就提示 No input file specified.求大佬看看 - V2EX PHP framewok 框架推荐 - V2EX 基于 yii2 框架开发的后台管理软件,免费 - V2EX 「一键部署你的专属服务器」——WNMP 一键包,让 Web 环境搭建回归简单 - V2EX 完蛋了,为什么我感觉 PHP 的语法这么优雅呢 - V2EX 未来属于 PHP - V2EX 记一次微信 access_token invalid credential, access_token is invalid or not latest - V2EX 重造 PHP -HTTP 性能检测,新增 List<int>、HashMap<K, V> - V2EX 求助: PHP 错误,请高手帮我改写下面的 PHP 代码 - V2EX 为什么现在 WEB3、区块链、钱包之类开始用 PHP 了 - V2EX PHP 8.5 加入了 pipe 语法 - V2EX 大家好,我又來了,新作品 https://www.freetalkhub.com, PHP 开发 - V2EX laravel 和 thinkphp 选择哪个? - V2EX 202505 最新调研: PHP Opcode 加密混淆哪家强? - V2EX 有做过基于 webrtc 技术的多人视频会议系统的吗 - V2EX 2025 年 PHP 路在何方 - V2EX 求助 CodeIgniter 输出的 html 格式很乱,缩进乱七八糟的。。。 - V2EX 有个关于 PHP 的小疑惑 - V2EX workman 与阿里云实时语音通信的问题。 - V2EX 目前 PHP 中比较好用的工作流引擎有哪些? - V2EX 有老哥熟悉 OpenCart 的吗? - V2EX 开源 PHP Composer 私有 package 管理平台 - V2EX 推荐个好用的集成环境 - V2EX phpinfo(); logo 变成了绿色的大象 - V2EX PHP 项目有类似前端 monorepo 的概念吗? 想用 PHP 做微服务开发,有偿求指导 - V2EX PHP 语言已经过气了吗 - V2EX 国内 PHP 卷王是鸟哥 mysql update 更新失败的原因。 - V2EX PHP 程序员是往运维还是前端发展作为拓展呢 - V2EX 求大佬解决一下一个问题 - V2EX [2024-11-21] PHP 8.4 Released! - V2EX 如何获得抖音视频播放量?各位大牛支支招~ 如何优雅地切换 composer 镜像 - V2EX 与银行对接 sm4 国密算法 - V2EX Laravel 二手项目,语言切换问题,求解 - V2EX 两道 PHP 题目,都是求 flag 值 帮忙推荐一个开源论坛源码? Laravel 完成了 A 轮融资🎉 - V2EX PHP 虽然没落了,但是 PHP 的东西是真的好用 - V2EX 世界上最好的 serverless 平台! - V2EX imgur 是怎么做到一个链接新窗口打开 302,但是还能作为图片显示的? - V2EX PHP 编译器 BPC 编译 ThinkPHP8 + PHPUnit 测试 的视频来了 - V2EX
推荐一个优雅的 PHP http 请求工具,仿照了 py 的 requests - V2EX
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');