推荐一个优雅的 PHP http 请求工具,仿照了 py 的 requests - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tg11
V2EX    PHP

推荐一个优雅的 PHP http 请求工具,仿照了 py 的 requests

  •  
  •   tg11 45 天前 1701 次点击
    这是一个创建于 45 天前的主题,其中的信息可能已经有所发展或是发生改变。

    这是我某天突然想到的,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'); 
    tlerbao
        1
    tlerbao  
       45 天前   3
    是 Guzzle 不好用吗?
    zhhqiang
        2
    zhhqiang  
       45 天前 via Android
    屠龙勇士终成恶龙
    dajj
        3
    dajj  
       45 天前
    看起来不错
    GoogleQi
        4
    GoogleQi  
       45 天前
    了解一下 Guzzle ,你这个是有什么优势?
    kakki
        6
    kakki  
       45 天前
    你是不是在找 Guzzle? 写之前不先搜一下社区?
    tg11
        7
    tg11  
    OP
       45 天前
    @tlerbao python 习惯这种语法了,反正闲着我也是闲着,没上传过 composer 包,做一次尝试
    tg11
        8
    tg11  
    OP
       45 天前
    @kakki 反正闲着我也是闲着,没上传过 composer 包,做一次尝试
    tg11
        9
    tg11  
    OP
       45 天前
    @GoogleQi 反正闲着我也是闲着,没上传过 composer 包,做一次尝试
    hello333
        10
    hello333  
       45 天前
    棒棒哒
    iamzcr
        11
    iamzcr  
       44 天前
    php 的 http 请求,Guzzle 吊打一切
    way2create
        12
    way2create  
       44 天前
    哥们你是怎么做到帖子标题内容 代码 回复 3 个画风的
    tg11
        13
    tg11  
    OP
       44 天前
    @way2create 大概是我表达能力比较欠缺,而且我比较心虚吧
    sunny1688
        14
    sunny1688  
       43 天前
    PhpRequest::get PHP 应该全大写,PHPRequest::get
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     897 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 19:41 PVG 03:41 LAX 12:41 JFK 15:41
    Do have faith in what you're doing.
    ubao snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86