从api响应中获取数据

时间:2017-07-14 06:13:35

标签: php

我有来自api的回复。我希望在此密钥中显示[line_items]密钥的所有数据,所有食品都像食品名称,食品成本,客户订购的食品数量一样。我无法理解我错在哪里。 这是我的代码。

<?php 
$url = 'http://localhost/cook-n-share/view-order/?cook_id='.$currnet_uid.''; //echo $url;
$content = file_get_contents($url);
$json = json_decode($content, true); 
echo '<pre>';
print_r($json);
$i=1;
foreach( $json as $value){   ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php  echo $value['order']['billing_address']['first_name']; ?></td>
<td><?php echo $value['order']['line_items'][0]['name']; ?></td>
<td id="status"><?php echo $value['order']['status']; ?></td>
<?php } ?>
  

打印输出($ json)

Array
(
    [0] => Array
        (
            [order] => Array
                (
                    [id] => 172
                    [order_number] => 172
                    [created_at] => 2017-07-13T07:43:48Z
                    [updated_at] => 2017-07-13T07:43:54Z
                    [completed_at] => 1970-01-01T00:00:00Z
                    [status] => processing
                    [currency] => GBP
                    [total] => 140.00
                    [subtotal] => 140.00
                    [total_line_items_quantity] => 2
                    [total_tax] => 0.00
                    [total_shipping] => 0.00
                    [cart_tax] => 0.00
                    [shipping_tax] => 0.00
                    [total_discount] => 0.00
                    [shipping_methods] => 
                    [payment_details] => Array
                        (
                            [method_id] => cod
                            [method_title] => Cash on delivery
                            [paid] => 1
                        )

                [billing_address] => Array
                    (
                        [first_name] => Virat
                        [last_name] => Kohli
                        [company] => 
                        [address_1] => Noida sector 2
                        [address_2] => 
                        [city] => Noida
                        [state] => UP
                        [postcode] => 201301
                        [country] => IN
                        [email] => virat@bcc.com
                        [phone] => 8010695858
                    )

                [shipping_address] => Array
                    (
                        [first_name] => 
                        [last_name] => 
                        [company] => 
                        [address_1] => 
                        [address_2] => 
                        [city] => 
                        [state] => 
                        [postcode] => 
                        [country] => 
                    )

                [note] => 
                [customer_ip] => ::1
                [customer_user_agent] => mozilla/5.0 (windows nt 6.3) applewebkit/537.36 (khtml, like gecko) chrome/59.0.3071.115 safari/537.36
                [customer_id] => 31
                [view_order_url] => http://localhost/cook-n-share/my-account/view-order/172
                [line_items] => Array
                    (
                        [0] => Array
                            (
                                [id] => 37
                                [subtotal] => 140.00
                                [subtotal_tax] => 0.00
                                [total] => 140.00
                                [total_tax] => 0.00
                                [price] => 70.00
                                [quantity] => 2
                                [tax_class] => 
                                [name] => Masala Dosa
                                [product_id] => 169
                                [sku] => 
                                [meta] => Array
                                    (
                                    )

                            )

                    )

                [shipping_lines] => Array
                    (
                    )

                [tax_lines] => Array
                    (
                    )

                [fee_lines] => Array
                    (
                    )

                [coupon_lines] => Array
                    (
                    )

                [customer] => Array
                    (
                        [id] => 31
                        [created_at] => 2017-06-21T05:48:18Z
                        [email] => virat@bcc.com
                        [first_name] => Virat
                        [last_name] => Kohli
                        [username] => virat@bcc.com
                        [role] => subscriber
                        [last_order_id] => 172
                        [last_order_date] => 2017-07-13T07:43:48Z
                        [orders_count] => 1
                        [total_spent] => 140.00
                        [avatar_url] => http://1.gravatar.com/avatar/d0d7d5f364a1468aef28ba15651e545a?s=96
                        [billing_address] => Array
                            (
                                [first_name] => Virat
                                [last_name] => Kohli
                                [company] => 
                                [address_1] => Noida sector 2
                                [address_2] => 
                                [city] => Noida
                                [state] => UP
                                [postcode] => 201301
                                [country] => IN
                                [email] => virat@bcc.com
                                [phone] => 8010695858
                            )

                        [shipping_address] => Array
                            (
                                [first_name] => 
                                [last_name] => 
                                [company] => 
                                [address_1] => 
                                [address_2] => 
                                [city] => 
                                [state] => 
                                [postcode] => 
                                [country] => 
                            )

                    )

            )

        [http] => Array
            (
                [request] => Array
                    (
                        [headers] => Array
                            (
                                [0] => Accept: application/json
                                [1] => Content-Type: application/json
                                [2] => User-Agent: WooCommerce API Client-PHP/2.0.1
                            )

                        [method] => GET
                        [url] => http://localhost/cook-n-share/wc-api/v2/orders/172?oauth_consumer_key=ck_24293582cc668729aec0901d59a1d23c4c88a6a0&oauth_timestamp=1500012282&oauth_nonce=8780f3def07fefa2139d66046f04e655a16f1d6e&oauth_signature_method=HMAC-SHA256&oauth_signature=ir2ZjbuuUfjgAXtx%2F4UhAK2oaOb6yGvLvNHXLX1CPRA%3D
                        [params] => Array
                            (
                                [oauth_consumer_key] => ck_24293582cc668729aec0901d59a1d23c4c88a6a0
                                [oauth_timestamp] => 1500012282
                                [oauth_nonce] => 8780f3def07fefa2139d66046f04e655a16f1d6e
                                [oauth_signature_method] => HMAC-SHA256
                                [oauth_signature] => ir2ZjbuuUfjgAXtx/4UhAK2oaOb6yGvLvNHXLX1CPRA=
                            )

                        [data] => Array
                            (
                            )

                        [body] => 
                        [duration] => 1.00477
                    )

                [response] => Array
                    (
                        [body] => {"order":{"id":172,"order_number":"172","created_at":"2017-07-13T07:43:48Z","updated_at":"2017-07-13T07:43:54Z","completed_at":"1970-01-01T00:00:00Z","status":"processing","currency":"GBP","total":"140.00","subtotal":"140.00","total_line_items_quantity":2,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"cod","method_title":"Cash on delivery","paid":true},"billing_address":{"first_name":"Virat","last_name":"Kohli","company":"","address_1":"Noida sector 2","address_2":"","city":"Noida","state":"UP","postcode":"201301","country":"IN","email":"virat@bcc.com","phone":"8010695858"},"shipping_address":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""},"note":"","customer_ip":"::1","customer_user_agent":"mozilla\/5.0 (windows nt 6.3) applewebkit\/537.36 (khtml, like gecko) chrome\/59.0.3071.115 safari\/537.36","customer_id":31,"view_order_url":"http:\/\/localhost\/cook-n-share\/my-account\/view-order\/172","line_items":[{"id":37,"subtotal":"140.00","subtotal_tax":"0.00","total":"140.00","total_tax":"0.00","price":"70.00","quantity":2,"tax_class":"","name":"Masala Dosa","product_id":169,"sku":"","meta":[]}],"shipping_lines":[],"tax_lines":[],"fee_lines":[],"coupon_lines":[],"customer":{"id":31,"created_at":"2017-06-21T05:48:18Z","email":"virat@bcc.com","first_name":"Virat","last_name":"Kohli","username":"virat@bcc.com","role":"subscriber","last_order_id":172,"last_order_date":"2017-07-13T07:43:48Z","orders_count":1,"total_spent":"140.00","avatar_url":"http:\/\/1.gravatar.com\/avatar\/d0d7d5f364a1468aef28ba15651e545a?s=96","billing_address":{"first_name":"Virat","last_name":"Kohli","company":"","address_1":"Noida sector 2","address_2":"","city":"Noida","state":"UP","postcode":"201301","country":"IN","email":"virat@bcc.com","phone":"8010695858"},"shipping_address":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""}}}}
                        [code] => 200
                        [headers] => Array
                            (
                                [Date] =>  Fri, 14 Jul 2017 06:04:42 GMT
                                [Server] =>  Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/7.0.13
                                [X-Powered-By] =>  PHP/7.0.13
                                [Content-Length] =>  2095
                                [Content-Type] =>  application/json; charset=UTF-8
                            )

                    )

            )

    )

[1] => Array
    (
        [order] => Array
            (
                [id] => 173
                [order_number] => 173
                [created_at] => 2017-07-13T07:46:07Z
                [updated_at] => 2017-07-13T07:46:12Z
                [completed_at] => 1970-01-01T00:00:00Z
                [status] => processing
                [currency] => GBP
                [total] => 240.00
                [subtotal] => 240.00
                [total_line_items_quantity] => 3
                [total_tax] => 0.00
                [total_shipping] => 0.00
                [cart_tax] => 0.00
                [shipping_tax] => 0.00
                [total_discount] => 0.00
                [shipping_methods] => 
                [payment_details] => Array
                    (
                        [method_id] => cod
                        [method_title] => Cash on delivery
                        [paid] => 1
                    )

                [billing_address] => Array
                    (
                        [first_name] => Gautam
                        [last_name] => kumar
                        [company] => 
                        [address_1] => Ashok Nagar
                        [address_2] => 
                        [city] => New Delhi
                        [state] => DL
                        [postcode] => 110096
                        [country] => IN
                        [email] => gautam@cubewires.co.in
                        [phone] => 8574234554
                    )

                [shipping_address] => Array
                    (
                        [first_name] => 
                        [last_name] => 
                        [company] => 
                        [address_1] => 
                        [address_2] => 
                        [city] => 
                        [state] => 
                        [postcode] => 
                        [country] => 
                    )

                [note] => 
                [customer_ip] => ::1
                [customer_user_agent] => mozilla/5.0 (windows nt 6.3) applewebkit/537.36 (khtml, like gecko) chrome/59.0.3071.115 safari/537.36
                [customer_id] => 28
                [view_order_url] => http://localhost/cook-n-share/my-account/view-order/173
                [line_items] => Array
                    (
                        [0] => Array
                            (
                                [id] => 38
                                [subtotal] => 70.00
                                [subtotal_tax] => 0.00
                                [total] => 70.00
                                [total_tax] => 0.00
                                [price] => 70.00
                                [quantity] => 1
                                [tax_class] => 
                                [name] => Masala Dosa
                                [product_id] => 169
                                [sku] => 
                                [meta] => Array
                                    (
                                    )

                            )

                        [1] => Array
                            (
                                [id] => 39
                                [subtotal] => 70.00
                                [subtotal_tax] => 0.00
                                [total] => 70.00
                                [total_tax] => 0.00
                                [price] => 70.00
                                [quantity] => 1
                                [tax_class] => 
                                [name] => Masala Dosa
                                [product_id] => 158
                                [sku] => 
                                [meta] => Array
                                    (
                                    )

                            )

                        [2] => Array
                            (
                                [id] => 40
                                [subtotal] => 100.00
                                [subtotal_tax] => 0.00
                                [total] => 100.00
                                [total_tax] => 0.00
                                [price] => 100.00
                                [quantity] => 1
                                [tax_class] => 
                                [name] => Kadhai Paneer
                                [product_id] => 149
                                [sku] => 
                                [meta] => Array
                                    (
                                    )

                            )

                    )

                [shipping_lines] => Array
                    (
                    )

                [tax_lines] => Array
                    (
                    )

                [fee_lines] => Array
                    (
                    )

                [coupon_lines] => Array
                    (
                    )

                [customer] => Array
                    (
                        [id] => 28
                        [created_at] => 2017-06-16T04:50:25Z
                        [email] => gautam@cubewires.co.in
                        [first_name] => Gautam
                        [last_name] => kumar
                        [username] => gautam@cubewires.co.in
                        [role] => subscriber
                        [last_order_id] => 173
                        [last_order_date] => 2017-07-13T07:46:07Z
                        [orders_count] => 1
                        [total_spent] => 240.00
                        [avatar_url] => http://0.gravatar.com/avatar/0881970233cc5dc62abae09452a843e1?s=96
                        [billing_address] => Array
                            (
                                [first_name] => Gautam
                                [last_name] => kumar
                                [company] => 
                                [address_1] => Ashok Nagar
                                [address_2] => 
                                [city] => New Delhi
                                [state] => DL
                                [postcode] => 110096
                                [country] => IN
                                [email] => gautam@cubewires.co.in
                                [phone] => 8574234554
                            )

                        [shipping_address] => Array
                            (
                                [first_name] => 
                                [last_name] => 
                                [company] => 
                                [address_1] => 
                                [address_2] => 
                                [city] => 
                                [state] => 
                                [postcode] => 
                                [country] => 
                            )

                    )

            )

        [http] => Array
            (
                [request] => Array
                    (
                        [headers] => Array
                            (
                                [0] => Accept: application/json
                                [1] => Content-Type: application/json
                                [2] => User-Agent: WooCommerce API Client-PHP/2.0.1
                            )

                        [method] => GET
                        [url] => http://localhost/cook-n-share/wc-api/v2/orders/173?oauth_consumer_key=ck_24293582cc668729aec0901d59a1d23c4c88a6a0&oauth_timestamp=1500012283&oauth_nonce=4a6dbecc9e084ef791d368909f9f0cbd63352784&oauth_signature_method=HMAC-SHA256&oauth_signature=X0GFrK22vT7RcdLGev2fxKReRQRmZd97N9cLRdosW%2F0%3D
                        [params] => Array
                            (
                                [oauth_consumer_key] => ck_24293582cc668729aec0901d59a1d23c4c88a6a0
                                [oauth_timestamp] => 1500012283
                                [oauth_nonce] => 4a6dbecc9e084ef791d368909f9f0cbd63352784
                                [oauth_signature_method] => HMAC-SHA256
                                [oauth_signature] => X0GFrK22vT7RcdLGev2fxKReRQRmZd97N9cLRdosW/0=
                            )

                        [data] => Array
                            (
                            )

                        [body] => 
                        [duration] => 0.97498
                    )

                [response] => Array
                    (
                        [body] => {"order":{"id":173,"order_number":"173","created_at":"2017-07-13T07:46:07Z","updated_at":"2017-07-13T07:46:12Z","completed_at":"1970-01-01T00:00:00Z","status":"processing","currency":"GBP","total":"240.00","subtotal":"240.00","total_line_items_quantity":3,"total_tax":"0.00","total_shipping":"0.00","cart_tax":"0.00","shipping_tax":"0.00","total_discount":"0.00","shipping_methods":"","payment_details":{"method_id":"cod","method_title":"Cash on delivery","paid":true},"billing_address":{"first_name":"Gautam","last_name":"kumar","company":"","address_1":"Ashok Nagar","address_2":"","city":"New Delhi","state":"DL","postcode":"110096","country":"IN","email":"gautam@cubewires.co.in","phone":"8574234554"},"shipping_address":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""},"note":"","customer_ip":"::1","customer_user_agent":"mozilla\/5.0 (windows nt 6.3) applewebkit\/537.36 (khtml, like gecko) chrome\/59.0.3071.115 safari\/537.36","customer_id":28,"view_order_url":"http:\/\/localhost\/cook-n-share\/my-account\/view-order\/173","line_items":[{"id":38,"subtotal":"70.00","subtotal_tax":"0.00","total":"70.00","total_tax":"0.00","price":"70.00","quantity":1,"tax_class":"","name":"Masala Dosa","product_id":169,"sku":"","meta":[]},{"id":39,"subtotal":"70.00","subtotal_tax":"0.00","total":"70.00","total_tax":"0.00","price":"70.00","quantity":1,"tax_class":"","name":"Masala Dosa","product_id":158,"sku":"","meta":[]},{"id":40,"subtotal":"100.00","subtotal_tax":"0.00","total":"100.00","total_tax":"0.00","price":"100.00","quantity":1,"tax_class":"","name":"Kadhai Paneer","product_id":149,"sku":"","meta":[]}],"shipping_lines":[],"tax_lines":[],"fee_lines":[],"coupon_lines":[],"customer":{"id":28,"created_at":"2017-06-16T04:50:25Z","email":"gautam@cubewires.co.in","first_name":"Gautam","last_name":"kumar","username":"gautam@cubewires.co.in","role":"subscriber","last_order_id":173,"last_order_date":"2017-07-13T07:46:07Z","orders_count":1,"total_spent":"240.00","avatar_url":"http:\/\/0.gravatar.com\/avatar\/0881970233cc5dc62abae09452a843e1?s=96","billing_address":{"first_name":"Gautam","last_name":"kumar","company":"","address_1":"Ashok Nagar","address_2":"","city":"New Delhi","state":"DL","postcode":"110096","country":"IN","email":"gautam@cubewires.co.in","phone":"8574234554"},"shipping_address":{"first_name":"","last_name":"","company":"","address_1":"","address_2":"","city":"","state":"","postcode":"","country":""}}}}
                        [code] => 200
                        [headers] => Array
                            (
                                [Date] =>  Fri, 14 Jul 2017 06:04:43 GMT
                                [Server] =>  Apache/2.4.23 (Win32) OpenSSL/1.0.2h PHP/7.0.13
                                [X-Powered-By] =>  PHP/7.0.13
                                [Content-Length] =>  2513
                                [Content-Type] =>  application/json; charset=UTF-8
                            )

                    )

            )

        )

)

由于

0 个答案:

没有答案