无法通过cURL连接到PayPal API

时间:2013-03-11 17:25:11

标签: php curl paypal

我正在尝试通过“Classic”NVP API设置PayPal Express付款。

尝试使用cURL从我的服务器连接到PayPal-Sandbox,连接会在大约2分钟后停止并超时。

我正在使用文档中的example call

curl -v --insecure  https://api-3t.sandbox.paypal.com/nvp  -d  "USER=platfo_1255077030_biz_api1.gmail.com&PWD=1255077037&SIGNATURE=Abg0gYcQyxQvnf2HDJkKtA-p6pqhA1k-KTYE0Gcy1diujFio4io5Vqjf&METHOD=SetExpressCheckout&VERSION=78&PAYMENTREQUEST_0_PAYMENTACTION=SALE&PAYMENTREQUEST_0_AMT=19&PAYMENTREQUEST_0_CURRENCYCODE=USD&cancelUrl=http://www.yourdomain.com/cancel.html&returnUrl=http://www.yourdomain.com/success.html"

Shell输出是:

* About to connect() to api-3t.sandbox.paypal.com port 443 (#0)
* Trying 173.0.82.83... Connection timed out
* couldn't connect to host
* Closing connection #0
curl: (7) couldn't connect to host

当我尝试通过PHP curl尝试这样做时,我没有收到任何错误,只是一个空的资源句柄。

我可以轻松地从本地计算机和我可以访问的其他服务器执行请求(并获取正确的数据),因此我猜这是一些服务器端错误配置正在进行。不是服务员,我有点无能为力。

已启用cURL并在phpinfo

中记录以下内容
libcurl/7.21.0 OpenSSL/0.9.8o zlib/1.2.3.4 libidn/1.15 libssh2/1.2.6

也启用了openSSL。此外,我在尝试连接到API的实时版本时遇到了同样的问题。

4 个答案:

答案 0 :(得分:1)

尝试

$ch = curl_init();
# Merchant Account Credentials
$ppUserID = "User Id Email"; //Take it from sandbox dashboard for test mode or take it from paypal.com account in production mode, help: https://developer.paypal.com/docs/classic/api/apiCredentials/
$ppPass = "User Pass"; //Take it from sandbox dashboard for test mode or take it from paypal.com account in production mode, help: https://developer.paypal.com/docs/classic/api/apiCredentials/
$ppSign = "Paypal Sign"; //Take it from sandbox dashboard for test mode or take it from paypal.com account in production mode, help: https://developer.paypal.com/docs/classic/api/apiCredentials/

$ppAppID = "APP-80W284485P519543T"; //if it is sandbox then app id is always: APP-80W284485P519543T

$paypal_header_options = array();
    $paypal_header_options[] = "X-PAYPAL-SECURITY-USERID: $ppUserID";
    $paypal_header_options[] = "X-PAYPAL-SECURITY-PASSWORD: $ppPass";
    $paypal_header_options[] = "X-PAYPAL-SECURITY-SIGNATURE: $ppSign";
    $paypal_header_options[] = "X-PAYPAL-REQUEST-DATA-FORMAT: NV";
    $paypal_header_options[] = "X-PAYPAL-RESPONSE-DATA-FORMAT: NV";
    $paypal_header_options[] = "X-PAYPAL-APPLICATION-ID: $ppAppID";

$URL = 'https://api-3t.sandbox.paypal.com/nvp'
    .'?USER='.$ppUserID
    .'&PWD='.$ppPass
.'&SIGNATURE='.$ppSign
    .'&METHOD=SetExpressCheckout' 
    .'&VERSION=93'
    .'&RETURNURL=https://localhost/express-checkout-single-product/success.php?success=true'
    .'&CANCELURL=https://localhost/express-checkout-single-product/index.php'
.'&PAYMENTREQUEST_0_CURRENCYCODE=USD'
.'&PAYMENTREQUEST_0_AMT=250.00'    #The payment amount for the first receiver   # Merchant(Primary AC) Account Amount
.'&PAYMENTREQUEST_0_ITEMAMT=225.00'                         # Merchant(Primary AC) Account Email
.'&PAYMENTREQUEST_0_TAXAMT=25.00'    #Receiver designation (there can be only 1 primary receiver)
.'&PAYMENTREQUEST_0_PAYMENTACTION=Order' 
.'&PAYMENTREQUEST_0_SELLERPAYPALACCOUNTID=email' 
.'&PAYMENTREQUEST_0_PAYMENTREQUESTID=CART110-PAYMENT0'    #The payment amount for the second receiver   # Seller(Secondry AC) Account Amount
.'&PAYMENTREQUEST_1_CURRENCYCODE=USD'
.'&PAYMENTREQUEST_1_AMT=75.00'    #The payment amount for the first receiver    # Merchant(Primary AC) Account Amount
.'&PAYMENTREQUEST_1_ITEMAMT=65.00'                          # Merchant(Primary AC) Account Email
.'&PAYMENTREQUEST_1_TAXAMT=10.00'    #Receiver designation (there can be only 1 primary receiver)
.'&PAYMENTREQUEST_1_PAYMENTACTION=Order' 
.'&PAYMENTREQUEST_1_SELLERPAYPALACCOUNTID=email' 
.'&PAYMENTREQUEST_1_PAYMENTREQUESTID=CART110-PAYMENT1' 
    .'&L_PAYMENTREQUEST_0_NAME0=Departs2'
    .'&L_PAYMENTREQUEST_0_NAME0=Sunset'
.'&L_PAYMENTREQUEST_0_QTY0=1'
    .'&L_PAYMENTREQUEST_0_AMT0=125'
    .'&L_PAYMENTREQUEST_0_TAXAMT0=15'
    .'&L_PAYMENTREQUEST_0_NAME1=Departs'
.'&L_PAYMENTREQUEST_0_QTY1=1'
    .'&L_PAYMENTREQUEST_0_AMT1=100.00'
    .'&L_PAYMENTREQUEST_0_TAXAMT1=10.00';
    curl_setopt($ch, CURLOPT_URL, $URL);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  //  curl_setopt($ch, CURLOPT_HTTPHEADER, $paypal_header_options);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

    $paypal_response = curl_exec($ch);

$responseAr = explode('&', $paypal_response);
$parsedResponseAr = array();
foreach($responseAr as $i => $value) {
    $tmpAr = explode('=', $value);
    if(!empty($tmpAr))
        $parsedResponseAr[strtoupper($tmpAr[0])] = urldecode($tmpAr[1]);
}
print_r(json_encode($parsedResponseAr));
curl_close($ch);

答案 1 :(得分:0)

A)首先尝试wget https://api-3t.sandbox.paypal.com/nvp并查看是否可以与"错误请求"进行简单连接。回复,如果没有,你被Paypal服务器阻止或你的防火墙阻止你在端口443访问paypal

B)尝试添加自定义标头:-H "Host: api-3t.sandbox.paypal.com"

curl -v --insecure https://api-3t.sandbox.paypal.com/nvp -d "USER=platfo_1255077030_biz_api1.gmail.com&PWD=1255077037&SIGNATURE=Abg0gYcQyxQvnf2HDJkKtA-p6pqhA1k-KTYE0Gcy1diujFio4io5Vqjf&METHOD=SetExpressCheckout&VERSION=78&PAYMENTREQUEST_0_PAYMENTACTION=SALE&PAYMENTREQUEST_0_AMT=19&PAYMENTREQUEST_0_CURRENCYCODE=USD&cancelUrl=http://www.yourdomain.com/cancel.html&returnUrl=http://www.yourdomain.com/success.html" -H "Host: api-3t.sandbox.paypal.com"

C)什么版本的CURL以及您使用的是什么平台?我有:

curl 7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.21 Basic ECC zlib/1.2.3 libidn/1.18 libssh2/1.4.2

答案 2 :(得分:0)

由于错误身份验证无效的ssl证书颁发,很可能PayPal Express付款服务停止/削减您的连接。在这种情况下,请尝试向服务提供商询问此问题。

希望能解决你的问题。

答案 3 :(得分:0)

您的错误是由于您的服务器版本的nss包和使用SNI证书的api-3t.sandbox.paypal.com。您可以通过执行

来验证它

openssl s_client -servername api-3t.sandbox.paypal.com -tlsextdebug -connect api-3t.sandbox.paypal.com:443 2>/dev/null | grep "server name"

over bash。输出为TLS server extension "server name" (id=0), len=0,表示此服务器具有SNI证书。

解决方案实际上很简单。

对于RedHat变体:

sudo yum -y update nss*

对于Debian变体:

sudo apt-get update nss*

您实际上不必更改代码(针对连接问题)