Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

This PHP HTTP Client provides a minimalistic way to perform GET and POST HTTP requests with customizable headers. It allows you to handle JSON data, form-encoded requests, and more, without using cURL.

License

Notifications You must be signed in to change notification settings

phpdevcommunity/php-httpclient

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

3 Commits

Repository files navigation

PHP Http Client

This PHP HTTP Client provides a minimalistic way to perform GET and POST HTTP requests with customizable headers. It allows you to handle JSON data, form-encoded requests, and more, without using cURL.

Installation

You can install this library via Composer. Ensure your project meets the minimum PHP version requirement of 7.4.

composer require phpdevcommunity/php-httpclient

Requirements

  • PHP version 7.4 or higher

Features

  • Supports GET and POST requests.
  • Customize headers for each request.
  • Automatically handles JSON or form-encoded data.
  • Easily configurable base URL for all requests.
  • Includes error handling for invalid URLs and timeouts.

Usage

Basic GET Request

use PhpDevCommunity\HttpClient\HttpClient;
$client = new HttpClient(['base_url' => 'http://example.com']);
// Perform a GET request
$response = $client->get('/api/data');
if ($response->getStatusCode() === 200) {
 echo $response->getBody(); // Raw response body
 print_r($response->bodyToArray()); // JSON decoded response
}

GET Request with Query Parameters

$response = $client->get('/api/search', ['query' => 'test']);

POST Request (Form-Encoded)

$data = [
 'username' => 'testuser',
 'password' => 'secret'
];
$response = $client->post('/api/login', $data);

POST Request (JSON)

$data = [
 'title' => 'Hello World',
 'content' => 'This is a post content'
];
$response = $client->post('/api/posts', $data, true); // `true` specifies JSON content type

Custom Headers

$client = new HttpClient([
 'base_url' => 'http://example.com',
 'headers' => ['Authorization' => 'Bearer your_token']
]);
$response = $client->get('/api/protected');

Helper Functions

To make the HTTP client easier to use, we provide a set of helper functions that allow you to quickly send GET and POST requests without needing to manually instantiate the HttpClient class every time.

Available Helper Functions

1. http_client()

This function creates and returns a new HttpClient instance with the provided configuration options.

$client = http_client([
 'base_url' => 'http://example.com',
 'headers' => ['Authorization' => 'Bearer your_token']
]);

2. http_post()

Use this function to make a POST request with form-encoded data. It sends a request to the given URL with optional data and headers.

$response = http_post('http://example.com/api/login', [
 'username' => 'user123',
 'password' => 'secret'
]);

3. http_post_json()

This function sends a POST request with JSON-encoded data. Useful for APIs expecting JSON input.

$response = http_post_json('http://example.com/api/create', [
 'title' => 'New Post',
 'body' => 'This is the content of the new post.'
]);

4. http_get()

Make a GET request using this function. You can include query parameters and headers as needed.

$response = http_get('http://example.com/api/users', [
 'page' => 1,
 'limit' => 10
]);

Example Usage of Helpers

// Make a GET request
$response = http_get('http://api.example.com/items', ['category' => 'books']);
$data = $response->bodyToArray();
// Make a POST request with form data
$response = http_post('http://api.example.com/login', [
 'username' => 'user123',
 'password' => 'secret'
]);
// Make a POST request with JSON data
$response = http_post_json('http://api.example.com/posts', [
 'title' => 'Hello World',
 'content' => 'This is my first post!'
]);

These helper functions simplify making HTTP requests by reducing the need to manually create and configure the HttpClient for each request.

About

This PHP HTTP Client provides a minimalistic way to perform GET and POST HTTP requests with customizable headers. It allows you to handle JSON data, form-encoded requests, and more, without using cURL.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

AltStyle によって変換されたページ (->オリジナル) /