docs-v2
docs-v2 copied to clipboard
Add documentation on how to run requests internally
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!
$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
Sorry, didn't mean to close.
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 :)
Thanks @rmccue, where does this rest_get_server() function comes from?
Added in core recently per this ticket.
Brand new! Thanks, will work with this.