netrivet/wp-http

此包已被弃用且不再维护。作者建议使用downshiftorg/wp-http包代替。

为WordPress提供一组类似psr7、Guzzle的请求/响应类。

2.0.0 2018-01-15 16:03 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:48:30 UTC


README

提供了一致的面向对象API,用于在WordPress生态系统中发送HTTP请求和接收响应。它对WP_Http类进行了轻量级包装,并将内部WP_Http::request方法的返回值标准化,以返回类似Psr7、Guzzle的现代Response对象,WordPress中不存在此类对象。

使用方法

<?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!']

您还可以使用如下方式发送带有x-www-form-urlencoded数据的POST请求

$request->post('http://api.yolo.com/neckbeard', ['foo' => 'bar']);

它还提供了一个方便的方法来发送JSON数据,为您设置适当的Content-Type头,并对传递的数据进行json编码

$request->postJson('http://api.yolo.com/neckbeard', [
    'foo' => 'bar',
    'jim' => 'jam',
]);

安装

使用以下命令安装最新版本

$ composer require downshiftorg/wp-http

测试

$ vendor/bin/phpunit