Site
Home / Forum / General Discussion / How I use this API my Wordpress Website

How I use this API my Wordpress Website


Posted: 03 Mar 2022 04:58
shimulvisa
Posts: 1
Joined: 2022-02-21

Hi! I am a front-end developer! How I use this API on my website! I have to make like dhakasports.com.bd
Any one help me!


Posted: 03 Mar 2022 09:23

zag
Posts: 3,331
Joined: 2020-03-23

Hi! I am a front-end developer! How I use this API on my website! I have to make like dhakasports.com.bd
Any one help me!


First thing you will need is a JSON reader plugin for wordpress, a few people have done this before but not really sure on the details.

Posted: 04 Mar 2022 15:44
drestauro
Posts: 16
Joined: 2022-01-19

It may seem a bit daunting at first, but it's pretty easy once you figure out how to deal with Json in PHP. I have a decent caching strategy so I use shortcodes to call functions on the page that need the info. I also store every call I make in a wp-cache transient with a unique name for 1 hour to avoid a call every time a page is loaded. The general idea goes like this


function next_events($atts){ // this is a function that accepts an array of parameters from your shortcode
$atts = shortcode_atts(
array(
'league' =>'',
), $atts, 'get_next_events' ); // Callback to get your parameters
$leagueID =$atts['league']; // grabs passed league ID from shortcode
$request = get_transient( 'next_events_'. $leagueID ); // this pulls data from the transient if it exists
if( empty( $request ) ) { // no transient call API
$request = wp_remote_get( 'https://www.thesportsdb.com/api/v1/json/YOUR ID/eventsnextleague.php?id=' . $eventID ); //call api. The JSOn response is stored in $request
if( is_wp_error( $request ) ) {
return ''; // Bail early on error
}
else {
set_transient( 'next_events_'. $leagueID, $request, 3600 ); //store the data in a transient for 1 hour with a unique name
}
}
$body = wp_remote_retrieve_body( $request ); //Grabs the reponse body from the API repsose
$data = json_decode( $body ); //This turns the JSON into an Array, you may want to look up this fuction it may behave differently depending on your environment
foreach( $data->events as $event ) {
$output = $output . 'Event ' . $event->idEvent . 'is ' . strHomeTeam . ' vs ' . strAwayTeam . // example of accessing the data for each event returned from the API call and creating the HTML for your page.
}
return $output // returns the HTML created from the response to the page
}
add_shortcode('get_next_events', 'next_events'); //allows you to call function from a shortcode

* on your page call the functions like this

[get_next_events event="4480"] // Passes 4480 to the function (thats champions league)


I typed this all out on the fly, so there is likely to be some syntax issues. But this is one way to access the API and return the data. If you aren't caching pages as static for the day, I wouldn't recommend this as it will bog down wordpress.


Who is Online?

In total there are 68 users online :: 3 registered, 0 hidden and 65 guests (based on users active over the past 5 minutes) Most users ever online was 424 on Fri Nov 10, 2017 9:02 pm

About Us

Discussion forum for TheSportsDB.com site and related topics

Rules

- Be Polite
- Respect other users
- Always post log files with issues
- Try to be helpful
- No Piracy discussion

Showing 0 to 3 (Total: 3)