How to use the HTTP API with a Proxy?

If I want to route a HTTP request through a local (alias: on the current server) Proxy, how would I go around this?

The allowed Proxy settings for the wp-config.php are the following:

Read More
# HTTP Proxies
# Used for e.g. in Intranets
# Fixes Feeds as well
# Defines the proxy adresse.
define( 'WP_PROXY_HOST',          '127.0.84.1' );
# Defines the proxy port.
define( 'WP_PROXY_PORT',          '8080' );
# Defines the proxy username.
define( 'WP_PROXY_USERNAME',      'my_user_name' );
# Defines the proxy password.
define( 'WP_PROXY_PASSWORD',      'my_password' );
# Allows you to define some adresses which 
# shouldn't be passed through a proxy.
define( 'WP_PROXY_BYPASS_HOSTS',  'localhost, www.example.com' );

This question is a follow up on this question.

Related posts

Leave a Reply

1 comment

  1. The proxy settings work just like a regular HTTP requests but in this case obviously routed through a proxy. In terms of WordPress the API’s transport layers all support proxy connections(fsockopen, fopen, cURL, ).

    The things about proxy configurations are they come in several flavors and each setup is different so it makes answering this difficult, it doesn’t really matter if your proxy is on your localhost or remote, the wp-config.php settings will work regardless. Typically you want to use these settings if your on an intranet/firewall that has specific requirements.

    It is worth noting you can just set your localhost/webserver to use a proxy/chaining by default for HTTP requests and in that case it is not necessary to set any options with wp-config.php since this is configured at the server level . If you disable your proxy you typically see a response code of error 130 ERR_PROXY_CONNECTION_FAILED, but these setups are outside the scope of WordPress.

    Some tools that can help you setup and debug proxy connections:

    To dig into the WordPress HTTP API I recommend the following snippet using the http_api_debug action (altered to var_dump found via viper007bond’s site):

    add_action( 'http_api_debug', 'viper_http_api_debug', 10, 5 );
    
    function viper_http_api_debug( $response, $type, $class, $args, $url ) {
        // You can change this from error_log() to var_dump() but it can break AJAX requests
        var_dump( 'Request URL: ' . var_export( $url, true ) );
        var_dump( 'Request Args: ' . var_export( $args, true ) );
        var_dump( 'Request Response : ' . var_export( $response, true ) );
    }
    

    The Request Response is the interesting part, sometimes you can tell with a quick glance if your request is going through proxy..

    For example using the default HTTP API to make the following request.

    $api_url = 'http://api.wordpress.org/secret-key/1.0/';
    $response = wp_remote_get($api_url);
    $header = wp_remote_retrieve_headers( $response );
    
    var_dump($header);
    

    enter image description here

    Now the same exact request but using a remote proxy enabled via wp-config.php

    //I grabbed these off of Google search they will not work for long.
    define( 'WP_PROXY_HOST', '210.22.115.162' );
    define( 'WP_PROXY_PORT', '3128' );
    

    enter image description here

    As you can see the proxy the output is different, most importantly the proxy is adding the via tag, in this case a squid proxy. Proxies are supposed to do this and not alter the server response-header , but not everyone follows the rules so be careful;).

    The constant define( 'WP_PROXY_BYPASS_HOSTS', 'localhost, www.example.com' ) is useful for allowing access to hosts that you might not want to go through a proxy (WordPress updates for example). The comments in the class-http.php are misleading because by default localhost and get_option('siteurl); are already included but can be altered via the pre_http_send_through_proxy filter.

    Some additional options that work with the proxy settings are:
    WP_HTTP_BLOCK_EXTERNAL WP_ACCESSIBLE_HOSTS