pronamic/wp-http

本软件包最新版本(v1.2.3)没有提供许可证信息。

WordPress HTTP 库。

安装次数: 86,554

依赖: 19

建议: 0

安全性: 0

星标: 4

关注者: 6

分支: 2

公开问题: 0

类型:wordpress-plugin

v1.2.3 2024-05-15 13:24 UTC

This package is auto-updated.

Last update: 2024-09-12 14:18:46 UTC


README

WordPress HTTP 库。

用法

$response = Http::request( $url, $args );

$data = $response->json();

测试

模拟响应

$url = 'https://www.pronamic.nl/wp-json/wp/v2/types/post';

Http::fake( $url, __DIR__ . '/../http/pronamic-nl-wp-json-types-post.http' );

$response = \wp_remote_get( $url );

// or

$response = Http::get( $url );

PHPUnit

<?php

namespace YourNamespace;

use Pronamic\WordPress\Http\Factory;

class YourTest extends \WP_UnitTestCase {
	/**
	 * Setup.
	 */
	public function setUp() {
		parent::setUp();

		$this->factory = new Factory();
	}

	/**
	 * Test request.
	 */
	public function test_request() {
		$this->factory->fake( 'http://example.com/', __DIR__ . '/../http/example-com.http' );

		$result = \wp_remote_get( 'http://example.com/' );

		// asserts
	}
}

回车换行

为了将模拟的HTTP响应存储在*.http文件和Git中,请记住以下事项

HTTP/1.1定义了CR LF序列作为所有协议元素的行结束标记

https://tools.ietf.org/html/rfc2616#section-2.2

.gitattributes

*.http text eol=crlf

您可以使用如unix2dos之类的工具将行结束转换为CR LF

unix2dos *.http

如果行结束不正确,可能会导致以下错误

Undefined offset: 2

wordpress/wp-includes/class-http.php:732
src/Factory.php:97
wordpress/wp-includes/class-wp-hook.php:292
wordpress/wp-includes/plugin.php:212
wordpress/wp-includes/class-http.php:257
wordpress/wp-includes/class-http.php:626
wordpress/wp-includes/http.php:162
src/Facades/Http.php:71

灵感

Pronamic - Work with us