Make WordPress Core

Opened 10 years ago

Closed 7 years ago

#39400 closed enhancement (wontfix)

Add filter for remote request URL

Reported by: iandunn Owned by:
Priority: normal Milestone:
Component: HTTP API Version:
Severity: minor Keywords: has-patch close 2nd-opinion reporter-feedback
Cc: Focuses:

Description

Currently, I can filter an HTTP request's parameters, but not its URL.

I've run into several situations where this would be useful over the years, and it seems like others have as well.

There are a few workarounds, but they're inconvenient and ugly. This should be simple, and it seems like something developers just assume already exists.

Attachments (1)

39400.diff (738 bytes ) - added by iandunn 10 years ago.

Download all attachments as: .zip

Change History (5)

@iandunn
10 years ago

#1 @iandunn
10 years ago

  • Keywords has-patch added
  • Severity normalminor

#2 @dd32
10 years ago

Personally I find it weird that we have the http_request_args filter, and don't really see adding a http_request_url filter in addition to it a good idea.

Filtering the URL requested is currently possible, but it's not as straightforward as some might expect - which is IMHO a good thing, as it also forces you to handle more of the actual request logic.

To filter the URL, you can hook into pre_http_request, match the URL, and then return the result of the request you'd like to make.

For example:

add_filter( 'pre_http_request', function( $preempt, $r, $url ) { 
  if ( 'example.com' == parse_url( $url, PHP_URL_HOST ) ) {
      // mangle $r here to add extra headers, etc.
      // override the current request with the new one:
      $preempt = wp_remote_request( 'http://example.NET/', $r );
  }

  return $preempt;
}, 10, 3 );

wp_remote_request( 'http://example.com/' ); // returns result from example.NET

Edit: I should note that this is more or less what's shown in one of your links; what the link doesn't show is how clean and straightforward the filter can be when used correctly.

Last edited 10 years ago by dd32 (previous) (diff)

#3 @desrosj
7 years ago

  • Keywords close 2nd-opinion reporter-feedback added

Marking this as a close candidate pending a second opinion. Unless you are ok with closing this as a wontfix, @iandunn.

#4 @iandunn
7 years ago

  • Milestone Awaiting Review
  • Resolutionwontfix
  • Status newclosed

I personally still feel like a filter on the URL would be more straight-forward and consistent, but Dion is much more experienced in this area, so I'm happy to defer to his judgement.

Note: See TracTickets for help on using tickets.