I dug in the codex and found some interesting functions.
This is how I finally did it :
function api_request($api_url = null, $params=null,$method = 'GET'){
if (!$api_url){
return new WP_Error('no_api_url',"Missing API URL");
}
//Create request
$request = WP_REST_Request::from_url( $api_url );
//Method
$request->set_method( $method );
//params
switch($method){
case 'GET':
$request->set_query_params($params);
break;
case 'POST':
$request->set_body_params($params);
break;
}
//Get response
$response = rest_do_request( $request );
if ( $response->is_error() ) {
return $response->as_error();
}
//Get datas
$datas = $response->get_data();
return $datas;
}
Related Posts:
- Application passwords not working on localhost?
- GET request return value as error instead of success
- Suddenly get errors in admin when editing post type
- Persistent ‘Updating failed, invalid JSON response.’ Error in WordPress Editor
- Cannot access non-wordpress subdirectories as wordpress overrides them with a 404 error
- Showing errors with $wpdb update
- Change login error messages
- WP Rest API: details of latest post including featured media url in one request?
- WP_DEBUG is not set, but I’m still getting warnings
- How to disable the fatal error (WSOD) protection?
- Building a custom REST API
- Unable to locate WordPress Content directory (wp-content)
- Could not create directory /wp-content/upgrade/
- WP REST API only returning partial list of users
- Getting the warning: Missing argument 2 for manage_posts_custom_column()?
- WP JSON REST API (Ryan McCue) how to query posts with specific meta data with ‘AND’ relation?
- Fetch All Posts (Including Those Using a Custom Post Type) With WordPress API
- What log files does a WordPress installation write to?
- How to display admin error notice if settings saved succesfully?
- HTTPS leads to Sorry, you are not allowed to access this page
- How to fix “There has been a critical error on your website. Please check your site admin email inbox for instructions”?
- Notice: Undefined index: host in /var/www/html/wp-includes/canonical.php on line 445
- What is the best way to monitor PHP functions/executions?
- Notice: Use of undefined constant SCRIPT_DEBUG
- Get Image URL instead of Attachment Id in Rest API
- Notice: Constant WP_POST_REVISIONS already defined
- Uncaught ReferenceError:switchEditors is not defined
- How to stop certain warning logging in error.log?
- WordPress Redirecting to wp-admin/install.php
- How do I create a user using the new JSON api in 4.7?
- Error 404 Page Not Found When Updating a Post or Page
- Debug mode shows Strict Standards
- How to use WP_Error $data argument?
- Is it possible to disable caching of an option when using w3 total cache?
- Why on Earth am I getting “undefined_index” errors?
- WP Rest API v2 filter and display latest post with specific tag
- WordPress Rest API: How do we validate with our custom API key?
- Showing error “Function create_function() is deprecated”
- Include custom post meta value in fetched JSON
- How to loop through JSON data in wordpress WP REST API
- strange characters in wordpress website displayed for visitors [closed]
- WP Rest API v2 return posts with specific tag
- authentication issue with rest api – rest_cannot_create
- Debugging ‘Object of class WP_Query could not be converted to int’ error
- WordPress REST API – JSON “Rendered” Content Incorrect
- How to fix ob_end_flush() error?
- When does remove_filter not work?
- wordpress wp-json prefix issue
- How to: Rest endpoint returning empty object
- wp_remote_get() not retrieving pages properly
- error_log over 70GB. How to stop this? [closed]
- $wpdb->insert Database Error Duplicate Entry Error Logging
- Update wordpress fail – There has been a critical error on your website
- This webpage is not available ERR_NAME_NOT_RESOLVED
- Displaying oEmbed errors?
- Getting headers already sent error from plugin
- “Apache HTTP Service has stopped working” with WP Query in WordPress 2.9.2
- Error 310 too many redirects after switching domains
- How do I set up Debugging?
- Create a new user using WP REST API and declare meta object
- WP REST API – Retrieve content from page
- Error Logs to Diagnose Error 500 in LAMP
- Add image only in first post
- Error messages when adding code to function.php or trying to delete inactive plugin files
- Full-Ajax Theme: parseJSON error while building a JSON object from a WordPress custom template
- “Notice: Undefined index:” error when adding new content?
- 418 header status, I’m a teapot [closed]
- Showing “ Notice: Undefined variable:” and “ Notice: Trying to get property of non-object”
- Publishing Time – WordPress Plain White Screen with No Error
- Object of class WP_Error could not be converted to string
- Android authentication
- WP REST API route request explain
- Implementing the wp json-rest api
- New WordPress Error Message – Not seen this before
- How Do I Add User Custom Field to REST API Response?
- Add Category Name to REST API
- Does wordpress have an error log?
- Disable REST API for a user ROLE
- Display sub-taxonomies based on SELECTED parent-taxonomy
- WP_DISABLE_FATAL_ERROR_HANDLER vs WP_DEBUG ? What to use and when to use to see errors?
- Handling duplicates with wp_insert_term()
- Recommend a guide to catching plugin errors, please?
- How to handle core code throwing notice on valid input?
- Getting Warnings & Notices from Fresh WordPress 3.1.2 install
- Call to undefined function get_header() in index.php
- How can I create a custom page for this error?
- ping_status in JSON REST API
- WordPress JSON API returns normal site page in html. How do I get it to give me JSON?
- WordPress REST API “rest_authentication_errors” doesn’t work external queries?
- Get all post media images via WP REST API
- List Category Posts plugin upgrade fails with fatal error
- Can’t GET draft posts via REST API from headless frontend
- Sodium compat error
- Fatal error: require(): Failed opening required ‘WP_DIRwp-blog-header.php’
- Increase the page size of the WordPress REST API
- Recommended way to remove WP REST API returned data for custom post
- What do these “Failed to import” errors mean and how do I fix it?
- How to get a Custom 500 Error Page to show?
- Dashboard Whitescreen of Death?
- add_sub_menu page() to be replaced by add_theme_page()