我正在制作使用Paypal的东西,并且在Paypal流程中我的最后一步遇到了一些麻烦。我有四个Paypal API的调用工作正常,但最后一个没有。首先,这是我的卷曲功能
function curl($PayPalMode,$nvpreq) {
$API_Endpoint = "https://api-3t.".$PayPalMode.".paypal.com/nvp";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $API_Endpoint);
curl_setopt($ch, CURLOPT_VERBOSE, 1)
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);
curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
$httpResponse = curl_exec($ch);
$httpResponseAr = explode("&", $httpResponse);
$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)) {
return "Invalid HTTP Response for POST request($nvpreq) to $API_Endpoint.";
}
return $httpParsedResponseAr;
}
然后,如上所述,这对于不同的事情使用了四次,例如
$data_set = array(
USER = $PayPalApiUsername,
PWD = $PayPalApiPassword,
SIGNATURE = $PayPalApiSignature,
METHOD = "SetExpressCheckout",
VERSION = urlencode(109.0),
L_BILLINGTYPE0 = "RecurringPayments",
L_BILLINGAGREEMENTDESCRIPTION0 = urlencode($paypal_obj->name)." (".urlencode($paypal_obj->price)."/Month)",
CANCELURL = $PayPalCancelURL,
RETURNURL = $PayPalReturnURL,
CURRENCYCODE = $PayPalCurrencyCode
);
$nvpreq_set = http_build_query($data_set);
$set_express_checkout_curl = curl($PayPalMode,$nvpreq_set);
前四个请求(包括GetExpressCheckoutDetails,CreateRecurringPaymentsProfile和GetRecurringPaymentsProfileDetails)都可以使用完全相同的方法正常工作。然而,第五个也是最后一个没有。这是使用UpdateRecurringPaymentsProfile,因为我有时想要使用试用月。它使用与上面完全相同的方法
$data_update = array(
USER = $PayPalApiUsername,
PWD = $PayPalApiPassword,
SIGNATURE = $PayPalApiSignature,
METHOD = "UpdateRecurringPaymentsProfile",
VERSION = urlencode(109.0),
PROFILEID = $payer_id,
TRIALBILLINGPERIOD = "Month",
TRIALBILLINGFREQUENCY = 1,
TRIALTOTALBILLINGCYCLES = 1,
TRIALAMT = 10.00,
CURRENCYCODE = "GBP"
);
$nvpreq_update = http_build_query($data_update);
$update_profile_curl = curl($PayPalMode,$nvpreq_update);
然而,这不起作用,而是返回错误11510 - 无效的试用金额,试用金额必须> = 0.在向Paypal发送支持消息后,他们确认TRIALAMT未被通过,这都是他们正在接受
Timestamp 20-Jan-2016 17:40:41 GMT (1453311641)
UpdateRecurringPaymentsProfileRequest
profileid "(profileid)"
currencycode "GBP"
trialtotalbillingcycles "1"
method "UpdateRecurringPaymentsProfile"
version "109"
user (email address)
correlation_id (correlation id)
正如您所看到的,不仅仅是TRIALAMT没有被传递,它也是TRIALBILLINGPERIOD和TRIALBILLINGFREQUENCY。这是所有这一切中唯一的例子,一些数据没有被传递 - 我的问题是有谁知道为什么会这样?我完全迷失了,我不知道为什么这些数据不会被传递,除非我的最终请求有问题?
答案 0 :(得分:0)
据我所知,urlencoding你的params(在curl函数内)可以防止数据丢失,如本网址所述:http://www.brandonchecketts.com/archives/array-versus-string-in-curlopt_postfields
...因此,在PHP / cURL中发送POST请求时,首先将其作为字符串进行urlencode非常重要。
这将生成multipart / form-data版本
$data = array('name' => 'value', 'name2' => 'value2'); curl_setopt($curl_object, CURLOPT_POSTFIELDS, $data)
这个简单的更改将确保它使用application / x-www-form-urlencoded版本:
$data = array('name' => 'value', 'name2' => 'value2'); $encoded = ''; foreach($data as $name => $value){ $encoded .= urlencode($name).'='.urlencode($value).'&'; } // chop off the last ampersand $encoded = substr($encoded, 0, strlen($encoded)-1); curl_setopt($curl_object, CURLOPT_POSTFIELDS, $encoded)