Converting CURL request to WordPress wp_remote_post

I need to convert a PHP Library from CURL to wp_remote_post for use for a WordPress Plugin.

I have reference the WordPress wp_remote_post page here.

Read More

This is the code I attempt to convert..

 $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $scheme . $url['host'] . $url['path']);
        curl_setopt($ch, CURLOPT_PORT, $port);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        curl_setopt($ch, CURLOPT_USERAGENT, $this->_config['UserAgent']);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $query);
        curl_setopt($ch, CURLOPT_HEADER, true); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        $response = "";
        $response = curl_exec($ch);

        if ($response === false) {
            $errorResponse = curl_error($ch);
            require_once("FBAOutboundServiceMWS/Exception.php");
            curl_close($ch);

            throw new FBAOutboundServiceMWS_Exception(array(
                'Message' => $errorResponse,
                'ErrorType' => 'HTTP'
            ));
        }

        curl_close($ch);

This is what I ‘thought’ was correct but doesn’t work…

$THEurl = $scheme . $url['host'] . $url['path'];
    $response = wp_remote_post( $THEurl, array(
        'method' => 'POST',
        'timeout' => 45,
        'redirection' => 5,
        'httpversion' => '1.0',
        'blocking' => true,
        'headers' => array(),
        'body' => $query,
        'cookies' => array()
        )
    );

    if ( is_wp_error( $response ) ) {
       $errorResponse = $response->get_error_message();
            require_once("FBAOutboundServiceMWS/Exception.php");
            curl_close($ch);

            throw new FBAOutboundServiceMWS_Exception(array(
                'Message' => $errorResponse,
                'ErrorType' => 'HTTP'
            ));
    } 

At what point does wp_remote_post actually get executed? Just when the function is called?
Many thanks for help in advance 😉

Related posts

Leave a Reply

1 comment

  1. Try this out for me and see what you get back

    $THEurl = $scheme . $url['host'] . $url['path'];
    $response = wp_remote_post( $THEurl, array(
        'method' => 'POST',
        'timeout' => 45,
        'redirection' => 5,
        'httpversion' => '1.0',
        'blocking' => true,
        'headers' => array(),
        'body' => $query,
        'cookies' => array()
        )
    );
    
    if ( is_wp_error( $response ) ) {
        $errorResponse = $response->get_error_message();
        require_once("FBAOutboundServiceMWS/Exception.php");
    
        throw new FBAOutboundServiceMWS_Exception(
            array(
                'Message' => $errorResponse,
                'ErrorType' => 'HTTP'
            )
        );
    } else {
        echo 'Response:<pre>';
        print_r( $response );
        echo '</pre>';
    }
    

    Took out the code you didn’t need and also added in the printing of the response which you weren’t doing before.