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

2 Answers
2

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.

Leave a Reply

Your email address will not be published. Required fields are marked *