Skip to content
This repository has been archived by the owner on Aug 27, 2019. It is now read-only.

Add documentation on how to run requests internally #121

Open
basschoen opened this issue Feb 25, 2016 · 6 comments
Open

Add documentation on how to run requests internally #121

basschoen opened this issue Feb 25, 2016 · 6 comments
Assignees

Comments

@basschoen
Copy link

basschoen commented Feb 25, 2016

For server side JavaScript rendering I want to call some of the API functions in PHP code. What is the best way to do this? For example, how can I fetch all the categories

xx/wp-json/wp/v2/categories

without a cURL request, but with normal PHP code within my WordPress plugin. Thanks!

@Shelob9
Copy link
Contributor

Shelob9 commented Feb 25, 2016

$r = wp_remote_get( 'http://whatever.com/wp/v2/categories' ); You should find Tom McFarlin's series on the WordPress HTTP API.

https://codex.wordpress.org/Function_Reference/wp_remote_get

@Shelob9 Shelob9 closed this as completed Feb 25, 2016
@Shelob9 Shelob9 reopened this Feb 25, 2016
@Shelob9 Shelob9 closed this as completed Feb 25, 2016
@Shelob9
Copy link
Contributor

Shelob9 commented Feb 25, 2016

Sorry, didn't mean to close.

@rmccue
Copy link
Member

rmccue commented Feb 26, 2016

You can internally run requests quite easily:

$request = new WP_REST_Request( 'GET', '/wp/v2/categories' );
$server = rest_get_server();
$response = $server->dispatch( $request );

This is way cheaper than a cURL request, but note that this won't do embedding or anything for you.

We should document how to run these internal requests :)

@rmccue rmccue reopened this Feb 26, 2016
@rmccue rmccue changed the title Server side React rendering with WP-API Add documentation on how to run requests internally Feb 26, 2016
@basschoen
Copy link
Author

Thanks @rmccue, where does this rest_get_server() function comes from?

@rmccue
Copy link
Member

rmccue commented Feb 26, 2016

Added in core recently per this ticket.

@basschoen
Copy link
Author

Brand new! Thanks, will work with this.

@kadamwhite kadamwhite added this to the Consuming the API milestone Aug 25, 2016
@kadamwhite kadamwhite self-assigned this Sep 6, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants