tg11
V2EX  ›  PHP

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

  •  
  •   tg11 · Aug 25, 2025 · 4710 views
    This topic created in 439 days ago, the information mentioned may be changed or developed.

    这是我某天突然想到的,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
    tlerbao
    Aug 25, 2025❤️3
    1
    是 Guzzle 不好用吗?
    zhhqiang
    zhhqiang
    Aug 25, 2025 via Android
    2
    屠龙勇士终成恶龙
    dajj
    dajj
    Aug 25, 2025
    3
    看起来不错
    GoogleQi
    GoogleQi
    Aug 25, 2025
    4
    了解一下 Guzzle ,你这个是有什么优势?
    kakki
    kakki
    Aug 25, 2025
    6
    你是不是在找 Guzzle? 写之前不先搜一下社区?
    tg11
    tg11
    OP
    Aug 25, 2025
    7
    @tlerbao python 习惯这种语法了,反正闲着我也是闲着,没上传过 composer 包,做一次尝试
    tg11
    tg11
    OP
    Aug 25, 2025
    8
    @kakki 反正闲着我也是闲着,没上传过 composer 包,做一次尝试
    tg11
    tg11
    OP
    Aug 25, 2025
    9
    @GoogleQi 反正闲着我也是闲着,没上传过 composer 包,做一次尝试
    hello333
    hello333
    Aug 25, 2025
    10
    棒棒哒
    iamzcr
    iamzcr
    Aug 26, 2025
    11
    php 的 http 请求,Guzzle 吊打一切
    way2create
    way2create
    Aug 26, 2025
    12
    哥们你是怎么做到帖子标题内容 代码 回复 3 个画风的
    tg11
    tg11
    OP
    Aug 26, 2025
    13
    @way2create 大概是我表达能力比较欠缺,而且我比较心虚吧
    sunny1688
    sunny1688
    Aug 27, 2025
    14
    PhpRequest::get PHP 应该全大写,PHPRequest::get
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2073 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 02:35 · PVG 10:35 · LAX 18:35 · JFK 21:35
    ♥ Do have faith in what you're doing.