Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have a website in local development at test:8888 and I am trying to get the following to work in my functions.php file.

$response = wp_remote_get( 'test:8888/?p=1' );
print_r($response);

Unfortunately this is printing

WP_Error Object ( [errors] => Array ( [http_request_failed] => Array ( [0] => A valid URL was not provided. ) ) [error_data] => Array ( ) )  

Is it possible to request your own url while developing on localhost?

share|improve this question

4 Answers 4

'test:8888/?p=1' isn't a valid URL.

Try 'http://test:8888/?p=1' instead.

share|improve this answer
    
that gives this WP_Error Object ( [errors] => Array ( [http_request_failed] => Array ( [0] => Operation timed out after 5001 milliseconds with 0 bytes received ) ) [error_data] => Array ( ) ) –  Mike Dec 8 '11 at 2:50
    
I am running MAMP –  Mike Dec 8 '11 at 2:50
    
Is "test:8888" a valid URL on your machine? There's no problem with accessing local resources, but those resources must actually exist and be accessible. –  Otto Dec 8 '11 at 14:06
    
yes, that resolves fine on my machine. –  Mike Dec 8 '11 at 16:22

Ad Timeout

You should be able to get around the timeout using a filter

add_filter( 'http_request_timeout', 'wpse35826_timeout_extd' );
function wpse35826_timeout_extd( $time )
{
    // Default timeout is 5
    return 10;
}

Choose the right protocol/scheme

About your protocol/scheme problem: If it's your local install, you can use the conditional.

function wpse35826_remote_get( $args )
{
    $protocol = is_SSL() ? 'https://' : 'http://';
    $response = wp_remote_get( "{$protocol}test:8888/?p=1" );
    if ( is_wp_error( $response ) )
        return "{$response->get_error_code()}: {$response->get_error_message()}";

    return print htmlspecialchars( var_export( $response, true ) );
}

// Trigger the request
wpse35826_remote_get();

If you're doing requests to your own local server, then there might be a problem with the SSL verification. WP uses a filter to trigger it's setting:

add_filter( 'https_local_ssl_verify', '__return_false' );

This filter does not(!) trigger for local servers doing requests to a server on the web. For external servers use the following filter:

add_filter( 'https_ssl_verify', '__return_false' );

Issues with local requests

Then there also can be issues with blocked local request:

add_filter( 'block_local_requests', '__return_false' );

Other things that can be influencing your ability to do local requests are constants set wrong in your wp-config.php file:

WP_HTTP_BLOCK_EXTERNAL
// If WP_HTTP_BLOCK_EXTERNAL is defined you can add hosts which shouldn't be blocked.
WP_ACCESSIBLE_HOSTS

In case you're using Proxies:

// Allows you to define some adresses which shouldn't be passed through a proxy.
// Core sets both www and non-www as bypass.
// get_option('siteurl') and localhost are bypassed by default.
WP_PROXY_BYPASS_HOSTS

No cURL?

If there's cURL not available and you're not streaming data to a file, WP will fallback to using Fsocketopen() instead. From a core comment, which sums it up nicely:

Fsocketopen has issues with 'localhost' with IPv6 with certain versions of PHP, It attempts to connect to ::1, which fails when the server is not set up for it. For compatibility, always connect to the IPv4 address.

Point is, that WP only jumps in if we got the host name localhost. The fsocketopen host will then get set to '127.0.0.1'.

share|improve this answer

I think you actually have two problems here. Otto's answer solved your first problem, because leaving the http:// off made it an invalid URL, which is why you got the A valid URL was not provided error.

The second problem is something different and unrelated, and is giving you the Operation timed out error. I can't say for sure without seeing your entire code, but I'm guessing that the wp_remote_get() call is inside a callback function which is registered to a hook that is also firing on the page that's being requested by wp_remote_get(). That situation creates a recursive loop that ultimately times out.

You need to make sure that wp_remote_get() call isn't being fired on the page that you're requesting. You can do that by registering the callback to a different hook, or by using conditional tags to avoid calling wp_remote_get() on the requested page.

Here's an example, assuming you'll always be calling non-admin pages:

if( !is_admin() )
    return;

$response = wp_remote_get( 'http://test:8888/?p=1' );
print_r($response);
share|improve this answer

I had a similar problem and solved it like this:

function wpse35826_remote_get() {
    $response = wp_remote_get( 'http://test:8888/?p=1' );
    echo 'Response:<pre>';
    print_r($response);
    echo '</pre>';
}
add_action('admin_init','wpse35826_remote_get');
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.