paypal rest api付款电话使用php返回401

时间:2013-08-29 00:12:19

标签: php curl paypal paypal-sandbox

我正在尝试将paypal添加到我的网站。我一直按照https://developer.paypal.com/webapps/developer/docs/integration/direct/make-your-first-call/的说明进行操作,但它无效。我已成功发送了访问令牌请求并得到了响应。响应中的信息存储在名为$ accessToken的对象中。问题在于我尝试从上面列出的站点在步骤3中进行API调用。我从请求中发回401错误。我很确定请求作为函数参数发送的$ url是正确的。它是https://api.sandbox.paypal.com/v1/payments/payment。在过去一周半的时间里,我一直在互联网上寻求帮助,但我没有取得任何进展。任何帮助将不胜感激。谢谢!

function MakePaymentAPICall($accessToken, $sale, $url, $url_success, $url_cancel){

// Create cURL resource
$ch = curl_init();

// Set url
curl_setopt($ch, CURLOPT_URL, $url);

$tokenType = $accessToken->GetTokenType();
$token = $accessToken->GetAccessToken();
$auth = "Authorization:" . $tokenType . " " . $token;
$saleTotal = $sale->GetTotal();

$header = array(
'Content-Type' => 'application/json',
'Authorization' => $tokenType . ' ' . $token
);


$dataArray = array(
    'intent' => 'sale',
    'redirect_urls' => array(
            'return_url' => $url_success,
            'cancel_url' => $url_cancel
        ),
    'payer' => array(
            'payment_method' => 'paypal'
        ),
    'transactions' => array(
            'amount' => array(
                    'total' => $saleTotal,
                    'currency' => 'USD'
                ),
            'description' => 'Test payment.'
        )
);


curl_setopt($ch, CURLOPT_HEADER, http_build_query($header));


// set data to post
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($dataArray));


curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);


// Execute curl command
$output = curl_exec($ch);


// Get info about request
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);


// Close cURL resource to free up system resources
curl_close($ch);

return $output;

} // MakePaymentAPICall函数

2 个答案:

答案 0 :(得分:1)

@ Jason247

http://php.net/manual/en/function.curl-setopt.php

我认为 CURLOPT_HEADER 需要int或bool,要么是1,要么是TRUE来发送标题。

我相信 CURLOP_HTTPHEADER 是您想要的,您可以直接将数组传递给它而无需编码为查询字符串。

e.g。

curl_setopt($curlHandle, CURLOPT_HEADER, 1);
curl_setopt($curlHandle, CURLOPT_HTTPHEADER, $curlHeaders);

答案 1 :(得分:0)

401错误通常表示访问令牌无效或已过期: https://developer.paypal.com/webapps/developer/docs/integration/direct/rest-payments-error-handling/

您是否在授权标题中包含“Bearer”?例如:

Authorization:Bearer EMxItHE7Zl4cMdkvMg-f7c63GQgYZU8FjyPWKQlpsqQP