/** * 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})" ); * } * ``` * * @access public * * @param string $method HTTP method (GET, POST, DELETE, etc.) * @param string $url URL to make the HTTP request to. * @param array $headers Add specific headers to the request. * @param array $options * @return object */ function http_request($method, $url, $data = null, $headers = array(), $options = array()) { $cert_path = '/rmccue/requests/library/Requests/Transport/cacert.pem'; if (inside_phar()) { // cURL can't read Phar archives $options['verify'] = extract_from_phar(WP_CLI_ROOT . '/vendor' . $cert_path); } else { foreach (get_vendor_paths() as $vendor_path) { if (file_exists($vendor_path . $cert_path)) { $options['verify'] = $vendor_path . $cert_path; break; } } if (empty($options['verify'])) { WP_CLI::error_log("Cannot find SSL certificate."); } } try { $request = \Requests::request($url, $headers, $data, $method, $options); return $request; } catch (\Requests_Exception $ex) { // Handle SSL certificate issues gracefully \WP_CLI::warning($ex->getMessage()); $options['verify'] = false; try { return \Requests::request($url, $headers, $data, $method, $options); } catch (\Requests_Exception $ex) { \WP_CLI::error($ex->getMessage()); } } }