myth21/context-http-requester

dev-main 2023-05-26 10:40 UTC

This package is auto-updated.

Last update: 2024-10-26 13:41:53 UTC


README

Features: Class abstracts the HTTP communication through stream contexts in PHP. It provides convenient methods to send GET, POST, and PUT requests, and set request headers. The class stores request headers and response headers of the last request sent for further inspection. It also has a configuration option to decode the response or not. The class is extensible to implement other HTTP methods as well. One thing that could be improved is the lack of error handling. For example, the code assumes that the file contents of the response will always be a string, which may not be true if there was an error. Another potential concern is that this class may be too simple for some situations. It doesn't provide functionality for customizing the HTTP status code that is sent in the response, setting cookies, or handling redirects, for example. Overall, this code provides a lightweight way to perform simple HTTP requests in PHP, but it may not be suitable for more complex situations.

Requirements

  • PHP >= 8.0
  • PHPUnit >= 9.3

Install

composer require myth21/context-http-requester:dev-main

Usage

Get entity list:

$contextHttpRequester = new ContextHttpRequester();

$contextHttpRequester->sendGetRequest('http://example.com/comments/');

Create entity:

$contextHttpRequester->sendPostRequest('http://example.com/comments/', [
    'id' => null,
    'name' => 'Bob',
    'text' => 'Hello, World',
]);

Update entity:

$contextHttpRequester->sendPutRequest('http://example.com/comments/123/', [
    'name' => 'Alice',
    'text' => 'Hi, everyone',
]);

Testing

Require phpunit.

Optionality you can run server from directory comment_server.php for testing by command:

php -S localhost:8000 -f comment_server.php

Run test, example:

php vendor/bin/phpunit vendor/myth21/context-http-requester/CommentClientTest.php -c vendor/myth21/context-http-requester/example.phpunit.xml --color --do-not-cache-result