使用带有PayPal NVP API的wp_http时出现问题

时间:2011-06-12 作者:alonisser

尝试将我编写的插件转换为使用wp\\u http而不是Curl(因此它可以在没有Curl的服务器上工作),该插件将发布到paypal nvp api并返回一个加密按钮

当我尝试使用WP\\U Http类时,我得到“POST的Http响应无效”

我很乐意为您提供帮助,因为我想让这个插件尽可能通用并遵守标准。

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

1 个回复
最合适的回答,由SO网友:TheDeadMedic 整理而成

看看您的原始代码,我想说您需要添加sslverify 参数。

另一方面,请使用可用的HTTP函数,而不是自己实例化该类(不必手动加载该类)。

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

结束

相关推荐

WordPress的HTTP API使用缓存了吗?

我正在开发一个插件,用于从远程服务器检索数据并将其显示在仪表板上。为此,我将使用WordPressHTTP API 但我没有看到任何关于使用任何形式缓存的HTTP API的信息。由于我不想让我的插件给远程服务器的管理员带来问题,我想知道是否遗漏了一些关于HTTP API进行缓存的文档,或者是否还需要使用Transients API 为我的插件实现缓存?