int, 'headers' => array, 'body' => string, 'error' => string] */ public static function get( $url, array $args = array() ) { $args = wp_parse_args( $args, array( 'timeout' => 20, 'redirection' => 0, 'sslverify' => true, 'headers' => array(), 'user-agent' => 'ZonePilot-Probe/' . FEHU_AP_VERSION, ) ); $res = wp_remote_get( $url, $args ); if ( is_wp_error( $res ) ) { return array( 'status' => 0, 'headers' => array(), 'body' => '', 'error' => $res->get_error_message(), ); } $headers = array(); foreach ( wp_remote_retrieve_headers( $res ) as $name => $value ) { $headers[ strtolower( $name ) ] = is_array( $value ) ? implode( ', ', $value ) : (string) $value; } return array( 'status' => (int) wp_remote_retrieve_response_code( $res ), 'headers' => $headers, 'body' => (string) wp_remote_retrieve_body( $res ), 'error' => '', ); } /** * GET a URL straight from the origin server IP, bypassing Cloudflare (like curl --resolve). * * @param string $url URL on this site's host. * @param string $ip Origin IP (defaults to 127.0.0.1 = this server). * @return array Same shape as get(). */ public static function direct( $url, $ip = '' ) { self::$pin = '' !== $ip ? $ip : '127.0.0.1'; add_action( 'http_api_curl', array( __CLASS__, 'pin_curl' ), 10, 3 ); // Certificates of the origin may not match when reached by IP; we only want the status code. $res = self::get( $url, array( 'sslverify' => false ) ); remove_action( 'http_api_curl', array( __CLASS__, 'pin_curl' ), 10 ); self::$pin = ''; return $res; } /** * Pin the host to an IP on the cURL handle. * * @param resource|\CurlHandle $handle cURL handle. * @param array $args Request args. * @param string $url URL. */ public static function pin_curl( $handle, $args, $url ) { if ( '' === self::$pin ) { return; } $parts = wp_parse_url( $url ); $host = isset( $parts['host'] ) ? $parts['host'] : ''; $port = isset( $parts['port'] ) ? (int) $parts['port'] : ( ( isset( $parts['scheme'] ) && 'http' === $parts['scheme'] ) ? 80 : 443 ); $ip = false !== strpos( self::$pin, ':' ) ? '[' . self::$pin . ']' : self::$pin; curl_setopt( $handle, CURLOPT_RESOLVE, array( $host . ':' . $port . ':' . $ip ) ); // phpcs:ignore WordPress.WP.AlternativeFunctions.curl_curl_setopt } /** * The Cloudflare cache status header of a response ("" when not via Cloudflare). * * @param array $res Response from get(). * @return string */ public static function cache_status( array $res ) { return isset( $res['headers']['cf-cache-status'] ) ? strtoupper( $res['headers']['cf-cache-status'] ) : ''; } /** * Whether a response came through Cloudflare. * * @param array $res Response. * @return bool */ public static function via_cloudflare( array $res ) { return isset( $res['headers']['cf-ray'] ); } }