Paypal curl_setopt($ch, CURLOPT_POSTFIELDS with PHP 变量

Paypal curl_setopt($ch, CURLOPT_POSTFIELDS with PHP variables

早上好

尝试使用 PHP 和 PayPal API 向我的网站添加自定义 PayPal 订阅。我之前没有真正使用过 PHP 或 Curl,所以它需要一段时间,但我设法编写了一个脚本以从沙箱中获取访问令牌并 link 到现有的订阅计划,你可以订阅。下面的代码有效...

curl_setopt($ch, CURLOPT_POSTFIELDS, "\n  {\n   \"plan_id\":$planID,\n   \"start_time\":$startTime,\n      \"application_context\": {\n        \"brand_name\": \"Sleep Happy Mattress\",\n        \"locale\": \"en-US\",\n        \"shipping_preference\": \"SET_PROVIDED_ADDRESS\",\n        \"user_action\": \"SUBSCRIBE_NOW\",\n        \"payment_method\": {\n          \"payer_selected\": \"PAYPAL\",\n          \"payee_preferred\": \"IMMEDIATE_PAYMENT_REQUIRED\"\n        },\n        \"return_url\": \"https://example.com/returnUrl\",\n        \"cancel_url\": \"https://example.com/cancelUrl\"\n      }\n    }");

但是,每当我尝试将 planID 和 start_time 作为 php 变量包含在 curl_setopt($ch, CURLOPT_POSTFIELDS 中时,我都会收到“请求不正确-已形成、句法不正确或违反架构。”错误。

$planID = 'P-25Y56437062492726MFWZ4GI';
$startTime = '2021-10-22T00:00:00Z';

// 由 curl-to-PHP 生成:http://incarnate.github.io/curl-to-php/

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, 'https://api-m.sandbox.paypal.com/v1/billing/subscriptions');

curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "\n  {\n   \"plan_id\": \"P-25Y56437062492726MFWZ4GI\",\n   \"start_time\": \"2021-10-22T00:00:00Z\",\n      \"application_context\": {\n        \"brand_name\": \"Sleep Happy Mattress\",\n        \"locale\": \"en-UK\",\n        \"shipping_preference\": \"SET_PROVIDED_ADDRESS\",\n        \"user_action\": \"SUBSCRIBE_NOW\",\n        \"payment_method\": {\n          \"payer_selected\": \"PAYPAL\",\n          \"payee_preferred\": \"IMMEDIATE_PAYMENT_REQUIRED\"\n        },\n        \"return_url\": \"https://example.com/returnUrl\",\n        \"cancel_url\": \"https://example.com/cancelUrl\"\n      }\n    }");
curl_setopt($ch, CURLOPT_POSTFIELDS, "\n  {\n   \"plan_id\":$planID,\n   \"start_time\":$startTime,\n      \"application_context\": {\n        \"brand_name\": \"Sleep Happy Mattress\",\n        \"locale\": \"en-US\",\n        \"shipping_preference\": \"SET_PROVIDED_ADDRESS\",\n        \"user_action\": \"SUBSCRIBE_NOW\",\n        \"payment_method\": {\n          \"payer_selected\": \"PAYPAL\",\n          \"payee_preferred\": \"IMMEDIATE_PAYMENT_REQUIRED\"\n        },\n        \"return_url\": \"https://example.com/returnUrl\",\n        \"cancel_url\": \"https://example.com/cancelUrl\"\n      }\n    }");

我的串联是错误的还是变量需要先以某种方式进行操作,或者两者兼而有之??

非常感谢您的帮助

克里斯

请勿指定或包含 start_time,除非您希望结帐时出现金额未显示的问题。

你的问题是你没有在变量值周围加上引号,这对于 JSON 字符串语法是必需的。观察:

\"plan_id\": \"$planID\",

使用 json_encode() 从数组对象构造字符串会更好。

像这样创建 JSON 字符串:

$data = [
    'plan_id' => $planID,
    'start_time' => $startTime,
    'application_context' => [
        'brand_name' => 'Sleep Happy Mattress',
        'locale' => 'en-US',
        'shipping_preference' => 'SET_PROVIDED_ADDRESS',
        'user_action' => 'SUBSCRIBE_NOW',
        'payment_method' => [
            'payer_selected' => 'PAYPAL',
            'payee_preferred' => 'IMMEDIATE_PAYMENT_REQUIRED'
        ],
        'return_url' => 'https://example.com/returnUrl',
        'cancel_url' => 'https://example.com/cancelUrl'
    ],    
];

然后你可以检查嵌套等是否正确(请注意我不知道上面是否正确,我只是从你的问题中复制它),如果你 json_encode 它你将创建格式正确 JSON.

$jsonEncoded = json_encode($data);