WP_CLI\Utils
http_request()
Make a HTTP request to a remote URL.
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})" );
}
Хуков нет.
Возвращает
Объект.
Использование
http_request( $method, $url, $data, $headers, $options );
- $method(строка) (обязательный)
- HTTP method (GET, POST, DELETE, etc.).
- $url(строка) (обязательный)
- URL to make the HTTP request to.
- $data(массив|null)
- Data to send either as a query string for GET/HEAD requests, or in the body for POST requests.
По умолчанию: null - $headers(массив)
- Add specific headers to the request.
По умолчанию: [] - $options(массив)
An associative array of additional request options.
По умолчанию: []
-
halt_on_error(true|false)
Whether or not command execution should be halted on error. Default: true -
verify(true|false|строка)
A boolean to use enable/disable SSL verification or string absolute path to CA cert to use.
По умолчанию: detected CA cert bundled with the Requests library - insecure(true|false)
Whether to retry automatically without certificate validation.
-
Код http_request() http request WP-CLI 2.13.0-alpha
function http_request( $method, $url, $data = null, $headers = [], $options = [] ) {
$insecure = isset( $options['insecure'] ) && (bool) $options['insecure'];
$halt_on_error = ! isset( $options['halt_on_error'] ) || (bool) $options['halt_on_error'];
unset( $options['halt_on_error'] );
if ( ! isset( $options['verify'] ) ) {
// 'curl.cainfo' enforces the CA file to use, otherwise fallback to system-wide defaults then use the embedded CA file.
$options['verify'] = ! empty( ini_get( 'curl.cainfo' ) ) ? ini_get( 'curl.cainfo' ) : true;
}
$options = WP_CLI::do_hook( 'http_request_options', $options );
RequestsLibrary::register_autoloader();
$request_method = [ RequestsLibrary::get_class_name(), 'request' ];
try {
try {
return $request_method( $url, $headers, $data, $method, $options );
} catch ( \Requests_Exception | \WpOrg\Requests\Exception $exception ) {
if (
true !== $options['verify']
|| 'curlerror' !== $exception->getType()
|| curl_errno( $exception->getData() ) !== CURLE_SSL_CACERT
) {
throw $exception;
}
$options['verify'] = get_default_cacert( $halt_on_error );
return $request_method( $url, $headers, $data, $method, $options );
}
} catch ( \Requests_Exception | \WpOrg\Requests\Exception $exception ) {
if (
! $insecure
||
'curlerror' !== $exception->getType()
||
! in_array( curl_errno( $exception->getData() ), [ CURLE_SSL_CONNECT_ERROR, CURLE_SSL_CERTPROBLEM, CURLE_SSL_CACERT_BADFILE ], true )
) {
$error_msg = sprintf( "Failed to get url '%s': %s.", $url, $exception->getMessage() );
if ( $halt_on_error ) {
WP_CLI::error( $error_msg );
}
throw new RuntimeException( $error_msg, 0, $exception );
}
$warning = sprintf(
"Re-trying without verify after failing to get verified url '%s' %s.",
$url,
$exception->getMessage()
);
WP_CLI::warning( $warning );
// Disable certificate validation for the next try.
$options['verify'] = false;
try {
return $request_method( $url, $headers, $data, $method, $options );
} catch ( \Requests_Exception | \WpOrg\Requests\Exception $exception ) {
$error_msg = sprintf( "Failed to get non-verified url '%s' %s.", $url, $exception->getMessage() );
if ( $halt_on_error ) {
WP_CLI::error( $error_msg );
}
throw new RuntimeException( $error_msg, 0, $exception );
}
}
}