Proxying WP_HTTP Requests

If you just want to set up your Human Made proxy, this is not the page you need. For that, see Set up your web proxy.

This guide tells you how to proxy WordPress HTTP API requests from a WordPress site through the HM proxy. You’ll only need to do this if there are IP address based access restrictions on the host name that your site needs to connect to. (For example, some clients have APIs which are only accessible through the HM proxy).

You may also choose to proxy all WordPress HTTP API requests through the HM proxy using the method below for added security.

There are couple of easy ways to do this, one using the Charles HTTP proxy app ( Which is available for Mac, Windows, and Linux, but this guide assumes you’re using Mac ), and another using SSH Tunnels which does not require any specific tooling, just a terminal.

A. Using Charles HTTP Proxy A. Using Charles HTTP Proxy

  1. Start Charles.
  2. Connect to the HM proxy as normal.
  3. Add the following constants to your site’s wp-config.php file:

    define( ‘WP_PROXY_HOST’, ‘x.x.x.x’ );
    define( ‘WP_PROXY_PORT’, 8888 );

    where x.x.x.x is your local machine’s network IP address. You can find this in the Help -> Local IP Address menu in Charles.

  4. In OS X, go to System Preferences -> Network -> Advanced -> Proxies and ensure that both the Secure Web Proxy and the Web Proxy are set to 127.0.0.1:8888.

  5. Click SOCKS Proxy on the left and note down the port number.
  6. In Charles, go to Proxy -> External Proxy Settings, enable the SOCKS proxy, and set it to 127.0.0.1:xxxx, where xxxx is the port you just noted down from your network settings.

If you need to enable proxying for HTTPS URLs:

  1. In Charles, click Help -> SSL Proxying -> Install Charles Root Certificate and follow any instructions.
  2. Trigger an HTTPS request to the the domain name you want to enable SSL proxying for.
  3. In Charles, right-click the host name when it shows up in the list on the left hand side, and click Enable SSL Proxying. Future HTTPS connections to that host will now be proxied through Charles.

B. Using SSH Tunnels: B. Using SSH Tunnels:

  1. Open a new terminal
  2. Initiate a new SSH Tunnel using ssh -fND localhost:8888 USER@eu-west-1.aws.hmn.md
  3. Add the following constants to WordPress wp-config.php :

    define( ‘WP_PROXY_HOST’, ‘socks://10.0.2.2’ );
    define( ‘WP_PROXY_PORT’, 8888 );

Notes:

  • Replace USER with your HM Proxy username.
  • Feel free to change the port from 8888 to anything available on your host machine.
  • -f is for running the process in the background, feel free to omit to know if the connection drops for some reason.
  • 10.0.2.2 is the magic IP for Vagrant hosts, change it to your host IP if you’re not running WordPress in Vagrant.

Check that it works Check that it works

You can test everything is working by adding this code to your site:

$request = wp_remote_get( ‘http://ipchicken.com’ );
var_dump( $request );
exit;

At this point, Charles (if you’re using it) may prompt you to allow the connection. Click Allow.

Find Name Address in the output and it should be set to something that ends in amazonaws.com. If it is, the HTTP API is being correctly proxied.