netrivet / wp-http
2.0.0
2018-01-15 16:03 UTC
Requires
- php: >=5.3.0
Requires (Dev)
- phpunit/phpunit: ^4.7
- symfony/var-dumper: ~2.0
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