WP-API / docs-v2

Documentation for version 2 of the API
http://v2.wp-api.org
58 stars 76 forks source link

Add documentation on how to run requests internally #121

Open basschoen opened 8 years ago

basschoen commented 8 years ago

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 commented 8 years ago

$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 commented 8 years ago

Sorry, didn't mean to close.

rmccue commented 8 years ago

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 :)

basschoen commented 8 years ago

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

rmccue commented 8 years ago

Added in core recently per this ticket.

basschoen commented 8 years ago

Brand new! Thanks, will work with this.