pinkcrab / http
Wrapper around Nyholm\Psr7 library with a few helper methods and a basic emitter. For use in WordPress during ajax calls.
Requires
- php: >=8.0.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.9.*
- phpstan/phpstan: ^2.0
- phpunit/phpunit: ^8.0 || ^9.0
- roots/wordpress: 6.9.*
- symfony/var-dumper: *
- szepeviktor/phpstan-wordpress: ^2.0
- vlucas/phpdotenv: ^5.4
- wp-coding-standards/wpcs: *
- wp-phpunit/wp-phpunit: 6.9.*
- yoast/phpunit-polyfills: ^1.0.0 || ^2.0.0
This package is auto-updated.
Last update: 2026-04-18 23:36:54 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
Tested Against
- PHP 8.0, 8.1, 8.2, 8.3 & 8.4
- WP 6.6, 6.7, 6.8 & 6.9
- MySQL 8.4
Change Log
- 1.2.0 - Drop PHP 7.x, require PHP 8.0+. Bump dev deps: phpstan 2.x, phpstan-wordpress 2.x, phpunit 8|9, WP 6.6-6.9 test matrix. Retire the
WP_6_2/WP_6_3/WP_6_4/WP_6_5workflows and addWP_6_6/WP_6_7/WP_6_8/WP_6_9(PHP 8.0-8.4,mysql:8.4). Standardise.scrutinizer.yml. Add curl-alternative and PSR12 file-header excludes tophpcs.xml. Suppress the WP 6.8wp_is_block_themeearly-call E_USER_NOTICE intests/wp-config.phpunder PHPUnit's notice-to-exception conversion. Fill in descriptions on every bare@param/@return/@throwsacrosssrc/. No public API changes. - 1.1.0 - Add nullable type hint to the reason phrase in the response and update dev dependencies
- 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_Helperclass, patchedServerRequestfromGloablsto 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.