pinkcrab / http
Wrapper around Nyholm\Psr7 library with a few helper methods and a basic emitter. For use in WordPress during ajax calls.
Installs: 2 004
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 1
Requires
- php: >=7.2.0
- nyholm/psr7: ^1.3
- nyholm/psr7-server: ^1.0
Requires (Dev)
- dealerdirect/phpcodesniffer-composer-installer: *
- gin0115/wpunit-helpers: ~1
- php-stubs/wordpress-stubs: ^6.0 || ^5.9
- phpstan/phpstan: ^1.0
- phpunit/phpunit: ^7.0 || ^8.0
- roots/wordpress: ^6.1
- symfony/var-dumper: *
- szepeviktor/phpstan-wordpress: ^1.0
- vlucas/phpdotenv: ^5.4
- wp-coding-standards/wpcs: *
- wp-phpunit/wp-phpunit: ^6.1
- yoast/phpunit-polyfills: ^0.2.0 || ^1.0.0
This package is auto-updated.
Last update: 2024-11-07 10:04:13 UTC
README
Wrapper around Nyholm\Psr7 library with a few helper methods and a basic emitter. For use in WordPress during ajax calls.
Why?
Throughout a few of our modules we need to handle HTTP requests and responses. The WP_HTTP_* classes are great, but PS7 compliant libraries have a lot more to offer.
So this small module acts a wrapper for the Nyholm\Psr7 and Nyholm\Psr7Server libraries and gives a few helper methods. You can easily create and emit either Responses that extend WP_HTTP_RESPONSE or implements ResponseInterface
Examples
Creates a WP_HTTP_Response
<?php use PinkCrab\HTTP\HTTP; use PinkCrab\HTTP\HTTP; $http = new HTTP(); $response = $http->wp_response( ['some_key'=>'some_value'], 200, ['Content-Type' => 'application/json; charset=UTF-8'] ); // OR $response = HTTP_Helper::wp_response( ['some_key'=>'some_value'], 200, ['Content-Type' => 'application/json; charset=UTF-8'] ); // Emit to client $http->emit_response($response);
As both have the same signatures, you can interchange at will. Obviously the PS7 Response has more functionality to fine tune the response.
Creates a PS7 Response
<?php use PinkCrab\HTTP\HTTP; use PinkCrab\HTTP\HTTP; $http = new HTTP(); $response = $http->ps7_response( ['some_key'=>'some_value'], 200, ['Content-Type' => 'application/json; charset=UTF-8'] ); // OR $response = HTTP_Helper::response( ['some_key'=>'some_value'], 200, ['Content-Type' => 'application/json; charset=UTF-8'] ); // Emit to client $http->emit_response($response);
Creates a PS7 Request
<?php use PinkCrab\HTTP\HTTP; use PinkCrab\HTTP\HTTP_Helper; $http = new HTTP(); $request = $http->psr7_request( 'GET', 'https://google.com' ); // OR $request = HTTP_Helper::request( 'GET', 'https://google.com' );
Get ServerRequest fromGlobals
Returns a populated instance of ServerRequestInterface.
<?php use PinkCrab\HTTP\HTTP; use PinkCrab\HTTP\HTTP_Helper; $server = (new HTTP())->request_from_globals(); // OR $server = HTTP_Helper::global_server_request();
Create Stream
The PSR7 HTTP objects work with streams for the body, you can wrap all scalars values which can cast to JSON in a stream.
<?php use PinkCrab\HTTP\HTTP; use PinkCrab\HTTP\HTTP_Helper; $stream = (new HTTP())->stream_from_scalar($data); // OR $stream = HTTP_Helper::stream_from_scalar($data);
The
(new HTTP())->create_stream_with_json()
has been marked as deprecated since 0.2.3. use(new HTTP())->stream_from_scalar()
in its place.
License
MIT License
http://www.opensource.org/licenses/mit-license.html
Change Log
- 1.0.0 - Removed HTTP::create_stream_with_json()
- 0.2.6 - Readme changes
- 0.2.5 - Removed object type hint for param in emit_response
- 0.2.4 - Typo on scalar (all typed as scala)
- 0.2.3 - Added in
HTTP_Helper
class, patchedServerRequest
fromGloabls
to include the raw $_POST in its body. - 0.2.2 - Added the helper for wrapping data as json in Stream
- 0.2.1 - Removed die() from end of Emit calls and just returned back void. Die to happen at other end
- 0.2.0 - Moved from Guzzle being injected in constructor to using custom HTTP (pink crab). Plug move to composer format.