Reputation: 165
We have Rest API implemented on .Net Core. This REST API has several endpoints that return JSON data
And now we need to extend one of our Wordpress sites to consume this API.
Basically what we need is to display JSON data (that comes from the REST API) on Wordpress pages.
I think the ideal scenario for us would be to implement templates pages on Wordpress and then just populate placeholders with data that comes from that API
The questions are:
What is the best way to call 3rd party API from Wordpress?
Is there a way to create template pages and then fill them out with data?
Are you aware of any plugins that would help?
Additional details:
All API endpoint are GET
We should be able to add parameters to the API calls.
Upvotes: 0
Views: 2036
Reputation: 324
In that case the wp_remote_get function is your friend. Create a page template in WordPress (ressource: https://developer.wordpress.org/themes/template-files-section/page-template-files/), then retrieve the data from your REST API and insert in the page template:
<?php /* Template Name: Example Template */ ?>
<?php
$url = 'http://example.org/api';
$response = wp_remote_get( esc_url_raw( $url ) );
$api_response = json_decode( wp_remote_retrieve_body( $response ), true );
print_r($api_response);
?>
$api_response will hold an array of data from your JSON.
Upvotes: 1