Wordpress

Integrate Third Party API in WordPress

2 July 2022

Today We intregate the third party Cricket Series API from Rapid API Hub You can also get your own api from here.

We going to create a shortcode and add api data in our shortcode and then we can use the shortcode anywhere in our site. So Lets start.

Step1: Go to the functions.php file in your child theme.

Step2. Add this code in your functions.php file

add_shortcode(‘api_data_fetch’,’api_data_code’);
function api_data_code()
{
$curl = curl_init();

curl_setopt_array($curl, [
CURLOPT_URL => “https://cricket-live-data.p.rapidapi.com/series”,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_ENCODING => “”,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => “GET”,
CURLOPT_HTTPHEADER => [
“x-rapidapi-host: cricket-live-data.p.rapidapi.com”,
“x-rapidapi-key: ae3e6eaba7msh3cc575695005a77p1ac09ajsn0fad278f9675”
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$data = json_decode($response);
$entries = $data->results[0]->series;
//print_r($entries);
foreach ($entries as $value) {
echo’

‘;
echo “Series Name: $value->series_name
“;
echo “Status: $value->status
“;
echo “Date: $value->updated_at
“;
echo ”
“;
echo’

‘;
}
}

Step3: Use this shortcode api_data_fetch

 

Leave a Reply

Your email address will not be published. Required fields are marked *

ten + 16 =

Author

Hello, I'm
Rajat Meshram

I am a WordPress Developer having 5+ years of Experience. My professional experience includes designing and implementing web pages, user interfaces and plugins for WordPress, helping clients to troubleshoot and fix their WordPress products, designing themes that are as functional as they are beautiful and working with a team of colleagues to create the best products possible.

Latest Post

Latest Tags