V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
tg11
V2EX  ›  PHP

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

  •  
  •   tg11 · 10 天前 · 1114 次点击

    这是我某天突然想到的,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  
       10 天前   ❤️ 2
    是 Guzzle 不好用吗?
    zhhqiang
        2
    zhhqiang  
       10 天前 via Android
    屠龙勇士终成恶龙
    dajj
        3
    dajj  
       10 天前
    看起来不错
    GoogleQi
        4
    GoogleQi  
       10 天前
    了解一下 Guzzle ,你这个是有什么优势?
    kakki
        6
    kakki  
       10 天前
    你是不是在找 Guzzle? 写之前不先搜一下社区?
    tg11
        7
    tg11  
    OP
       10 天前
    @tlerbao python 习惯这种语法了,反正闲着我也是闲着,没上传过 composer 包,做一次尝试
    tg11
        8
    tg11  
    OP
       10 天前
    @kakki 反正闲着我也是闲着,没上传过 composer 包,做一次尝试
    tg11
        9
    tg11  
    OP
       10 天前
    @GoogleQi 反正闲着我也是闲着,没上传过 composer 包,做一次尝试
    hello333
        10
    hello333  
       9 天前
    棒棒哒
    iamzcr
        11
    iamzcr  
       9 天前
    php 的 http 请求,Guzzle 吊打一切
    way2create
        12
    way2create  
       9 天前
    哥们你是怎么做到帖子标题内容 代码 回复 3 个画风的
    tg11
        13
    tg11  
    OP
       9 天前
    @way2create 大概是我表达能力比较欠缺,而且我比较心虚吧
    sunny1688
        14
    sunny1688  
       7 天前
    PhpRequest::get PHP 应该全大写,PHPRequest::get
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   5355 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 40ms · UTC 07:52 · PVG 15:52 · LAX 00:52 · JFK 03:52
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.