Luminova Framework

Network Request

Last updated: 2024-05-09 06:31:20

The Network class is a utility designed to simplify making HTTP requests to external resources such as APIs, web services, or websites. It provides an easy-to-use interface for sending HTTP requests and handling responses, including accessing response headers, status codes, and response bodies.

By default, it uses cURL for making HTTP requests, but it also supports the Guzzle client for those who prefer it.


Methods

Before you dive into the network implementation, also take a look at the response interface Network Response Object.

constructor

Constructor for NetworkInterface.

public __construct(\Luminova\Interface\HttpClientInterface|null $client = null): mixed

Parameters:

ParameterTypeDescription
$client\Luminova\Interface\HttpClientInterface|nullThe HTTP client instance.

getClient

Get the HTTP client instance.

public getClient(): \Luminova\Interface\HttpClientInterface

Return Value:

\Luminova\Interface\HttpClientInterface - The HTTP client instance.


send

Send a request.

public send(string $method, string $url, array $data = [], array $headers = []): \Luminova\Http\Message\Response

Parameters:

ParameterTypeDescription
$methodstringThe HTTP method (GET, POST, etc.).
$urlstringThe URL to send the request to.
$dataarrayThe request body data.
$headersarrayThe request headers.

Return Value:

\Luminova\Http\Message\Response - The response from the request.

Throws:


get

Perform a GET request.

public get(string $url, array $data = [], array $headers = []): \Luminova\Http\Message\Response

Parameters:

ParameterTypeDescription
$urlstringThe URL to send the request to.
$dataarrayThe request body data.
$headersarrayThe request headers.

Return Value:

\Luminova\Http\Message\Response - The response from the request.

Throws:


fetch

Fetch data using a GET request.

public fetch(string $url, array $headers = []): \Luminova\Http\Message\Response

Parameters:

ParameterTypeDescription
$urlstringThe URL to send the request to.
$headersarrayThe request headers.

Return Value:

\Luminova\Http\Message\Response - The response from the request.

Throws:


post

Perform a POST request.

public post(string $url, array $data = [], array $headers = []): \Luminova\Http\Message\Response

Parameters:

ParameterTypeDescription
$urlstringThe URL to send the request to.
$dataarrayThe request body data.
$headersarrayThe request headers.

Return Value:

\Luminova\Http\Message\Response - The response from the request.

Throws:


request

Perform a request.

public request(string $method, string $url, array $data = [], array $headers = []): \Luminova\Http\Message\Response

Parameters:

ParameterTypeDescription
$methodstringThe HTTP method (GET, POST, etc.).
$urlstringThe URL to send the request to.
$dataarrayThe request body data.
$headersarrayThe request headers.

Return Value:

\Luminova\Http\Message\Response - The response from the request.

Throws:


sendAsync

Send a request asynchronously.

public sendAsync(\GuzzleHttp\Psr7\Request $request): \GuzzleHttp\Promise\PromiseInterface

Parameters:

ParameterTypeDescription
$request\GuzzleHttp\Psr7\RequestThe request object to send.

Return Value:

\GuzzleHttp\Promise\PromiseInterface - A promise that resolves with the response.

Examples

Send network request using default Curl client.

<?php 
use \Luminova\Http\Network;
use \Luminova\Exceptions\AppException;
use \Exception;

$network = new Network();

try{
    $response = $network->get('https://example.com/api/foo');
    $statusCode = $response->getStatusCode();
    $content = $response->getContents();
} catch (AppException | Exception $e) {
    echo $e->getMessage();
}

Send a network request using Guzzle.

<?php 
use \Luminova\Http\Network;
use \Luminova\Http\Client\Guzzle;
use \Luminova\Exceptions\AppException;
use \Exception;

$network = new Network(new Guzzle());

try{
    $response = $network->get('https://example.com/api/foo');
    $statusCode = $response->getStatusCode();
    $content = $response->getContents();
} catch (AppException | Exception $e) {
    echo $e->getMessage();
}