simsoft/http-client

A simple CURL HTTP Client.

1.0.1 2025-06-29 09:39 UTC

This package is auto-updated.

Last update: 2025-06-29 09:40:39 UTC


README

This is a simple CURL HTTP client implementation. For advance HTTP client, suggest guzzlehttp/guzzle or symfony/http-client

  1. Installation
  2. Basic Usage
  3. Sending Request
  4. Post Request
  5. Set Headers
  6. Set CURL options
  7. Response Object
  8. Advance Usage
    1. Create Custom API Client
    2. Create Custom Response

Install

composer require simsoft/http-client

Basic Usage

require "vendor/autoload.php";

use Simsoft\HttpClient\HttpClient;

$response = (new HttpClient())
     ->withBaseUri('https://domain.com/api/endpoint')
     ->withMethod('GET')
     ->withHeaders(['Authorization' => 'Bearer secret_token'])
     ->query(['foo' => 'bar'])
     ->request();

if ($response->ok()) {
    //{"status": 200, "data": [{"name": "John Doe","gender": "m"},{"name": "Jane Doe","gender": "f"}]}
    echo $response->getAttribute('status') . PHP_EOL;
    echo $response->getAttribute('data.0.name') . PHP_EOL;
    echo $response->getAttribute('data.1.name') . PHP_EOL;
} else {
    // {"errors": {"status": 404, "title": "The resource was not found"}}
    echo $response->getAttribute('errors.status') . PHP_EOL;
    echo $response->getAttribute('errors.title') . PHP_EOL;
}

// Output:
200
John Doe
Jane Doe

Sending Requests

use Simsoft\HttpClient\HttpClient;

$client = new HttpClient();
$client->withBaseUri('https://domain.com/api/endpoint');

$response = $client->get(); // Perform GET request.
$response = $client->get(['foo' => 'bar', 'foo1' => 'bar2']); // Perform GET request with query params. ?foo=bar&foo1=bar2

$response = $client->patch(); // Perform PATCH request.
$response = $client->delete(); // Perform DELETE request.

Post requests

use Simsoft\HttpClient\HttpClient;

$client = new HttpClient();
$client->withBaseUri('https://domain.com/api/endpoint');

$response = $client->formData(['foo' => 'bar'])->post(); // Perform form-data post

$response = $client->urlEncoded(['foo' => 'bar'])->post(); // Perform x-www-form-urlencoded post

$response = $client->raw(json_encode(['foo' => 'bar']))->post(); // Perform raw content post

$response = $client->graphQL('{
   users {
    id
    name
   }
}')->post(); // Perform GraphQL post.

Set Headers

use Simsoft\HttpClient\HttpClient;

$client = new HttpClient();
$client
    ->withBaseUri('https://domain.com/api/endpoint');
    ->withHeaders([
        'Authorization' => 'Bearer {{secret_token}}',
        'Content-Type' => 'application/json',
    ]);

$response = $client->formData(['foo' => 'bar'])->post();

Set CURL options

use Simsoft\HttpClient\HttpClient;

$client = new HttpClient();
$client
    ->withBaseUri('https://domain.com/api/endpoint');
    ->withOptions([
        CURLOPT_SSL_VERIFYHOST => false,
        CURLOPT_SSL_VERIFYPEER => false,
    ]);

$response = $client->formData(['foo' => 'bar'])->post();

Response Object

use Simsoft\HttpClient\HttpClient;

$response = $client = new HttpClient()
               ->withBaseUri('https://domain.com/api/endpoint')
               ->post(['foo' => 'bar']);

print_r($response->getHeaders());
// output
[
    'Content-Type' => 'application/json',
    'Cache-Control' => 'no-cache',
]

echo $response->getHeaderLine('Content-Type'); // output: application/json
echo $response->getStatusCode(); // output: 200.
echo $response->getTotalTime(); // output: 0.0112 micro seconds.

if ($response->ok()) {
    echo $response->getAttribute('data');

    //{"status": 200, "data": [{"name": "John Doe","gender": "m"},{"name": "Jane Doe","gender": "f"}]}
    echo $response->getAttribute('status') . PHP_EOL;       // 200
    echo $response->getAttribute('data.0.name') . PHP_EOL;  // 'John Doe'
    echo $response->getAttribute('data.1.name') . PHP_EOL;  // 'Jane Doe'

    // output all names.
    foreach($response->getAttribute('data.*.name') as $name) {
        echo $name . PHP_EOL;
    }

} elseif ($response->hasError()) {
    echo $response->getMessage() . PHP_EOL;

    // {"errors": {"status": 404, "title": "The resource was not found"}}
    echo $response->getAttribute('errors.status') . PHP_EOL;
    echo $response->getAttribute('errors.title') . PHP_EOL;
}

License

The Simsoft HttpClient is licensed under the MIT License. See the LICENSE file for details