An Unexpected HTTP Error occurred during the API request

I just did a clean install of WordPress. But I have a problem when I install or use the WordPress API. So send me a sign

An Unexpected HTTP Error occurred during the API request.

Read More

The strange thing is that I have looked everywhere. And the solutions do not work for me porponen WordPress 3.2.1 and do not know how to solve the problem

Related posts

Leave a Reply

3 comments

  1. Use the following function to debug the HTTP API request, you will get to know the actual reason why the HTTP API request is failing.

    Paste the following code in your theme’s function.php.

    function dump_http_response( $response, $type, $transport, $args, $url ) {
        if ( is_admin() && $type == "response" )  {
            echo '<span style="color: #f00;">';
            var_dump( $response );
            echo '</span>';
        }
    }
    add_action( 'http_api_debug', 'dump_http_response', 1, 5 );
    
  2. This script gives you a rough overview what kind of filesystem mehods are allowed/installed on your server. The first three methods are the methods WordPress prefered and WordPress can be forced to use one of them by defining FS_METHOD in the wp-config.php.

    Copy the code into a file, upload the file to your server, checkout which filesystem methods are allowed/installed and delete the file asap.

    <?php
    $yes = '<span style="color:green; font-weight:bold">:)</span>';
    $no  = '<span style="color:red; font-weight:bold">:(</span>';
    $items = array();
    
    $items['fopen'][0] = 'Opening urls via fopen (for FS_METHOD "direct")';
    $items['fopen'][1] = ( TRUE == ini_get( 'allow_url_fopen' ) ) ?
        ' is allowed '.$yes : ' is not allowed '.$no;
    
    $items['ssh2'][0] = 'SSH2 (for FS_METHOD "ssh")';
    $items['ssh2'][1] = ( TRUE === extension_loaded( 'ssh2' ) ) ?
        ' is installed '.$yes : ' is not installed '.$no;
    
    $items['ftp'][0] = 'FTP (for FS_METHOD "ftpext")';
    $items['ftp'][1] = ( TRUE === extension_loaded( 'ftp' ) ) ?
        ' is installed '.$yes : ' is not installed '.$no;
    
    $items['sockets'][0] = 'Sockets (for FS_METHOD "ftpsockets")';
    $items['sockets'][1] = ( TRUE === extension_loaded( 'sockets' ) ) ?
        ' are installed '.$yes : ' are not installed '.$no;
    
    $items['curl'][0] = 'Curl';
    $items['curl'][1] = ( TRUE === extension_loaded( 'curl' ) ) ?
        ' is installed '.$yes : ' is not installed '.$no;
    
    echo '<html>';
    echo '<ol>';
    
    foreach ( $items as $item ) {
        printf( '<li>%s%s</li>', $item[0], $item[1] );
    }
    
    echo '</ol>';
    echo '</html>';
    

    You can try to force WordPress to use a filesystem method by defining it in your wp-config.php, e.g. to use the php ftp extension instead of fopen:

    if ( ! defined( 'FS_METHOD' )
      define( 'FS_METHOD', 'ftpext' );
    

    This could be helpfull if you got trouble using a filesystem method and want to use another one.

  3. The “WordPress API” is a pretty vague (WP got multiple APIs). But from your error it seems that it’s a HTTP API problem. There’re multiple possible obstacles:

    SSL Certificate issues

    If you got a problem with your SSL certificate (necessary when the server is a subdomain of the site listed in the SSL certificate; using a wildcard certificate obviates the need for this).

    As pointed out in the comments, the problem could also be that your server has out-of-date or missing SSL CA root certificates.

    Request problems (WP HTTP API not responding 200/OK)

    You think you ain’t got cURL available and the fsocketopen fallback didn’t work out?

    First you want to check if the response works out. For this task, you can use two services (where both tell you slightly different details):

    Then, as @HameedullahKhan pointed out in his answer, there’s a debug filter for the WP HTTP API. This filter runs at the absolute last point of any request, right before giving it back to the developer.

    <?php
    /** Plugin Name: (#28871) Debug WP HTTP API response */
    add_action( 'http_api_debug', 'wpse28871_debug_request', 999, 5 );
    function wpse28871_debug_request( $response, $type, $class, $args, $url )
    {
        printf( '<pre>ResponseData: %s', is_wp_error( $response ) 
            ? $response->get_error_code().' '.$response->get_error_message()
            : $response
        );
        printf( '<br />ResponseType: %s',      $type );
        printf( '<br />ResponseClass: %s',     $class );
        printf( '<br />ResponseArgs: %s',      $args );
        printf( '<br />ResponseURl: %s</pre>', $url );
        # @TODO Uncomment to exit when debugging AJAX requests
        # exit();
    }
    

    If the above doesn’t give you any insights, you can still try to debug the cURL object right after it got built and right before it got fired. This way is pretty unknown, but it has enormous powers when you combine it with above plugin: It tells you if the request has a problem with the cURL args or if it fails later, narrowing down the range of possible problems pretty fast.

    <?php
    /* Plugin Name: (#28871) Debug WP HTTP API cURL arguments */
    add_action( 'http_api_curl', 'wpse28871_curl_debug' );
    function wpse28871_curl_debug( $handle )
    {
        printf(
             '<pre>%s</pre>'
            ,var_export( curl_getinfo( $handle, CURLINFO_HEADER_OUT ), true ) )
        );
    }