gpt4 book ai didi

json中的php变量

转载 作者:太空宇宙 更新时间:2023-11-03 15:45:54 25 4
gpt4 key购买 nike

我有这段用于 paypal 单次支付调用的代码,我想添加一个 php 变量,其中价格为“值”:".$planPrice."如果我输入任何数字而不是 $planPrice,例如:“1000”,它可以正常工作并且我收到通知,但是当我添加 php 变量时,它会这样说:

Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Invalid JSON String' in C:\xampp\htdocs\website\paypal_payouts\PayPal-PHP-SDK\paypal\rest-api-sdk-php\lib\PayPal\Validation\JsonValidator.php:29 Stack trace: #0 C:\xampp\htdocs\website\paypal_payouts\PayPal-PHP-SDK\paypal\rest-api-sdk-php\lib\PayPal\Common\PayPalModel.php(50): PayPal\Validation\JsonValidator::validate('{\r\n ...') #1 C:\xampp\htdocs\website\paypal_payouts\CreateSinglePayout.php(67): PayPal\Common\PayPalModel->__construct('{\r\n ...') #2 C:\xampp\htdocs\website\admin\reports\paynow.php(19): include('C:\xampp\htdocs...') #3 {main} thrown in C:\xampp\htdocs\website\paypal_payouts\PayPal-PHP-SDK\paypal\rest-api-sdk-php\lib\PayPal\Validation\JsonValidator.php on line 29

这是php中的代码:

$senderItem = new \PayPal\Api\PayoutItem();
$senderItem->setRecipientType('Email')
->setNote('Thanks for your patronage!')
->setReceiver($coachUsername)
->setSenderItemId("2014031400023")
->setAmount(new \PayPal\Api\Currency("{
'value':".$planPrice.",
'currency':'USD'
}"));

最佳答案

You don't need to pass a JSON string to Paypal\Api\Currency ,使用数组会更简单:

$senderItem = new \PayPal\Api\PayoutItem();
$senderItem->setRecipientType('Email')
->setNote('Thanks for your patronage!')
->setReceiver($coachUsername)
->setSenderItemId("2014031400023")
->setAmount(new \PayPal\Api\Currency([
'value' => $planPrice,
'currency' => 'USD'
]));

但如果你必须使用 JSON,则它需要有效。 JSON 只允许对字符串和键名使用引号:

$senderItem = new \PayPal\Api\PayoutItem();
$senderItem->setRecipientType('Email')
->setNote('Thanks for your patronage!')
->setReceiver($coachUsername)
->setSenderItemId("2014031400023")
->setAmount(new \PayPal\Api\Currency('{
"value":'.$planPrice.',
"currency":"USD"
}'));

(我在这里假设 "value" 应该设置为一个 JSON 数字。如果它应该是一个 JSON 字符串,请将倒数第三行替换为 "value": "' . $planPrice . '", 代替。)

关于json中的php变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35085940/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com