varsitynewsnetwork / wordpress-rest-api-client

A Wordpress API client for PHP
77 stars 63 forks source link

Is there a way to have response headers passed? #13

Open te-online opened 6 years ago

te-online commented 6 years ago

Thank you for this helpful library, the only one for accessing the WP REST API with PHP – as far as I can see!

Now, my goal is to load every available post from the REST API to display them in a frontend.

As of the current WordPress version, we can only get 100 posts per request from the API.

$client->posts()->get(
   null, 
   array(
      'per_page' => 100,
   )
);

Fortunately, there is a header X-WP-TotalPages, where I can see how many requests I would need to fire to get all available posts. (see https://wordpress.stackexchange.com/questions/217490/get-post-count-in-wp-rest-api-v2-and-get-all-categories/250589#250589).

However, I don't see a way to get this header from the $client instance. Is there a way that I don't see?

Right now I do requests, until there are no posts in the response anymore. This leads to one request that I could save.

fontebasso commented 6 years ago

When the request is triggered the response is formatted to return an array for you from the body of the Wordpress response. There is no way to get header data through the client instance.

te-online commented 6 years ago

Thanks for clarifying this. In my opinion there should be a straight-forward way to implement pagination to make this library useful for more than just some minor use cases.

boboudreau commented 6 years ago

This library is open source. Feel free to submit a PR if you'd like endpoints to store response headers and provide a way to retrieve them. Since we're using an HTTP library that adheres to https://www.php-fig.org/psr/psr-7/ we certainly have access to response headers. We use them here: https://github.com/varsitynewsnetwork/wordpress-rest-api-client/blob/master/src/Endpoint/AbstractWpEndpoint.php#L46-L49

fontebasso commented 6 years ago

@boboudreau what do you think about returning the response object for the developer implementing this library to decide whether to get the header or not, format the body json, etc?

It seems to me that it can make it difficult to implement it to have to fetch the content and format it. Something the library does now. Maybe we can bring the return into an array with the 'headers' and 'content' keys?

te-online commented 6 years ago

what do you think about returning the response object for the developer implementing this library to decide whether to get the header or not, format the body json, etc?

I like the idea, but it would inherently break the current usage/API, which makes me concerned about backwards compatibility.

We could add another method like getWithHeaders which returns something like array('json' => 'json response ...', 'headers' => [...]), but I feel that this is kind of ugly and not the best way. Any ideas, @boboudreau? As you said, I could try to prepare a pull-request.

mysyfy commented 5 years ago

+1 for getResponse / getWithHeaders to use WP header fields:

https://developer.wordpress.org/rest-api/using-the-rest-api/pagination/

  1. X-WP-Total: the total number of records in the collection
  2. X-WP-TotalPages: the total number of pages encompassing all available records