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 - $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.8.0-alpha
function http_request( $method, $url, $data = null, $headers = [], $options = [] ) { if ( ! class_exists( 'Requests_Hooks' ) ) { // Autoloader for the Requests library has not been registered yet. Requests::register_autoloader(); } $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; } try { try { return Requests::request( $url, $headers, $data, $method, $options ); } catch ( Requests_Exception $ex ) { if ( true !== $options['verify'] || 'curlerror' !== $ex->getType() || curl_errno( $ex->getData() ) !== CURLE_SSL_CACERT ) { throw $ex; } $options['verify'] = get_default_cacert( $halt_on_error ); return Requests::request( $url, $headers, $data, $method, $options ); } } catch ( Requests_Exception $ex ) { // CURLE_SSL_CACERT_BADFILE only defined for PHP >= 7. if ( ! $insecure || 'curlerror' !== $ex->getType() || ! in_array( curl_errno( $ex->getData() ), [ CURLE_SSL_CONNECT_ERROR, CURLE_SSL_CERTPROBLEM, 77 /*CURLE_SSL_CACERT_BADFILE*/ ], true ) ) { $error_msg = sprintf( "Failed to get url '%s': %s.", $url, $ex->getMessage() ); if ( $halt_on_error ) { WP_CLI::error( $error_msg ); } throw new RuntimeException( $error_msg, null, $ex ); } $warning = sprintf( "Re-trying without verify after failing to get verified url '%s' %s.", $url, $ex->getMessage() ); WP_CLI::warning( $warning ); // Disable certificate validation for the next try. $options['verify'] = false; try { return Requests::request( $url, $headers, $data, $method, $options ); } catch ( Requests_Exception $ex ) { $error_msg = sprintf( "Failed to get non-verified url '%s' %s.", $url, $ex->getMessage() ); if ( $halt_on_error ) { WP_CLI::error( $error_msg ); } throw new RuntimeException( $error_msg, null, $ex ); } } }