web-dev-qa-db-fra.com

problème d'utilisation de WP_Http avec Paypal nvp api

en essayant de convertir un plugin que j'ai écrit pour utiliser wp_http au lieu de Curl (donc ça fonctionnera sur des serveurs sans curl) le plugin poste sur l'API Paypal nvp et récupère un bouton crypté

quand j'essaie d'utiliser la classe WP_Http, j'obtiens une "réponse HTTP invalide pour le test POST".

je serais heureux de pouvoir compter sur cette aide, car je souhaite que ce plugin soit aussi générique que possible et respecte les normes.

c'est le code du plugin original pour la partie publication utilisant php Curl (et fonctionnel)

    $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

Et c'est le code avec wordpress http api (qui ne fonctionne pas correctement):

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.");
}
2
alonisser

En regardant votre code d'origine, je dirais que vous devez ajouter le sslverify arg.

Et en passant, utilisez les fonctions HTTP disponibles plutôt que d’instancier vous-même la classe (vous ne devriez pas avoir à charger la classe manuellement).

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
    )
) );
2
TheDeadMedic