Search code examples
phpwordpressapiwoocommerceget-headers

Woocommerce API PHP Response Header


I cannot seem to find a way to access the X-WP-TotalPages in the header of the response, I am able to display my orders and everything the way I want to but for the life of me cannot figure out how to get to the headers in the response.

I am using this at the moment;

require($_SERVER["DOCUMENT_ROOT"] . "/vendor/autoload.php");

use Automattic\WooCommerce\Client;

$woocommerce = new Client(
'http://example.com', 
'**********************************', 
'**********************************',
[
    'wp_api' => true,
    'version' => 'wc/v1',
]
);
$endpoint = 'orders';
$options = ['filter[limit]' => '200', 'filter[period]' => 'year', 'filter[order]' => 'ASC', 'status' => 'processing'];

$result = $woocommerce->get($endpoint, $options);

I am pretty certain I need something along the lines of this;

print_r(get_headers($result['X-WP-TotalPages']));

I have tried a number of different variations but just cannot seem to figure it out, any help would be massively appreciated!


Solution

  • Came across the same question. Currently you are looking at the result of the response. What you are exactually looking for are the headers of the response. You can get them by

    $lastResponse = $woocommerce->http->getResponse();
    $headers = $lastResponse->getHeaders();
    $totalPages = $headers['X-WP-TotalPages'];