downshiftorg / wp-http
A psr7-ish, Guzzle-ish, set of request/response classes for WordPress.
Installs: 5 760
Dependents: 0
Suggesters: 0
Security: 0
Stars: 39
Watchers: 3
Forks: 3
Open Issues: 0
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: ^4.7
- symfony/var-dumper: ~2.0
This package is not auto-updated.
Last update: 2024-11-10 06:17:38 UTC
README
Provides a consistent, object-oriented API for making HTTP requests and receiving responses in the WordPress ecosystem. Provides a thin wrapper around the WP_Http
class and then normalizes the return values of the internal WP_Http::request
method to return a Psr7-ish, Guzzle-ish, modern-ish Response
object, which does not exist in WordPress.
Usage
<?php use DownShift\WordPress\Http\Request; $request = new Request(); $response = $request->get('http://api.yolo.com/status'); $response->getStatusCode(); // (int) 200 $response->getBody(); // (string) '{"msg": "You only live once!"}' $response->json(); // (array) ['msg' => 'You only live once!']
You can also send post requests with x-www-form-urlencoded
data like so:
$request->post('http://api.yolo.com/neckbeard', ['foo' => 'bar']);
It also provides a convenience method for posting json data, setting the appropriate Content-Type headers for you and json_encoding the data passed:
$request->postJson('http://api.yolo.com/neckbeard', [ 'foo' => 'bar', 'jim' => 'jam', ]);
Installation
Install the latest version with
$ composer require downshiftorg/wp-http
Tests
$ vendor/bin/phpunit