3

What I'm trying to do: Passing POST data by using wp_remote_post.

foreach ( $articles as $article_id ) {
    $postarray = array(
    'method'        => 'POST',
    'timeout'       => 5,
    'redirection'   => 5,
    'httpversion'   => '1.0',
    'blocking'      => true,
    'headers'       => array(),
    'body'          => array(
        'article_id' => $article_id
        ),
    'cookies' => array()
    );

    $response = wp_remote_post($url, $postarray);

    if ( is_wp_error($response) ) {
        $error_message = $response->get_error_message();
        echo $error_message;
    } else {
        // the rest of my code here
    }
}

I'm working with 20+ posts per call. Everytime the loop finish, I get this error message:

"Operation timed out after 5001 milliseconds with 0 bytes received"

The strange thing is, the data are actually received and stored successfully on the designated $url server.

Can anyone point me to the right direction, where should I look to avoid getting that error message?

Reference: wp_remote_post

7

After quite some time letting the error message bugging my screen, I figured out a way to solve this.

Yes, it's a timeout issue, and the codex didn't help me much. So I tried another approach, by setting a filter;

add_filter( 'http_request_timeout', 'wp9838c_timeout_extend' );

function wp9838c_timeout_extend( $time )
{
    // Default timeout is 5
    return 10;
}

I hope this could be another reference for someone else in the future.

  • 1
    It is always great if you find a solution yourself :-). Many thanks for posting your solution. Just remember to come back once the restriction have been lifted and accept your own answer. – Pieter Goosen Oct 30 '14 at 6:15
  • 1
    I came back in instant after I receive my email notice. Thank you for your guidance, @PieterGoosen – Nich Oct 30 '14 at 12:08
7

You can set the timeout directly in the wp_remote_post() $args, as per this example from developer.wordpress.org:

$response = wp_remote_post( $url, array(
    'method'      => 'POST',
    'timeout'     => 45,
    'redirection' => 5,
    'httpversion' => '1.0',
    'blocking'    => true,
    'headers'     => array(),
    'body'        => array(
        'username' => 'bob',
        'password' => '1234xyz'
    ),
    'cookies'     => array()
    )
);

if ( is_wp_error( $response ) ) {
    $error_message = $response->get_error_message();
    echo "Something went wrong: $error_message";
} else {
    echo 'Response:<pre>';
    print_r( $response );
    echo '</pre>';
}

One other thing to note: in this example, the timeout is 45 seconds, but in many cases, that will exceed the PHP max_execution_time time limit, so you'll still get an error, but this time, a fatal PHP (500) error rather than the http error returned by WordPress (so you're actually worse off!).

This can be solved by setting max_execution_time in your php.ini, or, if you're not running in safe_mode (unlikely on a production server), you can attempt to set it programatically within your code, as per the example below:

$timeout = 45;
if ( ! ini_get( 'safe_mode' ) ){
    set_time_limit( $timeout + 10 );
}

$response = wp_remote_post( $url, array(
    'timeout' => $timeout
) );

Here, I set the PHP timeout to 10 seconds more than the HTTP timeout, just to be safe.

Also, it would be a best practice to then reset the timeout back to what it was, which is probably the value returned by ini_get( 'max_execution_time' );

  • Question, regarding the max_execution_time, do you know what the default amount would be for wordpress? Or better, where I could find the setting? – wlh Jan 29 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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