Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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"

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.");
}
share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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
    )
) );
share|improve this answer
    
thanks TheDeadMedic for the help! you are this is the right way to go! the actual implentation should be: code $httpResponse = wp_remote_post($API_Endpoint,array( 'sslverify'=>false, 'body'=>$nvpreq ) ); code the urlencoding is needed because the nvp api has to get the url in a certain way including the string with all the button data. –  alonisser Jun 12 '11 at 14:48
    
No worries, glad it fixed it :) –  TheDeadMedic Jun 12 '11 at 16:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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