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
- Building a custom REST API
- Unable to locate WordPress Content directory (wp-content)
- How to fix “There has been a critical error on your website. Please check your site admin email inbox for instructions”?
- Notice: Use of undefined constant SCRIPT_DEBUG
- WordPress Redirecting to wp-admin/install.php
- WordPress Rest API: How do we validate with our custom API key?
- Include custom post meta value in fetched JSON
- authentication issue with rest api – rest_cannot_create
- When does remove_filter not work?
- This webpage is not available ERR_NAME_NOT_RESOLVED
- Error 310 too many redirects after switching domains
- How do I set up Debugging?
- Error Logs to Diagnose Error 500 in LAMP
- Implementing the wp json-rest api
- How Do I Add User Custom Field to REST API Response?
- Does wordpress have an error log?
- WP_DISABLE_FATAL_ERROR_HANDLER vs WP_DEBUG ? What to use and when to use to see errors?
- Call to undefined function get_header() in index.php
- WordPress JSON API returns normal site page in html. How do I get it to give me JSON?
- Recommended way to remove WP REST API returned data for custom post
- Create multiple posts with WP REST API at once
- Unexpected Output: register_activation_hook with namespace
- WP Site Only Shows Javascript for Main Page
- PHP notice coming from the WordPress core?
- Get notified by email when Fatal error occurs on wordpress site?
- Get PHP Fatal error on globalized $wpdb: Call to a member function insert() on a non-object- I have no clue. What to do?
- media_buttons_context is deprecated
- Error Establishing a Database Connection, but credentials are OK
- error: An Unexpected HTTP Error occurred during the API request
- What might cause “junk after document element” error?
- I am getting an error some times when uploading themes
- After upgrading Debian Buster to Bullseye, WordPress sites no longer updating
- How to Debug WordPress
- How to Get Featured Image from REST API?
- Error establishing a database connection only on some pages
- Deprecated issue in WordPress [closed]
- Why getting “The input type is invalid” when accessing edit.php?
- body_class REST field in WP-API
- Problem with running javascript in a shortcode
- merge wp rest api query to get posts per category does not work
- “load-scripts.php” hangs and times out when plupload is requested
- I have an error message coming up after I type in my domain. How Do I get my site back
- Site Address (URL) – causing error 404
- Object of class stdClass could not be converted to string in
- Is there a way I can fetch the WordPress Developer Code References with an API?
- How to trace an Error when it says nothing about any plugin?
- WordPress fonts need to upload with Google fonts?
- WP Rest API and json_decode()
- require_once() error after moving WordPressto another server
- Removed plugin generating error message
- Parse error:syntax error [closed]
- When sorting WP_List_Table, table sorts, but I also get SQL errors
- Why do these errors appear on my wordpress site? [closed]
- WordPress + Buddpyress – troubleshooting server overloading
- Error when activating child theme: `failed to open stream: No such file or directory`
- Error when trying to logout
- 403 error when publishing a post in wordpress. Error => Publishing failed. The response is not a valid JSON response
- Retrieve Data from Custom Endpoint
- Optimal WP Web App Backend for mobile app development
- Error message: Response is not a valid JSON response
- A critical error has occurred on the site
- I’m receiving the following error using the below code: Uncaught Error: Cannot use object of type WP_REST_Response as array
- Fatal error for category archives
- 403. You don’t have permission to access this resource error
- Critical error but refresh fixes it
- How to identify the origin of an issue or an unexpected behavior?
- WordPress forbidden 403 on delete in production vs local environment
- Fatal Error: Uncaught Error: call to undefined function get_plugin_data after uploading 2 blog posts(wp-shopify)
- WordPress error when update to new version
- Autotrader API Integration
- Rest WP_Error always return null
- Crawl Errors in Search console
- How to fix this CORS error?
- Fix Characters WordPress Ionic App
- One page is showing the “Index of /” error
- WP Rest API v2.0 user profile update issue
- Parse error: syntax error, unexpected ”tax_query” in wordpress
- WordPress Dashboard visible but site is not after migration
- Connecting a wordpress site to an AngularJS APP
- 403 error with no reason
- All of my wordpress sites showing †Ù¥
- Possible to hide the search engine indexing disabled prompt/error message?
- WordPress Error Missing Argument [duplicate]
- Error updating WordPress, does not ask for FTP password, just error
- Is it possible to make WordPress as a RESTful app?
- Post show up as post and pages
- Parse error: syntax error, unexpected T_FUNCTION
- The ( error_log ) file size is very large
- index page is not loading [closed]
- Solve theme installation problem?
- Fatal error trace not shown in development setup
- How to fix Notice: unserialize(): Error at offset 12620 of 12651 bytes error?
- syntax error, unexpected ‘=>’ (T_DOUBLE_ARROW) [closed]
- Error code 499 on specific cron job
- Please Help Me, How to Fix PHP Error Undefined Array Key “srcset”
- My websit dosent worke http errors 500
- 403 forbidden in admin console: fix seems to be temporary and then error is back