PHPackages                             jmcomponents/http - 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. jmcomponents/http

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

jmcomponents/http
=================

Library

v1.0.2(1y ago)14MITPHPPHP &gt;=8.1

Since Nov 22Pushed 1y ago2 watchersCompare

[ Source](https://github.com/JMComponents/Http)[ Packagist](https://packagist.org/packages/jmcomponents/http)[ RSS](/packages/jmcomponents-http/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)Dependencies (1)Versions (4)Used By (0)

JMComponents HTTP Library
=========================

[](#jmcomponents-http-library)

[![Packagist Version](https://camo.githubusercontent.com/59423201f55d75c1ff772fe273df397a29d7abaa286f3d3f2cfb4645c1c60578/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6d636f6d706f6e656e74732f687474702e737667)](https://camo.githubusercontent.com/59423201f55d75c1ff772fe273df397a29d7abaa286f3d3f2cfb4645c1c60578/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6a6d636f6d706f6e656e74732f687474702e737667)[![Tests](https://github.com/jmcomponents/http/actions/workflows/ci.yml/badge.svg)](https://github.com/jmcomponents/http/actions/workflows/ci.yml/badge.svg)[![GitHub Release](https://camo.githubusercontent.com/cf37a4618a68ba639ce8511fdcad18ef119bd43828ddfc4181e4496004789954/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6a6d636f6d706f6e656e74732f687474702e737667)](https://camo.githubusercontent.com/cf37a4618a68ba639ce8511fdcad18ef119bd43828ddfc4181e4496004789954/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f762f72656c656173652f6a6d636f6d706f6e656e74732f687474702e737667)[![Packagist Downloads](https://camo.githubusercontent.com/80d4fadd28a859b86d753fe937a368aa642e3748d0527dc0c2802329ff1222e4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a6d636f6d706f6e656e74732f687474702e737667)](https://camo.githubusercontent.com/80d4fadd28a859b86d753fe937a368aa642e3748d0527dc0c2802329ff1222e4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f6a6d636f6d706f6e656e74732f687474702e737667)[![Packagist License](https://camo.githubusercontent.com/fd3a4017b93cf7b596ce7ac939787016e58b387b1daba930a965da3f76ecaed9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a6d636f6d706f6e656e74732f687474702e737667)](https://camo.githubusercontent.com/fd3a4017b93cf7b596ce7ac939787016e58b387b1daba930a965da3f76ecaed9/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6a6d636f6d706f6e656e74732f687474702e737667)

A lightweight PHP library for making HTTP requests. This library provides a simple way to build HTTP requests, set headers, and handle different content types (JSON, XML, Form Data). It uses a fluent interface through a RequestBuilder class to make it easy to create and send requests.

Installation
------------

[](#installation)

You can install the library using Composer. Run the following command in your project directory:

```
composer require jmcomponents/http
```

Usage
-----

[](#usage)

### RequestBuilder

[](#requestbuilder)

The `RequestBuilder` class allows you to construct HTTP requests easily by chaining methods.

#### Example 1: Sending a JSON request

[](#example-1-sending-a-json-request)

```
use JMComponents\Http\Builders\RequestBuilder;

$builder = new RequestBuilder();
$request = $builder
    ->setMethod('POST')
    ->setUrl('https://example.com')
    ->addHeader('Content-Type', 'application/json')
    ->setJsonBody(['key' => 'value'])
    ->build();

// Send the request using your HTTP client (e.g., Guzzle, cURL, etc.)
```

#### Example 2: Sending XML request

[](#example-2-sending-xml-request)

```
use JMComponents\Http\Builders\RequestBuilder;

$builder = new RequestBuilder();
$request = $builder
    ->setMethod('POST')
    ->setUrl('https://example.com')
    ->addHeader('Content-Type', 'application/xml')
    ->setXmlBody('value')
    ->build();

// Send the request
```

#### Example 3: Sending Form Data request

[](#example-3-sending-form-data-request)

```
use JMComponents\Http\Builders\RequestBuilder;

$builder = new RequestBuilder();
$request = $builder
    ->setMethod('POST')
    ->setUrl('https://example.com')
    ->addHeader('Content-Type', 'application/x-www-form-urlencoded')
    ->setFormData(['key' => 'value'])
    ->build();

// Send the request
```

#### Complete Example: HTTP request using `HttpClient`

[](#complete-example-http-request-using-httpclient)

This is a complete example of how to make an HTTP request using the `RequestBuilder` to build the request and `HttpClient` to send it. Subsequently, we process the response obtained.

### Step 1: Create the request

[](#step-1-create-the-request)

First, we create the request using the `RequestBuilder`. We set the HTTP method, the URL, the headers and the body of the request.

```
use JMComponents\Http\Builders\RequestBuilder;
use JMComponents\Http\HttpClient;

// Create the RequestBuilder
$builder = new RequestBuilder();

// Build the request
$request = $builder
    ->setMethod('POST') // HTTP Method
    ->setUrl('https://jsonplaceholder.typicode.com/posts') // Destination URL
    ->setHeaders([ // Headers
        'Content-Type' => 'application/json',
        'Authorization' => 'Bearer ' // Authorization header
    ])
    ->setJsonBody([ // JSON Body
        'title' => 'foo',
        'body' => 'bar',
        'userId' => 1
    ])
    ->build();
```

### Step 2: Send the request and get the response

[](#step-2-send-the-request-and-get-the-response)

Once we have the request built, we use the `HttpClient` to send it. The `HttpClient` is responsible for sending the request and receiving the response.

```
// Create the HttpClient
$client = new HttpClient();

// Send the request and get the answer
$response = $client->send($request);

// Show the body of the response
echo $response->getBody();
```

### Step 3: Process the response

[](#step-3-process-the-response)

The response can be processed in different ways. In this example, we show how to convert the JSON response body to an associative array and an object.

#### Convert the response to an array

[](#convert-the-response-to-an-array)

If the response has a JSON body, we can easily convert it to an array:

```
try {
    $responseArray = $response->toArray(); // Converts the JSON response to an associative array
    print_r($responseArray);
} catch (\JsonException $e) {
    echo 'Error processing the response: ', $e->getMessage();
}
```

#### Convert the response to an object

[](#convert-the-response-to-an-object)

Similarly, we can convert the response to an object:

```
try {
    $responseObject = $response->toObject(); // Converts the JSON response to an object
    var_dump($responseObject);
} catch (\JsonException $e) {
    echo 'Error processing the response: ', $e->getMessage();
}
```

### Step 4: Handle errors

[](#step-4-handle-errors)

It is important to handle possible errors both in the request and in the processing of the response. Here's how to check the response status code and handle errors:

```
// Verify the status code of the response
if ($response->getStatusCode() !== 200) {
    echo 'Error: Request failed with status code: ', $response->getStatusCode();
} else {
    echo 'Success: The application was successful.';
}
```

This example is a more structured way to make an HTTP request from scratch (using `RequestBuilder`), send it (with `HttpClient`), and handle the response. Make sure the code is well documented and looks clear to the end user who will implement it.

Features
--------

[](#features)

- **JSON Body**: Easily send JSON data by using `setJsonBody()`.
- **XML Body**: Send XML data using `setXmlBody()`.
- **Form Data**: Submit form data with `setFormData()`.
- **Headers**: Add custom headers to your requests.
- **RequestBuilder**: Chainable methods for building HTTP requests fluently.

Methods
-------

[](#methods)

### RequestBuilder Class

[](#requestbuilder-class)

- **setMethod(string $method)**: Set the HTTP method (e.g., `GET`, `POST`, `PUT`).
- **setUrl(string $url)**: Set the URL for the request.
- **addHeader(string $name, string $value)**: Add a custom header.
- **setJsonBody(array $data)**: Set the request body as JSON.
- **setXmlBody(string $xml)**: Set the request body as XML.
- **setFormData(array $data)**: Set the request body as form data.
- **build()**: Builds and returns the final `Request` object.

### Request Class

[](#request-class)

- **getMethod()**: Get the HTTP method of the request.
- **getUrl()**: Get the URL of the request.
- **getHeaders()**: Get the headers of the request.
- **getBody()**: Get the body of the request (can be an array or string).
- **toJson()**: Convert the request body to JSON.

### Response Class

[](#response-class)

- **getStatusCode()**: Get the status code of the response.
- **getBody()**: Get the body of the response.
- **toArray()**: Convert the response body to an associative array.
- **toObject()**: Convert the response body to an object.

Contributing
------------

[](#contributing)

Feel free to fork this repository and submit pull requests. Please make sure your code follows the coding standards and includes tests for any new features.

License
-------

[](#license)

This library is open-source and available under the [MIT License](LICENSE).

###  Health Score

27

—

LowBetter than 49% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity5

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity49

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 81.8% 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 ~2 days

Total

3

Last Release

538d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/cf27f25cf094792bf0c736bf5845ae1692f49f8b485ee620df8e35e38f7668c2?d=identicon)[JoshuaMc1](/maintainers/JoshuaMc1)

---

Top Contributors

[![JoshuaMc1](https://avatars.githubusercontent.com/u/86990609?v=4)](https://github.com/JoshuaMc1 "JoshuaMc1 (9 commits)")[![JMComponents](https://avatars.githubusercontent.com/u/159521107?v=4)](https://github.com/JMComponents "JMComponents (2 commits)")

---

Tags

composer-libraryhttphttp-requestsphpphp-libraryphpunit-testsrequests

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  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

83510.1M71](/packages/smi2-phpclickhouse)

PHPackages © 2026

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