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
- Cannot access non-wordpress subdirectories as wordpress overrides them with a 404 error
- WP Rest API: details of latest post including featured media url in one request?
- Notice: Constant WP_POST_REVISIONS already defined
- How to stop certain warning logging in error.log?
- Error 404 Page Not Found When Updating a Post or Page
- Why on Earth am I getting “undefined_index” errors?
- How to loop through JSON data in wordpress WP REST API
- Debugging ‘Object of class WP_Query could not be converted to int’ error
- error_log over 70GB. How to stop this? [closed]
- Getting headers already sent error from plugin
- Create a new user using WP REST API and declare meta object
- Add image only in first post
- Object of class WP_Error could not be converted to string
- Getting Warnings & Notices from Fresh WordPress 3.1.2 install
- Sodium compat error
- Fatal error: require(): Failed opening required ‘WP_DIRwp-blog-header.php’
- What do these “Failed to import” errors mean and how do I fix it?
- Formating content rendered from wordpress REST API as JSON and not HTML
- Keep user’s privileges on accessing contents in JSON response
- How to use add_settings_error in register_setting callback
- : Failed to load resource
- How can I cache WordPress Rest API Response
- Error trying to publish immediately. Post status = future (Missed schedule error)
- Why aren’t tags and categories added in post request to WP Rest API
- Form Post results in 404 Not Found [duplicate]
- File does not exist – wp-index.php
- Getting 403 Access Denied Error Message in new WordPress Post
- Querying A Post That Includes Two Taxonomies Using JSON Rest API
- Error wordpress “Call to undefined function aq_resize()” [closed]
- Sorry, you are not allowed to access this page dashboard
- err_connection_closed after 20 minutes when loggedin in backend
- How to Fix “Are You Sure You Want to Do This”, while uploading my theme?
- Homepage Not Found Error [closed]
- Fatal error: Allowed memory size of 146800640 bytes exhausted (tried to allocate 57 bytes) in public_html/wp-includes/rewrite.php on line 1561
- Display all WP Errors together during form validation
- REST API: Display Category names in JSON?
- Updating posts exceeds maximum execution time
- Why can I not execute php files under the wp-includes folder?
- Headers already sent by
- Failed: Filesystem preventing downloads. ( ftpext)
- How to connect android app with WordPress website?
- Code snippet to display ID gives critical error
- I tried to build a theme from scratch and it crashed everything
- error log bloated by add_view
- Warning: require_once(/home/onetwo/public_html/wp-config.php): [closed]
- Unable to locate WordPress content directory
- WP Core Update Issue
- How to clone all WordPress Rest API end points
- Cannot upgrade to 4.0 – get SSL certificate error
- Fatal Error: get_header();
- Error when setting cookie
- Downgrade from latest nightly build to latest stable version
- styles/default.css in markup, where does it come from? [closed]
- download count29.php whene loading site [closed]
- My new WordPress blog keeps refreshing in Chrome browser
- Undefined index error on options array element?
- Getting no Error Log File saved to my local directory, and no debug info displayed in the web browser
- Disable and Hide WP-JSON
- WordPress REST API not working on localhost
- Create multiple pages with tables from json file
- How to turn off WP error handling, but turn on php error handling?
- Help with “Uncaught Error: Attempt to modify property “response” on bool”
- Error regarding invalid file permissions for upgrading WordPress version
- How to solve the error “Oops! That page can’t be found” when access Portfolio
- Get html from custom route api
- Publication in wrong category
- What’s the right way to validate JSON data coming from an AJAX POST request?
- WordPress output data to another website and pull data
- Consult array data in REST API Filter
- Why does this show up when i try to log in
- How can I make error messages in admin
- Dashboard widget, screen options/help doesn’t work?
- Getting amp; on every single category, post and page title. How to Remove?
- WordPress fails when my post is larger than 1400 words
- Seeing “error” as text on white when accessing admin
- Unregister Settings Error
- Weird Parse Error on Uploaded File
- 500 internal server error with wordpress
- call user func array error after moved the site to new server
- Why I am having error Call to undefined function get_header()?
- Avoiding “headers already sent” in WordPress
- Fatal error: Cannot declare class WP_User_Request, because the name is already in use?
- problem ajaxurl – designthemes-core-features
- Help with overloading the limits of CPU/RAM issue in a resseler account with wp sites
- WordPress “Allowed memory size” on wp-db.php [closed]
- Whole right area of wordpress admin dashboard not showing
- Only specific users timing out
- WordPress Customizer not loading
- Error Message – Cannot login to WordPress
- Custom WP Rest API Endpoints from JSON Schema
- Unable to log in: “Cannot serve directory www/wordpress/wp-includes/ No matching DirectoryIndex”
- How do I turn off WordPress errors and just have regular old php errors?
- I’m getting this ‘PHP Deprecated’ issue
- Error while running docker compose command. WordPress is already installed
- RSS Feed URL redirecting to site URL
- WordPress Rest Api search limited to 10 results
- Website slows down and Error 522