title: "WP_CLI\Utils\http_request()" post_status: publish comment_status: open taxonomy: category: - wp-cli-handbook post_tag: - Internal Api - Repos - Data
WP_CLI\Utils\http_request()
Make a HTTP request to a remote URL.
Usage
WP_CLI\Utils\http_request( $method, $url, $data = null, $headers = [], $options = [] )
$method (string) HTTP method (GET, POST, DELETE, etc.).
$url (string) URL to make the HTTP request to.
$data (array|null) Data to send either as a query string for GET/HEAD requests,
or in the body for POST requests.
$headers (array) Add specific headers to the request.
$options (array) {
Optional. An associative array of additional request options.
@type bool $halt_on_error Whether or not command execution should be halted on error. Default: true
@type bool|string $verify A boolean to use enable/disable SSL verification
or string absolute path to CA cert to use.
Defaults to detected CA cert bundled with the Requests library.
@type bool $insecure Whether to retry automatically without certificate validation.
}
@return (\Requests_Response|Response)
$url (string) URL to make the HTTP request to.
$data (array|null) Data to send either as a query string for GET/HEAD requests,
or in the body for POST requests.
$headers (array) Add specific headers to the request.
$options (array) {
Optional. An associative array of additional request options.
@type bool $halt_on_error Whether or not command execution should be halted on error. Default: true
@type bool|string $verify A boolean to use enable/disable SSL verification
or string absolute path to CA cert to use.
Defaults to detected CA cert bundled with the Requests library.
@type bool $insecure Whether to retry automatically without certificate validation.
}
@return (\Requests_Response|Response)
Notes
Wraps the Requests HTTP library to ensure every request includes a cert.
# `wp core download` verifies the hash for a downloaded WordPress archive
$md5_response = Utils\http_request( 'GET', $download_url . '.md5' );
if ( 20 != substr( $md5_response->status_code, 0, 2 ) ) {
WP_CLI::error( "Couldn't access md5 hash for release (HTTP code {$response->status_code})" );
}
Internal API documentation is generated from the WP-CLI codebase on every release. To suggest improvements, please submit a pull request.
相关
- WP_CLI\Utils\write_csv() - 将数据以 CSV 格式写入指定文件。
- WP_CLI\Utils\get_named_sem_ver() - 比较两个版本字符串以获取命名的语义化版本。
- WP_CLI\Utils\parse_ssh_url() - 解析 SSH URL 以获取其主机、端口和路径。
- WP_CLI\Utils\basename() - 与区域设置无关的 basename() 版本
- WP_CLI\Utils\isPiped() - 检查当前脚本的输出是 TTY 还是管道/重定向
- WP_CLI\Utils\proc_open_compat() - Windows 兼容的 `proc_open()`。解决 PHP 中的错误,并处理类 Unix 的 `ENV_VAR=blah cmd` 环境变量前缀。
- WP_CLI\Utils\esc_like() - 在为 MySQL 准备之前,转义 LIKE 特殊字符 % 和 _ 的前半部分。