Paypal 使用 PHP cURL 获取交易详情
Paypal get transaction details with PHP cURL
所以经过大量搜索后,我终于能够使用 PHP cURL 成功获取我的 Paypal 访问令牌。现在我正在尝试根据交易 ID 检索交易详细信息。到目前为止,我认为我的调用 URL 是正确的,但是我认为我的 post 数据格式可能不正确。使用的代码如下:
<?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
?>
我没有收到任何 cURL 错误,所以我认为问题要么是我发送的数据,要么是我如何格式化它。
可在此处找到有关如何执行此操作的简短 Paypal 指南-> https://developer.paypal.com/docs/classic/express-checkout/gs_transaction/#tryit
但是它是用 cURL 而不是 PHP cURL 扩展名写的,所以我不确定我是否正确发送数据。
Paypal GetTransactionDetails 详情-> https://developer.paypal.com/docs/classic/api/merchant/GetTransactionDetails_API_Operation_NVP/
非常感谢有关数据格式的任何指导或任何其他建议!
-------------------------------------------- - - - - - - 更新! - - - - - - - - - - - - - - - - - - ----------------------
现在,当我在执行 cURL 语句后立即打印结果时,我收到的信息如下:
RECEIVERID=GN7SRQEGEREASDV9BQU&EMAIL=peoplesrobotiblabal%40outlook%2ecom&PAYERID=JC5RWUUKWGDFYJYY&PAYERSTATUS=verified&COUNTRYCODE=US&SHIPTONAME=Jane%20Smurf...
可以看出,一些符号(例如句点)正在被占位符(例如“%40”)替换。能否确定其来源?是因为我期待 JSON 回复吗?
要从 PHP 数组构建 http 查询字符串,您可以使用 PHP http_build_query()
。示例:
$array = ['username' => 'John', 'password' => '123456abc'];
$postdata = http_build_query($array);
你说的符号是特殊字符的urlencoded形式。 Http 请求将要求对查询字符串进行 urlencoded。 %40
是 @
的 urlencoded 形式。如果将来您需要 encoding/decoding to/from 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;
<?php
function get_transaction_details( $transaction_id ) {
$api_request = 'USER=' . urlencode( 'api_username' )
. '&PWD=' . urlencode( 'api_password' )
. '&SIGNATURE=' . urlencode( 'api_signature' )
. '&VERSION=76.0'
. '&METHOD=GetTransactionDetails'
. '&TransactionID=' . $transaction_id;
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'https://api-3t.sandbox.paypal.com/nvp' ); // For live transactions, change to 'https://api-3t.paypal.com/nvp'
curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
// Uncomment these to turn off server and peer verification
// 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 );
// Set the API parameters for this transaction
curl_setopt( $ch, CURLOPT_POSTFIELDS, $api_request );
// Request response from PayPal
$response = curl_exec( $ch );
// print_r($response);
// If no response was received from PayPal there is no point parsing the response
if( ! $response )
die( 'Calling PayPal to change_subscription_status failed: ' . curl_error( $ch ) . '(' . curl_errno( $ch ) . ')' );
curl_close( $ch );
// An associative array is more usable than a parameter string
parse_str( $response, $parsed_response );
return $parsed_response;
}
$response = get_transaction_details('transaction_id');
echo "<pre>"; print_r($response);
?>
从 cURL 接收信息
$str = RECEIVERID=GN7SRQEGEREASDV9BQU&EMAIL=peoplesrobotiblabal%40outlook%2ecom&PAYERID=JC5RWUUKWGDFYJYY&PAYERSTATUS=verified&COUNTRYCODE=US&SHIPTONAME=Jane%20Smurf...
请使用parse_str("$str"); // add your value here, it will give you proper data format
更多详情:https://www.w3schools.com/php/func_string_parse_str.asp
所以经过大量搜索后,我终于能够使用 PHP cURL 成功获取我的 Paypal 访问令牌。现在我正在尝试根据交易 ID 检索交易详细信息。到目前为止,我认为我的调用 URL 是正确的,但是我认为我的 post 数据格式可能不正确。使用的代码如下:
<?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
?>
我没有收到任何 cURL 错误,所以我认为问题要么是我发送的数据,要么是我如何格式化它。
可在此处找到有关如何执行此操作的简短 Paypal 指南-> https://developer.paypal.com/docs/classic/express-checkout/gs_transaction/#tryit 但是它是用 cURL 而不是 PHP cURL 扩展名写的,所以我不确定我是否正确发送数据。
Paypal GetTransactionDetails 详情-> https://developer.paypal.com/docs/classic/api/merchant/GetTransactionDetails_API_Operation_NVP/
非常感谢有关数据格式的任何指导或任何其他建议!
-------------------------------------------- - - - - - - 更新! - - - - - - - - - - - - - - - - - - ----------------------
现在,当我在执行 cURL 语句后立即打印结果时,我收到的信息如下:
RECEIVERID=GN7SRQEGEREASDV9BQU&EMAIL=peoplesrobotiblabal%40outlook%2ecom&PAYERID=JC5RWUUKWGDFYJYY&PAYERSTATUS=verified&COUNTRYCODE=US&SHIPTONAME=Jane%20Smurf...
可以看出,一些符号(例如句点)正在被占位符(例如“%40”)替换。能否确定其来源?是因为我期待 JSON 回复吗?
要从 PHP 数组构建 http 查询字符串,您可以使用 PHP http_build_query()
。示例:
$array = ['username' => 'John', 'password' => '123456abc'];
$postdata = http_build_query($array);
你说的符号是特殊字符的urlencoded形式。 Http 请求将要求对查询字符串进行 urlencoded。 %40
是 @
的 urlencoded 形式。如果将来您需要 encoding/decoding to/from 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;
<?php
function get_transaction_details( $transaction_id ) {
$api_request = 'USER=' . urlencode( 'api_username' )
. '&PWD=' . urlencode( 'api_password' )
. '&SIGNATURE=' . urlencode( 'api_signature' )
. '&VERSION=76.0'
. '&METHOD=GetTransactionDetails'
. '&TransactionID=' . $transaction_id;
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, 'https://api-3t.sandbox.paypal.com/nvp' ); // For live transactions, change to 'https://api-3t.paypal.com/nvp'
curl_setopt( $ch, CURLOPT_VERBOSE, 1 );
// Uncomment these to turn off server and peer verification
// 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 );
// Set the API parameters for this transaction
curl_setopt( $ch, CURLOPT_POSTFIELDS, $api_request );
// Request response from PayPal
$response = curl_exec( $ch );
// print_r($response);
// If no response was received from PayPal there is no point parsing the response
if( ! $response )
die( 'Calling PayPal to change_subscription_status failed: ' . curl_error( $ch ) . '(' . curl_errno( $ch ) . ')' );
curl_close( $ch );
// An associative array is more usable than a parameter string
parse_str( $response, $parsed_response );
return $parsed_response;
}
$response = get_transaction_details('transaction_id');
echo "<pre>"; print_r($response);
?>
从 cURL 接收信息
$str = RECEIVERID=GN7SRQEGEREASDV9BQU&EMAIL=peoplesrobotiblabal%40outlook%2ecom&PAYERID=JC5RWUUKWGDFYJYY&PAYERSTATUS=verified&COUNTRYCODE=US&SHIPTONAME=Jane%20Smurf...
请使用parse_str("$str"); // add your value here, it will give you proper data format
更多详情:https://www.w3schools.com/php/func_string_parse_str.asp