downshiftorg/wp-http

A psr7-ish, Guzzle-ish, set of request/response classes for WordPress.

2.0.0 2018-01-15 16:03 UTC

This package is not auto-updated.

Last update: 2024-12-22 06:47:40 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