2016-05-16 141 views
0

因此,经过大量搜索后,我终于能够使用PHP cURL成功获取我的Paypal访问令牌。现在我试图根据事务ID检索交易细节。到目前为止,我认为我已经获得了正确的通话URL,但是我认为我的发布数据格式可能不正确。所使用的代码如下发现:Paypal通过PHP cURL获取交易详情

<?php 

//this function is for handling post call requests 
function make_post_call($url, $postdata) { 
    global $token; 
    $curl = curl_init($url); 
    curl_setopt($curl, CURLOPT_POST, true); 
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true); 
    curl_setopt($curl, CURLOPT_SSLVERSION , 6); 
    curl_setopt($curl, CURLOPT_HEADER, false); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, array(
       'Authorization: Bearer '.$token, 
       'Accept: application/json', 
       'Content-Type: application/json' 
       )); 

    curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata); 
    $response = curl_exec($curl); 
    print_r($response); //IM NOW RECEIVING OUTPUT, however symbols are now being replaced by placeholders such as '%40', how can i prevent this? 
    if (empty($response)) { 
     die(curl_error($curl)); //close due to error 
     curl_close($curl); 
    } else { 
     $info = curl_getinfo($curl); 
     echo "Time took: " . $info['total_time']*1000 . "ms\n"; 
     curl_close($curl); // close cURL handler 
     if($info['http_code'] != 200 && $info['http_code'] != 201) { 
      echo "Received error: " . $info['http_code']. "\n"; 
      echo "Raw response:".$response."\n"; 
      die(); 
     } 
    } 
    // Convert the result from JSON format to a PHP array 
    $jsonResponse = json_decode($response, TRUE); 
    return $jsonResponse; 
} 

$token = get_access_token($url,$postArgs); //works and returns a valid access token 

//This is the URL for the call 
$url = 'https://api-3t.sandbox.paypal.com/nvp'; 

//This is the data to be sent in the call 
$postdata = array(
'USER' => 'peoplesroewrwwsg_api1.outlook.com', 
'PWD' => 'KR2TVKHGDSHSNDJ6E2', 
'SIGNATURE' => 'AFcWxV21C7fdFHSGGSDGD51G0BJYUWOCSyjUAKPPGs', 
'METHOD' => 'GetTransactionDetails', 
'VERSION' => '123', 
'TransactionID' => '1RE953245246192109' 
); 

$postdata = http_build_query($postdata); 
//$postdata = json_encode($postdata); //Is this the correct way of formatting? 

$transactionInfo = make_post_call($url, $postdata); //get transaction details NOT WORKING 

print_r($transactionInfo); //does not print anything 
?> 

我不是recieving任何卷曲的错误,所以我认为这个问题是无论是数据发送即时消息或如何我格式化。

关于如何做到这一点breif贝宝指南可以在这里找到 - >https://developer.paypal.com/docs/classic/express-checkout/gs_transaction/#tryit 但它是用cURL而不是PHP CURL扩展,所以我不知道如果我正确地发送数据。

宝GetTransactionDetails细节这里 - >https://developer.paypal.com/docs/classic/api/merchant/GetTransactionDetails_API_Operation_NVP/

任何指导,关于数据格式,或其他任何建议,非常感谢!

---------------------------------------------- ----------- UPDATE!------------------------------- ----------------------

现在,当我打印结果一旦执行cURL语句即时通讯接收像这样的信息:

RECEIVERID=GN7SRQEGEREASDV9BQU&EMAIL=peoplesrobotiblabal%40outlook%2ecom&PAYERID=JC5RWUUKWGDFYJYY&PAYERSTATUS=verified&COUNTRYCODE=US&SHIPTONAME=Jane%20Smurf... 

可以看出,某些符号(如句点)正在被诸如'%40'等占位符替换。这个可以识别的魔法?是因为我期待JSON响应?

+1

文档明确指出名称=>值对不JSON。使用'http_build_query()'来构建发布数据。什么让你认为这种回应将会出现在JSON中? – frz3993

+0

你好!非常感谢你的建议(我也只是猜测,响应是JSON,我不知道它是什么)。我将它改回到'http_build_query()'并添加了打印语句,只要在函数内部接收到cURL结果,我就会收到一个sorta正确的输出结果!如何将某些符号替换为占位符,例如'%40' – DiscreteTomatoes

+0

这是执行http请求时需要的urldecoded字符串。您可以在发出curl请求后注释掉所有'print_r'并只执行'var_dump($ POST)'。请更新与结果的问题。我认为API会发出回复邮件请求。 – frz3993

回答

2

要从PHP数组构建一个http查询字符串,您可以使用PHP http_build_query()。例如:

$array = ['username' => 'John', 'password' => '123456abc']; 
$postdata = http_build_query($array); 

您提到的符号是特殊字符的urlencoded形式。 Http请求将要求查询字符串被urlencoded。 %40@的urlencoded形式。如果将来您需要对urlencoded字符串进行编码/解码,则可以使用rawurldecode(),rawurlencode(),urlencode()urldecode()

用于解析来自PAYPAL NVP API的响应。您可以使用parse_str()。实施例1:

$response = 'status=success&code=02'; 
parse_str($response, $response_array); 
//$response_array will contain the $response as array 

实施例2:

$response = 'status=success&code=02'; 
parse_str($response); 
//this will display success 
echo $status; 
//this will display 02 
echo $code;