hilalahmad/http-client

The HttpClient PHP package is a package designed for making HTTP requests using cURL. This package simplifies the process of sending GET, POST, PUT, PATCH, and DELETE requests, along with the ability to set custom headers, to external APIs or web services.

1.0.0 2023-11-07 12:49 UTC

This package is auto-updated.

Last update: 2024-05-07 13:56:53 UTC


README

A PHP package for make a request in easy way.

GitHub issues GitHub stars Total Downloads License

Table of Contents

Installation

You can install this package using Composer, a popular PHP package manager:

composer require hilalahmad/http-client

Features

  • Send GET, POST, PUT, PATCH, and DELETE requests.
  • Customize and set headers for your HTTP requests.
  • Process JSON responses with built-in methods.
  • Handle cURL errors and HTTP request errors gracefully.

Basic Usage

Here's an example of how to use the HttpClient package to send a GET request:

use Hilalahmad\HttpClient\HttpClient;

$url="url of api";

$headers=[
    'Authorization: Bearer your-token', // depend on you 
    'Content-Type: application/json',
]
$response = HttpClient::get($url, $headers);

$datas = HttpClient::getResponse($response);

foreach($datas as $data){
    echo $data->id;
}

Request Types

GET

Here's an example of how to use the HttpClient package to send a GET request:

$response = HttpClient::get($url, $headers);

$datas = HttpClient::getResponse($response);

foreach($datas as $data){
    echo $data->id;
}

POST

Here's an example of how to use the HttpClient package to send a POST request:

$data=[
    'value1'=>'1',
    'value2'=>'2'
];
$response = HttpClient::post($url,$data, $headers);

$datas = HttpClient::getResponse($response);

print_r($datas);

PATCH

Here's an example of how to use the HttpClient package to send a PATCH request:

$data=[
    'value1'=>'1',
    'value2'=>'2'
];
$response = HttpClient::patch($url,$data, $headers);

$datas = HttpClient::getResponse($response);

print_r($datas);

PUT

Here's an example of how to use the HttpClient package to send a PUT request:

$data=[
    'value1'=>'1',
    'value2'=>'2'
];
$response = HttpClient::put($url,$data, $headers);

$datas = HttpClient::getResponse($response);

print_r($datas);

DELETE

Here's an example of how to use the HttpClient package to send a PUT request:

$response = HttpClient::delete($url, $headers);

$datas = HttpClient::getResponse($response);

print_r($datas);

Contribution

If you'd like to contribute to this package or report issues, please check the Github repo for more details.

License

This package is open-source and is licensed under the MIT License.