admin管理员组文章数量:1130349
I'm running WordPress 4.9.8 with a Genesis theme (probably irrelevant, but...). I'd like to write some JSON pulled from the REST API directly to a script tag when a page is built (rather than hitting the API from an AJAX call) for snappier performance.
Fortunately, WP offers WP_REST_Request for situations like this. So I wrote some code that looks like this:
functions.php
add_action('wp_head', 'write_json_to_script_tag');
function write_json_to_script_tag(){
$request = new WP_REST_Request( 'GET', '/wp-json/wp/v2/pages/499/');
$response = rest_do_request( $request );
var_dump($response);
$server = rest_get_server();
$data = $server->response_to_data( $response, false );
$json = wp_json_encode( $data );
echo '<script type="text/javascript"> var the_json = ' . $json . '</script>';
}
When I hit in a browser it produces exactly the json I expect. When I curl that url I get the same result. But my server code returns {code: "rest_no_route", message: "No route was found matching the URL and request method",....}
I'm running WordPress 4.9.8 with a Genesis theme (probably irrelevant, but...). I'd like to write some JSON pulled from the REST API directly to a script tag when a page is built (rather than hitting the API from an AJAX call) for snappier performance.
Fortunately, WP offers WP_REST_Request for situations like this. So I wrote some code that looks like this:
functions.php
add_action('wp_head', 'write_json_to_script_tag');
function write_json_to_script_tag(){
$request = new WP_REST_Request( 'GET', '/wp-json/wp/v2/pages/499/');
$response = rest_do_request( $request );
var_dump($response);
$server = rest_get_server();
$data = $server->response_to_data( $response, false );
$json = wp_json_encode( $data );
echo '<script type="text/javascript"> var the_json = ' . $json . '</script>';
}
When I hit https://example/wp-json/wp/v2/pages/499 in a browser it produces exactly the json I expect. When I curl that url I get the same result. But my server code returns {code: "rest_no_route", message: "No route was found matching the URL and request method",....}
Share Improve this question asked Dec 6, 2018 at 16:22 vlasitsvlasits 1357 bronze badges 01 Answer
Reset to default 3The route should not include the /wp-json part, and there should be no trailing slash (/) at the end:
Wrong:
/wp-json/wp/v2/pages/499/Correct:
/wp/v2/pages/499
So:
$request = new WP_REST_Request( 'GET', '/wp/v2/pages/499' );
I'm running WordPress 4.9.8 with a Genesis theme (probably irrelevant, but...). I'd like to write some JSON pulled from the REST API directly to a script tag when a page is built (rather than hitting the API from an AJAX call) for snappier performance.
Fortunately, WP offers WP_REST_Request for situations like this. So I wrote some code that looks like this:
functions.php
add_action('wp_head', 'write_json_to_script_tag');
function write_json_to_script_tag(){
$request = new WP_REST_Request( 'GET', '/wp-json/wp/v2/pages/499/');
$response = rest_do_request( $request );
var_dump($response);
$server = rest_get_server();
$data = $server->response_to_data( $response, false );
$json = wp_json_encode( $data );
echo '<script type="text/javascript"> var the_json = ' . $json . '</script>';
}
When I hit in a browser it produces exactly the json I expect. When I curl that url I get the same result. But my server code returns {code: "rest_no_route", message: "No route was found matching the URL and request method",....}
I'm running WordPress 4.9.8 with a Genesis theme (probably irrelevant, but...). I'd like to write some JSON pulled from the REST API directly to a script tag when a page is built (rather than hitting the API from an AJAX call) for snappier performance.
Fortunately, WP offers WP_REST_Request for situations like this. So I wrote some code that looks like this:
functions.php
add_action('wp_head', 'write_json_to_script_tag');
function write_json_to_script_tag(){
$request = new WP_REST_Request( 'GET', '/wp-json/wp/v2/pages/499/');
$response = rest_do_request( $request );
var_dump($response);
$server = rest_get_server();
$data = $server->response_to_data( $response, false );
$json = wp_json_encode( $data );
echo '<script type="text/javascript"> var the_json = ' . $json . '</script>';
}
When I hit https://example/wp-json/wp/v2/pages/499 in a browser it produces exactly the json I expect. When I curl that url I get the same result. But my server code returns {code: "rest_no_route", message: "No route was found matching the URL and request method",....}
Share Improve this question asked Dec 6, 2018 at 16:22 vlasitsvlasits 1357 bronze badges 01 Answer
Reset to default 3The route should not include the /wp-json part, and there should be no trailing slash (/) at the end:
Wrong:
/wp-json/wp/v2/pages/499/Correct:
/wp/v2/pages/499
So:
$request = new WP_REST_Request( 'GET', '/wp/v2/pages/499' );
本文标签: Request to REST endpoint works fine in browser and curlbut fails from WPRESTRequest
版权声明:本文标题:Request to REST endpoint works fine in browser and curl, but fails from WP_REST_Request 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1749122997a2319311.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论