WP_REST_URL_Details_Controller::get_remote_url()privateWP 5.9.0

Retrieves the document title from a remote URL.

Метод класса: WP_REST_URL_Details_Controller{}

Хуки из метода

Возвращает

Строку|WP_Error. The HTTP response from the remote URL on success. WP_Error if no response or no content.

Использование

// private - только в коде основоного (родительского) класса
$result = $this->get_remote_url( $url );
$url(строка) (обязательный)
The website URL whose HTML to access.

Список изменений

С версии 5.9.0 Введена.

Код WP_REST_URL_Details_Controller::get_remote_url() WP 6.5.2

private function get_remote_url( $url ) {

	/*
	 * Provide a modified UA string to workaround web properties which block WordPress "Pingbacks".
	 * Why? The UA string used for pingback requests contains `WordPress/` which is very similar
	 * to that used as the default UA string by the WP HTTP API. Therefore requests from this
	 * REST endpoint are being unintentionally blocked as they are misidentified as pingback requests.
	 * By slightly modifying the UA string, but still retaining the "WordPress" identification (via "WP")
	 * we are able to work around this issue.
	 * Example UA string: `WP-URLDetails/5.9-alpha-51389 (+http://localhost:8888)`.
	*/
	$modified_user_agent = 'WP-URLDetails/' . get_bloginfo( 'version' ) . ' (+' . get_bloginfo( 'url' ) . ')';

	$args = array(
		'limit_response_size' => 150 * KB_IN_BYTES,
		'user-agent'          => $modified_user_agent,
	);

	/**
	 * Filters the HTTP request args for URL data retrieval.
	 *
	 * Can be used to adjust response size limit and other WP_Http::request() args.
	 *
	 * @since 5.9.0
	 *
	 * @param array  $args Arguments used for the HTTP request.
	 * @param string $url  The attempted URL.
	 */
	$args = apply_filters( 'rest_url_details_http_request_args', $args, $url );

	$response = wp_safe_remote_get( $url, $args );

	if ( WP_Http::OK !== wp_remote_retrieve_response_code( $response ) ) {
		// Not saving the error response to cache since the error might be temporary.
		return new WP_Error(
			'no_response',
			__( 'URL not found. Response returned a non-200 status code for this URL.' ),
			array( 'status' => WP_Http::NOT_FOUND )
		);
	}

	$remote_body = wp_remote_retrieve_body( $response );

	if ( empty( $remote_body ) ) {
		return new WP_Error(
			'no_content',
			__( 'Unable to retrieve body from response at this URL.' ),
			array( 'status' => WP_Http::NOT_FOUND )
		);
	}

	return $remote_body;
}