WP_REST_URL_Details_Controller::get_remote_url() – Retrieves the document title from a remote URL.

You appear to be a bot. Output may be restricted

Description

Retrieves the document title from a remote URL.

Usage

$string|WP_Error = WP_REST_URL_Details_Controller::get_remote_url( $url );

Parameters

$url
( string ) required – The website URL whose HTML to access.

Returns

string|WP_Error The HTTP response from the remote URL on success. WP_Error if no response or no content.

Source

File name: wordpress/wp-includes/rest-api/endpoints/class-wp-rest-url-details-controller.php


Lines:

1 to 55 of 55
  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;
  }
 

 View on GitHub View on Trac