获取JSON数组并显示来自该数组的结果。

时间:2022-03-17 21:18:15

I want to display on page some data from api call. The data is for bitcoin payments. So I have orders and if customer pay with bitcoins I want to see confirmations, amount etc.

我想在页面上显示一些来自api调用的数据。数据是用于支付比特币的。我有订单,如果客户用比特币付款,我想看到确认,金额等等。

Here is one example url which return json data.

这里有一个返回json数据的示例url。

Here is what I'm trying in my controller

这是我在控制器中尝试的。

public function ordersView($orderId) {
    /** @var Order $order */
    $order = Order::where('order_id', $orderId)->first();
    if (!$order) {
        App::abort(404);
    }

    $url="http://btc.blockr.io/api/v1/tx/info/9585d5f635eddf737c8351bfe0879c3dbef3d94de9feda2bd74c990b06b7dc52";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
    $result=curl_exec($ch); 
    curl_close($ch);

    $total = file_get_contents($url);

    return View::make('site.admin.orders_view', [
        'order' => $order,
        'total' => $total
    ]);
}

And then on the view

然后在视图上

@foreach($order->getOrderData($order->data) as $itemId => $item)

     // some product info like name, description etc..

    @foreach($total as $i => $totals)
            {{ $totals['confirmations'] }}
            {{ $totals['time_utc'] }}
    @endforeach
@endforeach

Current error which I get is

我得到的当前错误是

'Invalid argument supplied for foreach()

On the inner foreach

内foreach

    @foreach($total as $i => $totals)
            {{ $totals['confirmations'] }}
            {{ $totals['time_utc'] }}
    @endforeach

Can someone help me how exactly I can parse this data?

谁能帮助我准确地解析这些数据吗?

1 个解决方案

#1


1  

You need to convert your response in array formate because your response is json SO try this using json_decode() function you convert json to array...

您需要以数组形式转换响应,因为您的响应是json,所以请尝试使用json_decode()函数将json转换为数组……

    $url="http://btc.blockr.io/api/v1/tx/info/9585d5f635eddf737c8351bfe0879c3dbef3d94de9feda2bd74c990b06b7dc52";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
    $result=curl_exec($ch); 
    curl_close($ch);

    $total =json_decode( file_get_contents($url),true);

And Your response is...

和你的反应是……

Array
(
    [status] => success
    [data] => Array
        (
            [tx] => 9585d5f635eddf737c8351bfe0879c3dbef3d94de9feda2bd74c990b06b7dc52
            [block] => 429605
            [confirmations] => 468
            [time_utc] => 2016-09-13T12:59:24Z
            [is_coinbased] => 0
            [trade] => Array
                (
                    [vins] => Array
                        (
                            [0] => Array
                                (
                                    [address] => 19c9JnCoyRqUkUjJkbdK9qDApk2a5Vy558
                                    [is_nonstandard] => 
                                    [amount] => -0.103855
                                    [n] => 0
                                    [type] => 0
                                    [vout_tx] => ebe35a1aee39c17668a26a6d91d49e83651ee1c5fe28f23130202e617054e545
                                )

                        )

                    [vouts] => Array
                        (
                            [0] => Array
                                (
                                    [address] => 1DsRkxNy8LAAbi6kEh6ZoeZGjAVBuia4uw
                                    [is_nonstandard] => 
                                    [amount] => 0.00295729
                                    [n] => 0
                                    [type] => 1
                                    [is_spent] => 1
                                )

                            [1] => Array
                                (
                                    [address] => 1NusFWAG3mgkLszWo5oH13FLbwVaJZ6aRL
                                    [is_nonstandard] => 
                                    [amount] => 0.10069771
                                    [n] => 1
                                    [type] => 1
                                    [is_spent] => 0
                                )

                        )

                )

            [vins] => Array
                (
                    [0] => Array
                        (
                            [address] => 19c9JnCoyRqUkUjJkbdK9qDApk2a5Vy558
                            [is_nonstandard] => 
                            [amount] => -0.10385500
                            [n] => 0
                            [type] => 0
                            [vout_tx] => ebe35a1aee39c17668a26a6d91d49e83651ee1c5fe28f23130202e617054e545
                        )

                )

            [vouts] => Array
                (
                    [0] => Array
                        (
                            [address] => 1DsRkxNy8LAAbi6kEh6ZoeZGjAVBuia4uw
                            [is_nonstandard] => 
                            [amount] => 0.00295729
                            [n] => 0
                            [type] => 1
                            [is_spent] => 1
                            [extras] => Array
                                (
                                    [asm] => OP_DUP OP_HASH160 8d2af96bbb1c0464c8129db247458769b6767a10 OP_EQUALVERIFY OP_CHECKSIG
                                    [script] => 76a9148d2af96bbb1c0464c8129db247458769b6767a1088ac
                                    [reqSigs] => 1
                                    [type] => pubkeyhash
                                )

                        )

                    [1] => Array
                        (
                            [address] => 1NusFWAG3mgkLszWo5oH13FLbwVaJZ6aRL
                            [is_nonstandard] => 
                            [amount] => 0.10069771
                            [n] => 1
                            [type] => 1
                            [is_spent] => 0
                            [extras] => Array
                                (
                                    [asm] => OP_DUP OP_HASH160 f05a37d55fa0512b32320cf362bb96f94d886259 OP_EQUALVERIFY OP_CHECKSIG
                                    [script] => 76a914f05a37d55fa0512b32320cf362bb96f94d88625988ac
                                    [reqSigs] => 1
                                    [type] => pubkeyhash
                                )

                        )

                )

            [fee] => 0.00020000
            [days_destroyed] => 0.05
            [is_unconfirmed] => 
            [extras] => 
        )

    [code] => 200
    [message] => 
)

#1


1  

You need to convert your response in array formate because your response is json SO try this using json_decode() function you convert json to array...

您需要以数组形式转换响应,因为您的响应是json,所以请尝试使用json_decode()函数将json转换为数组……

    $url="http://btc.blockr.io/api/v1/tx/info/9585d5f635eddf737c8351bfe0879c3dbef3d94de9feda2bd74c990b06b7dc52";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL,$url);
    $result=curl_exec($ch); 
    curl_close($ch);

    $total =json_decode( file_get_contents($url),true);

And Your response is...

和你的反应是……

Array
(
    [status] => success
    [data] => Array
        (
            [tx] => 9585d5f635eddf737c8351bfe0879c3dbef3d94de9feda2bd74c990b06b7dc52
            [block] => 429605
            [confirmations] => 468
            [time_utc] => 2016-09-13T12:59:24Z
            [is_coinbased] => 0
            [trade] => Array
                (
                    [vins] => Array
                        (
                            [0] => Array
                                (
                                    [address] => 19c9JnCoyRqUkUjJkbdK9qDApk2a5Vy558
                                    [is_nonstandard] => 
                                    [amount] => -0.103855
                                    [n] => 0
                                    [type] => 0
                                    [vout_tx] => ebe35a1aee39c17668a26a6d91d49e83651ee1c5fe28f23130202e617054e545
                                )

                        )

                    [vouts] => Array
                        (
                            [0] => Array
                                (
                                    [address] => 1DsRkxNy8LAAbi6kEh6ZoeZGjAVBuia4uw
                                    [is_nonstandard] => 
                                    [amount] => 0.00295729
                                    [n] => 0
                                    [type] => 1
                                    [is_spent] => 1
                                )

                            [1] => Array
                                (
                                    [address] => 1NusFWAG3mgkLszWo5oH13FLbwVaJZ6aRL
                                    [is_nonstandard] => 
                                    [amount] => 0.10069771
                                    [n] => 1
                                    [type] => 1
                                    [is_spent] => 0
                                )

                        )

                )

            [vins] => Array
                (
                    [0] => Array
                        (
                            [address] => 19c9JnCoyRqUkUjJkbdK9qDApk2a5Vy558
                            [is_nonstandard] => 
                            [amount] => -0.10385500
                            [n] => 0
                            [type] => 0
                            [vout_tx] => ebe35a1aee39c17668a26a6d91d49e83651ee1c5fe28f23130202e617054e545
                        )

                )

            [vouts] => Array
                (
                    [0] => Array
                        (
                            [address] => 1DsRkxNy8LAAbi6kEh6ZoeZGjAVBuia4uw
                            [is_nonstandard] => 
                            [amount] => 0.00295729
                            [n] => 0
                            [type] => 1
                            [is_spent] => 1
                            [extras] => Array
                                (
                                    [asm] => OP_DUP OP_HASH160 8d2af96bbb1c0464c8129db247458769b6767a10 OP_EQUALVERIFY OP_CHECKSIG
                                    [script] => 76a9148d2af96bbb1c0464c8129db247458769b6767a1088ac
                                    [reqSigs] => 1
                                    [type] => pubkeyhash
                                )

                        )

                    [1] => Array
                        (
                            [address] => 1NusFWAG3mgkLszWo5oH13FLbwVaJZ6aRL
                            [is_nonstandard] => 
                            [amount] => 0.10069771
                            [n] => 1
                            [type] => 1
                            [is_spent] => 0
                            [extras] => Array
                                (
                                    [asm] => OP_DUP OP_HASH160 f05a37d55fa0512b32320cf362bb96f94d886259 OP_EQUALVERIFY OP_CHECKSIG
                                    [script] => 76a914f05a37d55fa0512b32320cf362bb96f94d88625988ac
                                    [reqSigs] => 1
                                    [type] => pubkeyhash
                                )

                        )

                )

            [fee] => 0.00020000
            [days_destroyed] => 0.05
            [is_unconfirmed] => 
            [extras] => 
        )

    [code] => 200
    [message] => 
)