WooCommerce API回显响应

时间:2021-07-30 作者:tiago calado

抛出woocommerce api我将一个产品发布到wordpress网站,但我想检索响应,现在我只想打印响应。

使用php如果我使用print\\r($woocommerce->;http),我可以

Automattic\\WooCommerce\\HttpClient\\HttpClient Object
(
    [ch:protected] => Resource id #7
    [url:protected] => https://mywebsite.com/wp-json/wc/v3/
    [consumerKey:protected] => ck_123456789789789789789789
    [consumerSecret:protected] => cs_123456789789789789789789
    [options:protected] => Automattic\\WooCommerce\\HttpClient\\Options Object
        (
            [options:Automattic\\WooCommerce\\HttpClient\\Options:private] => Array
                (
                    [wp_api] => 1
                    [version] => wc/v3
                    [verify_ssl] => 
                )

        )

    [request:Automattic\\WooCommerce\\HttpClient\\HttpClient:private] => Automattic\\WooCommerce\\HttpClient\\Request Object
        (
            [url:Automattic\\WooCommerce\\HttpClient\\Request:private] => https://mywebsite/wp-json/wc/v3/products
            [method:Automattic\\WooCommerce\\HttpClient\\Request:private] => POST
            [parameters:Automattic\\WooCommerce\\HttpClient\\Request:private] => Array
                (
                )

            [headers:Automattic\\WooCommerce\\HttpClient\\Request:private] => Array
                (
                    [Accept] => application/json
                    [User-Agent] => WooCommerce API Client-PHP/3.0.0
                    [Content-Type] => application/json;charset=utf-8
                )

            [body:Automattic\\WooCommerce\\HttpClient\\Request:private] => {"name":"P\\u00e1la","type":"simple","regular_price":"100","short_description":"","stock_quantity":"1","manage_stock":true,"categories":[{"id":37}],"images":[{"src":"https:\\/\\/mywebsite\\/wp-content\\/uploads\\/img2.jpg"}]}
        )

    [response:Automattic\\WooCommerce\\HttpClient\\HttpClient:private] => Automattic\\WooCommerce\\HttpClient\\Response Object
        (
            [code:Automattic\\WooCommerce\\HttpClient\\Response:private] => 201
            [headers:Automattic\\WooCommerce\\HttpClient\\Response:private] => Array
                (
                    [server] => nginx
                    [date] => Fri, 30 Jul 2021 08:04:11 GMT
                    [content-type] => application/json; charset=UTF-8
                    [location] => https://mywebsite/wp-json/wc/v3/products/752
                    [x-powered-by] => PHP/7.4.21
                    [x-robots-tag] => noindex
                    [link] => ; rel="https://api.w.org/"
                    [x-content-type-options] => nosniff
                    [access-control-expose-headers] => X-WP-Total, X-WP-TotalPages, Link
                    [access-control-allow-headers] => Authorization, X-WP-Nonce, Content-Disposition, Content-MD5, Content-Type
                    [expires] => Wed, 11 Jan 1984 05:00:00 GMT
                    [cache-control] => no-cache, must-revalidate, max-age=0
                    [allow] => GET, POST
                    [x-scale] => YXBvY2FzQGdpdGh1Yg==
                )

            [body:Automattic\\WooCommerce\\HttpClient\\Response:private] => {"id":752,"name":"P\\u00e1la","slug":"pala-11","permalink":"https:\\/\\/mywebsite\\/produto\\/pala-11\\/","date_created":"2021-07-30T08:04:11","date_created_gmt":"2021-07-30T08:04:11","date_modified":"2021-07-30T08:04:11","date_modified_gmt":"2021-07-30T08:04:11","type":"simple","status":"publish","featured":false,"catalog_visibility":"visible","description":"","short_description":"","sku":"","price":"100","regular_price":"100","sale_price":"","date_on_sale_from":null,"date_on_sale_from_gmt":null,"date_on_sale_to":null,"date_on_sale_to_gmt":null,"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":true,"stock_quantity":1,"backorders":"no","backorders_allowed":false,"backordered":false,"low_stock_amount":null,"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,"upsell_ids":[],"cross_sell_ids":[],"parent_id":0,"purchase_note":"","categories":[{"id":37,"name":"destaques","slug":"destaques"}],"tags":[],"images":[{"id":751,"date_created":"2021-07-30T08:04:11","date_created_gmt":"2021-07-30T08:04:11","date_modified":"2021-07-30T08:04:11","date_modified_gmt":"2021-07-30T08:04:11","src":"https:\\/\\/mywebsite\\/wp-content\\/uploads\\/img2.jpg","name":"img2.jpg","alt":""}],"attributes":[],"default_attributes":[],"variations":[],"grouped_products":[],"menu_order":0,"price_html":"100.00€<\\/span><\\/bdi><\\/span>","related_ids":[386,404,673,743,684],"meta_data":[],"stock_status":"instock","_links":{"self":[{"href":"https:\\/\\/mywebsite\\/wp-json\\/wc\\/v3\\/products\\/752"}],"collection":[{"href":"https:\\/\\/imobus.pt\\/wp-json\\/wc\\/v3\\/products"}]}}
        )

    [responseHeaders:Automattic\\WooCommerce\\HttpClient\\HttpClient:private] => HTTP/2 201 
server: nginx
date: Fri, 30 Jul 2021 08:04:11 GMT
content-type: application/json; charset=UTF-8
location: https://mywebsite/wp-json/wc/v3/products/752
x-powered-by: PHP/7.4.21
x-robots-tag: noindex
link: ; rel="https://api.w.org/"
x-content-type-options: nosniff
access-control-expose-headers: X-WP-Total, X-WP-TotalPages, Link
access-control-allow-headers: Authorization, X-WP-Nonce, Content-Disposition, Content-MD5, Content-Type
expires: Wed, 11 Jan 1984 05:00:00 GMT
cache-control: no-cache, must-revalidate, max-age=0
allow: GET, POST
x-scale: YXBvY2FzQGdpdGh1Yg==


)
我想使用[body:automatic\\WooCommerce\\HttpClient\\Response:private]数组,那么如何打印它呢?

1 个回复
最合适的回答,由SO网友:tiago calado 整理而成

Found it! thanks!

echo \'<pre>\';
  print_r($woocommerce->http->getResponse()->getbody());
echo \'</pre>\';