如何从JSON解码的PHP多维度数组中获取值? (PayPal API)

时间:2019-05-30 10:52:50

标签: php arrays json multidimensional-array paypal

我正在网站上执行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);

我得到了这个数组($ response的print_r):

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注意:数组到字符串的转换   /xxxxxxxxxxxxxxxxxxxxxxxxxxx/success.php,第57行PHP注意:数组   到/xxxxxxxxxxxxxxxxxxxxxxxx/success.php中的字符串转换   58 PHP注意:在数组到字符串的转换   第59行PHP上的/xxxxxxxxxxxxxxxxxxxxxxxx/success.php注意:数组   到/xxxxxxxxxxxxxxxxxxxxxxxx/success.php中的字符串转换   60 PHP注意:在数组到字符串的转换   /第61行上的/xxxxxxxxxxxxxxxxxxxxxxxx/success.php [...] PHP注意:   数组在/xxxxxxxxxxxxxxxxxxxxxxxx/success.php中转换为字符串   第87行

什么也没插入。

我该怎么办?

2 个答案:

答案 0 :(得分:0)

我认为您应该这样调用数组元素:

  

$ response ['summary']

您缺少引号

答案 1 :(得分:0)

我发现了问题:

我的查询声明缺少多个点,简单和双引号:

$requete="INSERT INTO xxxxxxxxx.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']."'
  );";

现在可以了,谢谢您的帮助!