【发布时间】:2019-10-16 00:38:32
【问题描述】:
我正在我的网站中实施 PayPal 付款。
Paypal API 通过 JSON 将订单详细信息发送到我的 success.php 脚本。
我想发送每个变量并将其存储在我的数据库中。
所以我得到了 JSON 数据:
$RAW=file_get_contents('php://input');
我得到的数据是这样的($RAW 的 print_r):
{"id":"XXXXXXXXXXXXX-XXXXXXXXX","event_version":"1.0","create_time":"2019-05-30T09:58:56.756Z","resource_type":"capture","resource_version":"2.0","event_type":"PAYMENT.CAPTURE.COMPLETED","summary":"Payment completed for EUR 10.0 EUR","resource":{"id":"XXXXXXXXXXXXXXXXXXX","amount":{"currency_code":"EUR","value":"10.00"},"final_capture":true,"seller_protection":{"status":"ELIGIBLE","dispute_categories":["ITEM_NOT_RECEIVED","UNAUTHORIZED_TRANSACTION"]},"seller_receivable_breakdown":{"gross_amount":{"currency_code":"EUR","value":"10.00"},"paypal_fee":{"currency_code":"EUR","value":"0.59"},"net_amount":{"currency_code":"EUR","value":"9.41"}},"status":"COMPLETED","create_time":"2019-05-30T09:58:52Z","update_time":"2019-05-30T09:58:52Z","links":[{"href":"https://api.sandbox.paypal.com/v2/payments/captures/XXXXXXXXXXXXXXX","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v2/payments/captures/XXXXXXXXXXXXX/refund","rel":"refund","method":"POST"},{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/XXXXXXXXXXXXXX","rel":"up","method":"GET"}]},"links":[{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/XXXXXXXXXXXXX-XXXXXXXXXXX","rel":"self","method":"GET"},{"href":"https://api.sandbox.paypal.com/v1/notifications/webhooks-events/XXXXXXXXXXXXXXX-XXXXXXXXXXXXXXXX/resend","rel":"resend","method":"POST"}]}
我在一个数组中解码它:
$response=json_decode($RAW, true);
我得到了这个数组(print_r of $response):
Array
(
[id] => XXXXXXXXXXX-XXXXXXXXXX
[event_version] => 1.0
[create_time] => 2019-05-30T09:58:56.756Z
[resource_type] => capture
[resource_version] => 2.0
[event_type] => PAYMENT.CAPTURE.COMPLETED
[summary] => Payment completed for EUR 10.0 EUR
[resource] => Array
(
[id] => XXXXXXXXXXXXX
[amount] => Array
(
[currency_code] => EUR
[value] => 10.00
)
[final_capture] => 1
[seller_protection] => Array
(
[status] => ELIGIBLE
[dispute_categories] => Array
(
[0] => ITEM_NOT_RECEIVED
[1] => UNAUTHORIZED_TRANSACTION
)
)
[seller_receivable_breakdown] => Array
(
[gross_amount] => Array
(
[currency_code] => EUR
[value] => 10.00
)
[paypal_fee] => Array
(
[currency_code] => EUR
[value] => 0.59
)
[net_amount] => Array
(
[currency_code] => EUR
[value] => 9.41
)
)
[status] => COMPLETED
[create_time] => 2019-05-30T09:58:52Z
[update_time] => 2019-05-30T09:58:52Z
[links] => Array
(
[0] => Array
(
[href] => https://api.sandbox.paypal.com/v2/payments/captures/XXXXXXXXXXX
[rel] => self
[method] => GET
)
[1] => Array
(
[href] => https://api.sandbox.paypal.com/v2/payments/captures/XXXXXXXXXXXX/refund
[rel] => refund
[method] => POST
)
[2] => Array
(
[href] => https://api.sandbox.paypal.com/v2/checkout/orders/XXXXXXXXXXXXXXX
[rel] => up
[method] => GET
)
)
)
[links] => Array
(
[0] => Array
(
[href] => https://api.sandbox.paypal.com/v1/notifications/webhooks-events/XXXXXXXXX-XXXXXX
[rel] => self
[method] => GET
)
[1] => Array
(
[href] => https://api.sandbox.paypal.com/v1/notifications/webhooks-events/XXXXXXX-XXXXXXXXX/resend
[rel] => resend
[method] => POST
)
)
)
但是当我尝试将其插入数据库时:
<?php
$RAW=file_get_contents('php://input');
$response=json_decode($RAW, true);
$sqluser="xxxxxxx";
$sqlpass="xxxx";
$database="xxxxxxxx";
$query="INSERT INTO testpaypal(
webhookId,
event_version,
create_time,
resource_type,
resource_version,
event_type,
summary,
resource__id,
resource__amount__currency_code,
resource__amount__value,
resource__final_capture,
resource__seller_protection__status,
resource__seller_protection__dispute_categories__1,
resource__seller_protection__dispute_categories__2,
resource__seller_receivable__gross_amount__currency_code,
resource__seller_receivable__gross_amount__value,
resource__seller_receivable__paypal_fee__currency_code,
resource__seller_receivable__paypal_fee__value,
resource__seller_receivable__net_amount__currency_code,
resource__seller_receivable__net_amount__value,
resource__status,
resource__create_time,
resource__update_time,
resource__links__href1,
resource__links__rel1,
resource__links__method1,
resource__links__href2,
resource__links__rel2,
resource__links__method2,
resource__links__href3,
resource__links__rel3,
resource__links__method3,
links__href1,
links__rel1,
links__method1,
links__href2,
links__rel2,
links__method2
)
VALUES(
$response[id],
$response[event_version],
$response[create_time],
$response[resource_type],
$response[resource_version],
$response[event_type],
$response[summary],
$response[resource][id],
$response[resource][amount][currency_code],
$response[resource][amount][value],
$response[resource][final_capture],
$response[resource][seller_protection][status],
$response[resource][seller_protection][dispute_categories][0],
$response[resource][seller_protection][dispute_categories][1],
$response[resource][seller_receivable_breakdown][gross_amount][currency_code],
$response[resource][seller_receivable_breakdown][gross_amount][value],
$response[resource][seller_receivable_breakdown][paypal_fee][currency_code],
$response[resource][seller_receivable_breakdown][paypal_fee][value],
$response[resource][seller_receivable_breakdown][net_amount][currency_code],
$response[resource][seller_receivable_breakdown][net_amount][value],
$response[resource][status],
$response[resource][create_time],
$response[resource][update_time],
$response[resource][links][0][href],
$response[resource][links][0][rel],
$response[resource][links][0][method],
$response[resource][links][1][href],
$response[resource][links][1][rel],
$response[resource][links][1][method],
$response[resource][links][2][href],
$response[resource][links][2][rel],
$response[resource][links][2][method],
$response[links][0][href],
$response[links][0][rel],
$response[links][0][method],
$response[links][1]href],
$response[links][1][rel],
$response[links][1][method]
);";
try { $db = new PDO("mysql:host=sqlserver;dbname=$database", $sqluser, $sqlpass); }
catch(Exception $e) { echo 'Error : '.$e->getMessage().''; echo 'N° : '.$e->getCode(); }
$result = $db->query($query);
file_put_contents("post.log", print_r($_POST, true));
file_put_contents("get.log", print_r($_GET, true));
file_put_contents("RAW.log", print_r($RAW, true));
file_put_contents("array.log", print_r($response, true));
?>
我收到此警告:
log PHP 注意:数组到字符串的转换 /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 57 PHP 注意:数组 在/xxxxxxxxxxxxxxxxxxxxxxxx/success.php 中在线转换成字符串 58 PHP注意:数组到字符串的转换 /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 59 PHP 注意:数组 在/xxxxxxxxxxxxxxxxxxxxxxxx/success.php 中在线转换成字符串 60 PHP注意:数组到字符串的转换 /xxxxxxxxxxxxxxxxxxxxxxxx/success.php 第 61 行 [...] PHP 注意: /xxxxxxxxxxxxxxxxxxxxxxxx/success.php 中的数组到字符串的转换 第 87 行
并没有插入任何内容。
我做错了什么?
【问题讨论】:
-
我建议您查看您的数据库设计,将所有这些数据存储在一个表中的想法不是特别好或灵活。
-
所有值 quotes 都不见了
-
每个数据库(表)字段都有类型,您必须提供适当类型的数据来存储它。您不能将字符串存储在数字字段或数组中......几乎任何字段。没有“数组”字段类型。
标签: php arrays json multidimensional-array paypal