problem using WP_Http with paypal nvp api

trying to convert a plugin I wrote to use wp_http instead of Curl (so it’ll work on servers without curl)
the plugin posts to paypal nvp api and gets back an encrypted button

when I try to use the WP_Http class i get “Invalid HTTP Response for POST”

Read More

would be glad for any help figuring this help – since I want to make this plugin as generic and adhering to standards as possible.

this is the original plugin code for the posting part using php Curl (and working)

    $API_UserName  = urlencode($username);
$API_Password  = urlencode($password);
$API_Signature = urlencode($signature);


//the curl part - maybe we should change this into wp post functions

// setting the curl parameters.
$ch = curl_init(); //calling curl phplib inside $CH
curl_setopt($ch, CURLOPT_URL, $API_Endpoint);//setting the endpoint
curl_setopt($ch, CURLOPT_VERBOSE, 1); //should check this, i don't really know

// turning off the server and peer verification(TrustManager Concept).
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);

// NVPRequest for submitting to server- including endpoint,credentials,the button data
$nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$API_Password&USER=$API_UserName&SIGNATURE=$API_Signature&$nvpStr_";

// setting the nvpreq as POST FIELD to curl
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);

// getting response from server
$httpResponse = curl_exec($ch);
$httpResponse =  $request->request($API_Endpoint,array('method'=>'post', 'body'=>$nvpreq));


if(!$httpResponse) {
    exit("$methodName_ failed: "/*.curl_error($ch).'('.curl_errno($ch).')'*/);
}

// Extract the button response details
$httpResponseAr = explode("&", $httpResponse->body);

$httpParsedResponseAr = array();
foreach ($httpResponseAr as $i => $value) {
    $tmpAr = explode("=", $value);
    if(sizeof($tmpAr) > 1) {
        $httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];
    }
}

if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) {
    exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.");
}

return $httpParsedResponseAr;// an array we'll parse back in the calling function - currently using only websitecode. but in the future maybe also the email code

And this is the code with wordpress http api (that doesn’t work properly):

if (!class_exists('WP_Http')) {

    include_once ('c:/wamp/www/wp/wp-includes/class-http.php');
}

$request = new WP_Http;
$httpResponse =  $request->request($API_Endpoint,array('method'=>'post', 'body'=>$nvpreq));


if(!$httpResponse) {
    exit("$methodName_ failed: "/*.curl_error($ch).'('.curl_errno($ch).')'*/);
}

// Extract the button response details
$httpResponseAr = explode("&", $httpResponse->body);

$httpParsedResponseAr = array();
foreach ($httpResponseAr as $i => $value) {
    $tmpAr = explode("=", $value);
    if(sizeof($tmpAr) > 1) {
        $httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];
    }
}

if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) {
    exit("Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.");
}

Related posts

Leave a Reply

1 comment

  1. Looking at your original code, I’d say you need to add the sslverify arg.

    And on a side note, use the HTTP functions available, rather than instantiating the class yourself (you shouldn’t have to load the class manually).

    wp_remote_post( $url, array(
        'sslverify' => false, // this is true by default!
        'body' => array( 
            'METHOD'    => $methodName, // if you're using an array, no need to URL encode
            'VERSION'   => $version,
            'PWD'       => $API_Password,
            'USER'      => $API_UserName,
            'SIGNATURE' => $API_Signature
        )
    ) );