PHPackages                             singiu/http-php - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. singiu/http-php

ActiveLibrary[HTTP &amp; Networking](/categories/http)

singiu/http-php
===============

1.0.1(8y ago)12.8k↓50%3MITPHP

Since Mar 22Pushed 8y ago1 watchersCompare

[ Source](https://github.com/Singiu/http-php)[ Packagist](https://packagist.org/packages/singiu/http-php)[ RSS](/packages/singiu-http-php/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (1)Versions (3)Used By (3)

PHP发送HTTP请求及处理响应的轻量组件
=====================

[](#php发送http请求及处理响应的轻量组件)

[![Latest Stable Version](https://camo.githubusercontent.com/affe6f74e50db07226c824f764a9159d5687dff4169a6679cfbdf9d53172c229/68747470733a2f2f706f7365722e707567782e6f72672f73696e6769752f687474702d7068702f762f737461626c65)](https://packagist.org/packages/singiu/http-php)[![Total Downloads](https://camo.githubusercontent.com/249ad6647a9816d1f4539589c8d7b14d8c1163fb9a35a73ee2f6545f13ee225f/68747470733a2f2f706f7365722e707567782e6f72672f73696e6769752f687474702d7068702f646f776e6c6f616473)](https://packagist.org/packages/singiu/http-php)[![Latest Unstable Version](https://camo.githubusercontent.com/b36f6b4df41f9ff06c50e5eabfc65aa9ba2f774b611a4e14e382befe9eb88429/68747470733a2f2f706f7365722e707567782e6f72672f73696e6769752f687474702d7068702f762f756e737461626c65)](https://packagist.org/packages/singiu/http-php)[![License](https://camo.githubusercontent.com/0f246572c736e41b4a236d3cd48cbf1585f5359ead0761a2ac23b4df253c0667/68747470733a2f2f706f7365722e707567782e6f72672f73696e6769752f687474702d7068702f6c6963656e7365)](https://packagist.org/packages/singiu/http-php)[![composer.lock](https://camo.githubusercontent.com/55b372681817e272216d00434b47bd53cb0261aad3ad9447ff8a439b90d8b8ae/68747470733a2f2f706f7365722e707567782e6f72672f73696e6769752f687474702d7068702f636f6d706f7365726c6f636b)](https://packagist.org/packages/singiu/http-php)

这个是用在我自己开发的其它组件包中的一个包，因为多个组件都有用到，所以我把它提取了出来，做成一个独立的包。 源码只是对 php-curl 模块方法的简单封装，功能非常轻量，如果你觉得合适，欢迎拿去使用。如果有使用上的问题或者 Bug，也欢迎提交到 Issues 中来。

现已支持 RESTful 请求方法。

安装
--

[](#安装)

可以通过 Composer 安装：

```
composer require singiu/http-php
```

基本用法
----

[](#基本用法)

### 发送请求

[](#发送请求)

#### 发送 GET 请求。

[](#发送-get-请求)

```
use Singiu\Http\Http;

Http::setBaseUrl('http://localhost');

$response = Http::get('/api/user');

$response = Http::get('api/user', [ // 最开头的 "/" 不要也可以。
    // 发送请求参数（当然你也可以自己拼接在 URL 的尾部）：
    'query' => [
        'page' => 2
    ]
]);

$result = $response->getResponseObject();
echo $result->username;
```

#### 发送 POST 请求。

[](#发送-post-请求)

```
use Singiu\Http\Http;

Http::setBaseUrl('http://localhost');

$response = Http:post('/api/user', [
    // 发送数据，这里和 GET 方法有些不一样，因为考虑到有些请求需要 GET 和 POST 同时传参的情况。
    'data' => [
        'username' => 'singiu',
        'phone' => 13838389438
    ]
]);

echo $response->getResponseText();
```

#### 其它参数：headers、timeout

[](#其它参数headerstimeout)

```
use Singiu\Http\Http;

$response = Http:get('/api/user', [
    // timeout 设置请求连接等待超时时间，单位为秒；headers 可以设置请求头信息。
    'timeout' => 30,
    'headers' => [
        'Content-Type' => 'application/json'
    ]
]);

$result = $response->getResponseArray();
$result['username'];
```

#### 发送REST请求方法。

[](#发送rest请求方法)

PUT、DELETE、OPTIONS、HEAD、PATCH 这类请求方法与 GET 类似。 需要注意的是，所有的方法都可以传入 'data' 参数来设定要传输的数据，但是除了 POST 请求可以在服务端使用 `$_POST` 全局变量来获取外，其它请求方法都不能正常获取，只能通过 `get_file_content('php://input')` 来获取并且需要用户自己解析。

### 处理响应

[](#处理响应)

Singiu\\Http\\Http::post 方法 和 Singiu\\Http\\Http::get 方法返回的是一个 Singiu\\Http\\Response 类的实例对象。 它现在包含以下方法：

```
$response->getUrl();            // 获取该响应的请求 URL。
$response->getStatusCode();     // 获取该响应的 HTTP 响应码。
$response->getResponseText();   // 以文本格式获取响应的内容。
$response->getResponseArray();  // 如果返回的文本是正确的 JSON 格式，则会返回一个解析后的数组。
$response->getResponseObject(); // 如果返回的文本是正确的 JSON 格式，则会返回一个解析后的对象。
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

33

—

LowBetter than 75% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity23

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~5 days

Total

2

Last Release

2975d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/68110d35506ef416f842839b6c94aae0259a6b81c676e27f903da9052d5a68d4?d=identicon)[Singiu](/maintainers/Singiu)

---

Top Contributors

[![Singiu](https://avatars.githubusercontent.com/u/3905448?v=4)](https://github.com/Singiu "Singiu (7 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/singiu-http-php/health.svg)

```
[![Health](https://phpackages.com/badges/singiu-http-php/health.svg)](https://phpackages.com/packages/singiu-http-php)
```

###  Alternatives

[friendsofsymfony/rest-bundle

This Bundle provides various tools to rapidly develop RESTful API's with Symfony

2.8k73.3M319](/packages/friendsofsymfony-rest-bundle)[php-http/discovery

Finds and installs PSR-7, PSR-17, PSR-18 and HTTPlug implementations

1.3k309.5M1.2k](/packages/php-http-discovery)[pusher/pusher-php-server

Library for interacting with the Pusher REST API

1.5k94.8M293](/packages/pusher-pusher-php-server)[react/http

Event-driven, streaming HTTP client and server implementation for ReactPHP

78026.4M414](/packages/react-http)[php-http/curl-client

PSR-18 and HTTPlug Async client with cURL

48347.0M384](/packages/php-http-curl-client)[smi2/phpclickhouse

PHP ClickHouse Client

84310.1M71](/packages/smi2-phpclickhouse)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
