maheshwaghmare / wp-dev-remote-request
缓存HTTP请求,并在指定过期时间内存储到瞬态中,以避免远程请求。
Requires
- php: >=5.3.0
- composer/installers: ~1.0
Requires (Dev)
- phpunit/phpunit: 5.7.16|6.*
- squizlabs/php_codesniffer: 3.*
- wp-coding-standards/wpcs: ^2.0
This package is auto-updated.
Last update: 2024-09-21 22:16:40 UTC
README
"WP Dev Remote Request" 包提供了 wp_dev_remote_request_get()
函数,允许我们发送HTTP请求并将响应存储到瞬态中。
因此,如果我们重新发送请求,它将服务瞬态中的数据。
如果瞬态过期,则触发实时请求并将数据存储到瞬态中,然后返回响应。
内部使用 wp_remote_*() 函数发送远程请求。
语法
wp_dev_remote_request_get( string / array() );
以下是快速示例
// Example 1: $response = wp_dev_remote_request_get( 'https://maheshwaghmare.com/wp-json/wp/v2/posts/' ); // Example 2: $response = wp_dev_remote_request_get( 'https://maheshwaghmare.com/wp-json/wp/v2/posts/?_fields=title' ); // Example 3: $response = wp_dev_remote_request_get( 'https://maheshwaghmare.com/wp-json/wp/v2/posts/?per_page=5&_fields=title' ); // Example 4: $response = wp_dev_remote_request_get( array( 'url' => 'https://maheshwaghmare.com/wp-json/wp/v2/posts/', ) ); // Example 5: $response = wp_dev_remote_request_get( array( 'url' => 'https://maheshwaghmare.com/wp-json/wp/v2/posts/?_fields=title', ) ); // Example 6: $response = wp_dev_remote_request_get( array( 'url' => 'https://maheshwaghmare.com/wp-json/wp/v2/posts/', 'query_args' => array( 'per_page' => 5, '_fields' => 'title', ) ) );
参数
以下是默认参数列表
'url' => '', 'query_args' => array(), 'remote_args' => array( 'timeout' => 60, ), 'expiration' => MONTH_IN_SECONDS, 'force' => false,
它是如何工作的?
该包提供了 wp_dev_remote_request_get()
函数来发送HTTP请求。
// "First" request for URL: https://maheshwaghmare.com/wp-json/wp/v2/posts/ $response = wp_dev_remote_request_get( 'https://maheshwaghmare.com/wp-json/wp/v2/posts/' ); var_dump( $response ); // array( // 'success' => true, // 'message' => 'Response from live site.', // 'data' => array( // ... // ), // ) // "SECOND" request for URL: https://maheshwaghmare.com/wp-json/wp/v2/posts/ $response = wp_dev_remote_request_get( 'https://maheshwaghmare.com/wp-json/wp/v2/posts/' ); var_dump( $response ); // array( // 'success' => true, // 'message' => 'Response from transient.', // 'data' => array( // ... // ), // ) // "Third" request for URL: https://maheshwaghmare.com/wp-json/wp/v2/posts/ $response = wp_dev_remote_request_get( 'https://maheshwaghmare.com/wp-json/wp/v2/posts/' ); var_dump( $response ); // array( // 'success' => true, // 'message' => 'Response from transient.', // 'data' => array( // ... // ), // )
在这里,我们可以看到只有第一次发送的实时HTTP请求,之后的请求将返回瞬态中的缓存响应。
在上面的例子中,我们将 https://maheshwaghmare.com/wp-json/wp/v2/posts
作为参数传递给 wp_dev_remote_request_get()
函数;
让我们看看另一个带有附加参数的例子。
如何将请求视为唯一的请求?
远程请求数据存储在瞬态中。
因此,在将数据存储到瞬态时,它会通过HTTP请求URL创建一个唯一的瞬态键。
例如:
wp_dev_remote_request_get( 'https://maheshwaghmare.com/wp-json/wp/v2/posts/' ); // Unique request.
wp_dev_remote_request_get( 'https://maheshwaghmare.com/wp-json/wp/v2/posts/?per_page=10' ); // Unique request.
wp_dev_remote_request_get( 'https://maheshwaghmare.com/wp-json/wp/v2/posts/?per_page=5' ); // Unique request.
wp_dev_remote_request_get( 'https://maheshwaghmare.com/wp-json/wp/v2/posts/?per_page=5&_field=title' ); // Unique request.
wp_dev_remote_request_get( 'https://maheshwaghmare.com/wp-json/wp/v2/posts/?_field=title&per_page=5' ); // Still unique request.
安装
使用Composer安装
使用以下命令使用Composer安装包
composer require maheshwaghmare/wp-dev-remote-request
安装包后,简单地使用 wp_dev_remote_request_get()
函数。
例如:
// Load files. require_once 'vendor/autoload.php'; // "First" request for URL: https://maheshwaghmare.com/wp-json/wp/v2/posts/ $response = wp_dev_remote_request_get( 'https://maheshwaghmare.com/wp-json/wp/v2/posts/' ); var_dump( $response ); // array( // 'success' => true, // 'message' => 'Response from live site.', // 'data' => array( // ... // ), // ) // "SECOND" request for URL: https://maheshwaghmare.com/wp-json/wp/v2/posts/ $response = wp_dev_remote_request_get( 'https://maheshwaghmare.com/wp-json/wp/v2/posts/' ); var_dump( $response ); // array( // 'success' => true, // 'message' => 'Response from transient.', // 'data' => array( // ... // ), // ) // "Third" request for URL: https://maheshwaghmare.com/wp-json/wp/v2/posts/ $response = wp_dev_remote_request_get( 'https://maheshwaghmare.com/wp-json/wp/v2/posts/' ); var_dump( $response ); // array( // 'success' => true, // 'message' => 'Response from transient.', // 'data' => array( // ... // ), // )
访问 https://packagist.org.cn/packages/maheshwaghmare/wp-dev-remote-request
调试
如果您启用了调试日志并尝试发送请求,您可以在 debug.log
文件中看到日志。
以下是上述代码的日志
[21-Jan-2021 13:42:48 UTC] REQUEST URL: https://maheshwaghmare.com/wp-json/wp/v2/posts/
[21-Jan-2021 13:42:48 UTC] ARGS: {"url":"https:\/\/maheshwaghmare.com\/wp-json\/wp\/v2\/posts\/","query_args":[],...
[21-Jan-2021 13:42:48 UTC] TRANSIENT_KEY: wp-dev-remote-request-bd2bab32e19a4d142e99051fcda7f4e7
[21-Jan-2021 13:42:52 UTC] RESULT: (Live) [{"id":37012,"date":"2021-01-18T23:33:10","date_gmt":...
[21-Jan-2021 13:42:53 UTC] MESSAGE: Response from live site.
[21-Jan-2021 13:42:53 UTC] DURATION: 5 seconds
[21-Jan-2021 13:42:53 UTC] REQUEST URL: https://maheshwaghmare.com/wp-json/wp/v2/posts/
[21-Jan-2021 13:42:53 UTC] ARGS: {"url":"https:\/\/maheshwaghmare.com\/wp-json\/wp\/v2\/posts\/","query_args":[],...
[21-Jan-2021 13:42:53 UTC] TRANSIENT_KEY: wp-dev-remote-request-bd2bab32e19a4d142e99051fcda7f4e7
[21-Jan-2021 13:42:53 UTC] RESULT: (Cached) [{"id":37012,"date":"2021-01-18T23:33:10","date_gmt":...
[21-Jan-2021 13:42:53 UTC] MESSAGE: Response from transient.
[21-Jan-2021 13:42:53 UTC] DURATION: 1 second
[21-Jan-2021 13:42:53 UTC] REQUEST URL: https://maheshwaghmare.com/wp-json/wp/v2/posts/
[21-Jan-2021 13:42:53 UTC] ARGS: {"url":"https:\/\/maheshwaghmare.com\/wp-json\/wp\/v2\/posts\/","query_args":[],"remote_args":{"timeout":60},"expiration":2592000,"force":false,"start_time":1611236573,"end_time":1611236573,"duration":0}
[21-Jan-2021 13:42:53 UTC] TRANSIENT_KEY: wp-dev-remote-request-bd2bab32e19a4d142e99051fcda7f4e7
[21-Jan-2021 13:42:53 UTC] RESULT: (Cached) [{"id":37012,"date":"2021-01-18T23:33:10","date_gmt":...
[21-Jan-2021 13:42:53 UTC] MESSAGE: Response from transient.
[21-Jan-2021 13:42:53 UTC] DURATION: 1 second
在这里,我们可以看到第一次请求因为发送了实时请求而花费了 5秒。
但是,第二次和第三次请求只花费了 1秒,因为它们从瞬态中获取了响应。