doutuo7609 2019-05-30 10:52
浏览 94

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

I'm implementing PayPal payments in my website.

The Paypal API sends me the order details through JSON to my success.php script.

I want to get each variable sent and store it in my database.

So I get the JSON data with :

$RAW=file_get_contents('php://input');

The data I get looks like this (print_r of $RAW) :

{"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"}]}

I decode it in an Array with :

$response=json_decode($RAW, true);

I get this array (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
                )

        )

)

But when I try to insert it in the database :

<?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));

?>

I get this warning :

log PHP Notice: Array to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 57 PHP Notice: Array to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 58 PHP Notice: Array to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 59 PHP Notice: Array to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 60 PHP Notice: Array to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 61 [...] PHP Notice: Array to string conversion in /xxxxxxxxxxxxxxxxxxxxxxxx/success.php on line 87

And nothing is inserted.

What do i do wrong ?

  • 写回答

2条回答 默认 最新

  • dtdh11647 2019-05-30 10:56
    关注

    I think you should call your array elements this way:

    $response['summary']

    You are missing the quotes

    评论

报告相同问题?

悬赏问题

  • ¥20 双层网络上信息-疾病传播
  • ¥50 paddlepaddle pinn
  • ¥20 idea运行测试代码报错问题
  • ¥15 网络监控:网络故障告警通知
  • ¥15 django项目运行报编码错误
  • ¥15 请问这个是什么意思?
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样