401与支付网关API交互时

时间:2015-01-04 14:08:04

标签: php json curl

我正在尝试将我的应用程序与支付网关集成。

他们的API基于REST API并使用JSON传输数据。他们有这个示例代码

$ curl -X POST -H "Content-Type: application/json" \
-u APIKEYEXAMPLEAPIKEYEXAMPLE:APIKEYEXAMPLEAPIKEYEXAMPLE \
https://api.netbanx.com/hosted/v1/orders \
-d '{
"merchantRefNum" : "ABCDE12345",
"currencyCode" : "USD",
"totalAmount" : 1234
}'

我正在尝试使用它来开发相同的PHP代码。到目前为止,我的代码是这样的,我接受了下面提供的答案并修改了一下,这是我的代码。

<?php
//API Url
$url = 'https://api.netbanx.com/hosted/v1/orders';

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = array(
    'merchantRefNum' => '89983483',
    'currencyCode' => 'USD',
    'totalAmount' => '1234'
);

//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Set the content type to application/json
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));

//Set the header authorization 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($ch, CURLOPT_USERPWD, 'Authorization: Basic ZGV2Y2VudHJlNDYxNzpCLXFhMi0wLTU0OGEwM2RkLTMwMmQwMjE1MDA4M2VhZTUwYjQxYzQ0ZDIwMTE4OGM3ZmY4Yjc0ZDIxMzUwY2NiMjdiMDIxNDYwMDc4MGFlMTRkM2E2MTEzY2RmMmJkODc5MjJmZjU3MWQwMGY0ZWU=');


//Execute the request
$result = curl_exec($ch);
?>

我的测试API Key devcentre4617:B-qa2-0-548a03dd-302d02150083eae50b41c44d201188c7ff8b74d21350ccb27b0214600780ae14d3a6113cdf2bd87922ff571d00f4ee

正如在他们的文档中所说,我将API密钥转换为base64添加了Basic这个词给了一个空格,并将HTTP Authorizatoin代码作为Basic ZGV2Y2VudHJlNDYxNzpCLXFhMi0wLTU0OGEwM2RkLTMwMmQwMjE1MDA4M2VhZTUwYjQxYzQ0ZDIwMTE4OGM3ZmY4Yjc0ZDIxMzUwY2NiMjdiMDIxNDYwMDc4MGFlMTRkM2E2MTEzY2RmMmJkODc5MjJmZjU3MWQwMGY0ZWU=

执行时我一直在

{"error":{"code":401,"message":"Not authorised"}}

我的代码有问题吗?或者是其他地方的问题?

UPDATE !!!

这是工作代码,问题在于API DOCs测试api是https://api.test.netbanx.com/hosted/v1/orders

<?php
//API Url
$url = 'https://api.test.netbanx.com/hosted/v1/orders';

//Initiate cURL.
$ch = curl_init($url);

//The JSON data.
$jsonData = array(
    'merchantRefNum' => '89983483',
    'currencyCode' => 'USD',
    'totalAmount' => '1234'
);

//Encode the array into JSON.
$jsonDataEncoded = json_encode($jsonData);

//Tell cURL that we want to send a POST request.
curl_setopt($ch, CURLOPT_POST, 1);

//Attach our encoded JSON string to the POST fields.
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);

//Set the content type to application/json and HTTP Authorization code

$headers = array(
    'Content-Type:application/json',
    'Authorization: Basic '. base64_encode("devcentre4617:B-qa2-0-548a03dd-302d02150083eae50b41c44d201188c7ff8b74d21350ccb27b0214600780ae14d3a6113cdf2bd87922ff571d00f4ee") //Base 64 encoding and appending Authorization: Basic 
);

curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

//Execute the request
$result = curl_exec($ch);
?>

1 个答案:

答案 0 :(得分:2)

您需要添加以下用户名和密码:

curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($ch, CURLOPT_USERPWD, 'APIKEYEXAMPLEAPIKEYEXAMPLE:APIKEYEXAMPLEAPIKEYEXAMPLE');
祝你好运:)