第一步:获取access_token:
<?php
$clientId = ''; // 替换为你的 PayPal Client ID
$clientSecret = ''; // 替换为你的 PayPal Client Secret
// PayPal API 请求的 URL
$url = "https://api-m.sandbox.paypal.com/v1/oauth2/token";
// 初始化 cURL
$ch = curl_init();
// 设置 cURL 请求的参数
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERPWD, $clientId . ":" . $clientSecret);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_POSTFIELDS, "grant_type=client_credentials");
// 执行请求
$response = curl_exec($ch);
// 检查请求是否出错
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
} else {
// 打印响应结果
echo $response;
}
// 关闭 cURL
curl_close($ch);
第二步:创建订单: