perf/http-client

Allows to execute HTTP queries.

2.0.0 2020-07-03 13:28 UTC

This package is auto-updated.

Last update: 2024-05-13 10:29:08 UTC


README

Simple HTTP client using cURL internally.

Installation & Requirements

There are no dependencies on other libraries.

Install with Composer:

composer require perf/http-client

Usage

Simple GET request

<?php

use perf\HttpClient\HttpClient;

$httpClient = HttpClient::createDefault();

$request = $httpClient->createRequest();
$request
    ->methodGet()
    ->setUrl('http://localhost/index.html')
;

$response = $httpClient->execute($request);

$httpStatusCode = $response->getHttpStatusCode();
$content        = $response->getBodyContent();

Simple POST request

<?php

use perf\HttpClient\HttpClient;

$httpClient = HttpClient::createDefault();

$request = $httpClient->createRequest();
$request
    ->methodPost(
        [
            'title'   => 'test article',
            'content' => 'article content ...',
            'photo'   => $httpClient->createFile('/path/to/file.jpg')
        ]
    )
    ->setUrl('http://localhost/create-article.php')
;

$response = $httpClient->execute($request);

$httpStatusCode = $response->getHttpStatusCode();
$content        = $response->getBodyContent();