如何将JSON API响应数组值设置为php中的变量?

时间:2018-04-17 23:00:23

标签: php json

我遇到了很多困难。我试图从WooCommerce JSON API响应数组中提取值并将它们分配给PHP中的变量。

以下是print_r()的完整响应代码:

Automattic\WooCommerce\HttpClient\Response Object
(
    [code:Automattic\WooCommerce\HttpClient\Response:private] => 201
    [headers:Automattic\WooCommerce\HttpClient\Response:private] => Array
        (
            [Date] => Tue, 17 Apr 2018 20:09:19 GMT
            [Server] => Apache/2.4.18 (Ubuntu)
            [Set-Cookie] => yith_wcwl_products=%5B%5D; expires=Tue, 17-Apr-2018 19:09:19 GMT; Max-Age=0; path=/, PHPSESSID=9a2ohat0vp3escuotlufdp55j5; path=/
            [Expires] => Wed, 11 Jan 1984 05:00:00 GMT
            [Cache-Control] => no-cache, must-revalidate, max-age=0
            [Pragma] => no-cache
            [X-Robots-Tag] => noindex
            [Link] => <mydomain.com/wp-json/>; rel="api.w.org/"
            [X-Content-Type-Options] => nosniff
            [Access-Control-Expose-Headers] => X-WP-Total, X-WP-TotalPages
            [Access-Control-Allow-Headers] => Authorization, Content-Type
            [Location] => mydomain.com/wp-json/wc/v2/products/1157
            [Allow] => GET, POST
            [Content-Length] => 2244
            [Content-Type] => application/json; charset=UTF-8
        )

    [body:Automattic\WooCommerce\HttpClient\Response:private] => {"id":1157,"name":"A NICE product","slug":"a-nice-product-16","permalink":"mydomain.com\/shop\/uncategorized\/a-nice-product-16\/","date_created":"2018-04-17T20:09:19","date_created_gmt":"2018-04-17T20:09:19","date_modified":"2018-04-17T20:09:19","date_modified_gmt":"2018-04-17T20:09:19","type":"simple","status":"publish","featured":false,"catalog_visibility":"visible","description":"A very meaningful product description","short_description":"","sku":"","price":"15.00","regular_price":"15.00","sale_price":"","date_on_sale_from":null,"date_on_sale_from_gmt":null,"date_on_sale_to":null,"date_on_sale_to_gmt":null,"price_html":"<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&#36;<\/span>15.00<\/span>","on_sale":false,"purchasable":true,"total_sales":0,"virtual":false,"downloadable":false,"downloads":[],"download_limit":-1,"download_expiry":-1,"external_url":"","button_text":"","tax_status":"taxable","tax_class":"","manage_stock":false,"stock_quantity":null,"in_stock":true,"backorders":"no","backorders_allowed":false,"backordered":false,"sold_individually":false,"weight":"","dimensions":{"length":"","width":"","height":""},"shipping_required":true,"shipping_taxable":true,"shipping_class":"","shipping_class_id":0,"reviews_allowed":true,"average_rating":"0","rating_count":0,"related_ids":[1127,884,1139,1038,1075],"upsell_ids":[],"cross_sell_ids":[],"parent_id":0,"purchase_note":"","categories":[{"id":181,"name":"Uncategorized","slug":"uncategorized"}],"tags":[],"images":[{"id":1156,"date_created":"2018-04-17T20:09:19","date_created_gmt":"2018-04-17T20:09:19","date_modified":"2018-04-17T20:09:19","date_modified_gmt":"2018-04-17T20:09:19","src":"mydomain.com\/wp-content\/uploads\/2018\/04\/TS-P002-M-GR-D001-LT002-BL-30.jpg","name":"TS-P002-M-GR-D001-LT002-BL-30.jpg","alt":"","position":0}],"attributes":[],"default_attributes":[],"variations":[],"grouped_products":[],"menu_order":0,"meta_data":[{"id":15498,"key":"_wpas_done_all","value":"1"}],"_links":{"self":[{"href":"mydomain.com\/wp-json\/wc\/v2\/products\/1157"}],"collection":[{"href":"mydomain.com\/wp-json\/wc\/v2\/products"}]}}

以下是我设置变量的代码:

foreach($lastResponse['body:Automattic\WooCommerce\HttpClient\Response:private'] as $result) {
$id = $result['id'];
$date_created = $result['date_created'];
}

以下是我收到的错误回复。显然,我收到了一个对象而不是一个数组。如何将对象转换为数组并提取我的变量?很感谢任何形式的帮助。

PHP Fatal error:  Uncaught Error: Cannot use object of type Automattic\WooCommerce\HttpClient\Response as array in /var/www/html/htaAPI/pushit.php:51
Stack trace:
#0 {main}
  thrown in /var/www/html/htaAPI/pushit.php on line 51

1 个答案:

答案 0 :(得分:2)

您正试图访问私有财产。

您希望执行以下操作(前提是$ lastResponse是\ Automattic \ WooCommerce \ HttpClient \ Response对象),否则替换为:

$json = json_decode($lastResponse->getBody());
$id = $json->id;
$date_created = $json->date_created;