fbpx

Freelance Programmer

How to Use the Yelp API to Fetch and Display Business Data on Your Website Using PHP

How to Use the Yelp API to Fetch and Display Business Data on Your Website Using PHP

Step 1: Sign up for a Yelp API key

To use the Yelp API, you need to sign up for a Yelp account and create an API key. Here are the steps:

  1. Go to https://www.yelp.com/developers and sign up for a Yelp account.
  2. Once you have signed up and logged in, click on the “Create New App” button.
  3. Fill out the required fields for your new app, and select the “API” option.
  4. Agree to the terms of service and click “Create New App”.
  5. You will now have access to your Yelp API key, which you will need to use in your PHP script.

Step 2: Write the PHP code to connect to the Yelp API

Here is an example PHP script that connects to the Yelp API and fetches data for a given business category in Australia.

				
					<?php
// Replace your Yelp API key here
$api_key = 'YOUR_API_KEY';

// Set the location to Australia
$location = 'Australia';

// Set the business category to search for
$category = 'restaurants';

// Set the Yelp API endpoint and parameters
$url = 'https://api.yelp.com/v3/businesses/search?location=' . urlencode($location) . '&categories=' . urlencode($category);

// Set the cURL options to make the API request
$options = array(
  CURLOPT_HTTPHEADER => array(
    'Authorization: Bearer ' . $api_key,
    'Content-Type: application/json'
  ),
  CURLOPT_RETURNTRANSFER => true
);

// Use cURL to make the API request
$ch = curl_init($url);
curl_setopt_array($ch, $options);
$response = curl_exec($ch);
curl_close($ch);

// Decode the API response JSON data into an array
$data = json_decode($response, true);

// Display the business names and ratings on the webpage
foreach ($data['businesses'] as $business) {
  echo '<h2>' . $business['name'] . '</h2>';
  echo '<p>Rating: ' . $business['rating'] . '</p>';
}
?>

				
			

In this example, we are searching for restaurants in Australia using the Yelp API. The script uses cURL to make a GET request to the Yelp API endpoint with the necessary parameters, and then decodes the JSON response into an array. Finally, the script displays the business names and ratings on the webpage.

Step 3: Add the PHP code to your webpage

To display the results of the Yelp API on your webpage, you can simply add the PHP code to your webpage. For example:

				
					<!DOCTYPE html>
<html>
<head>
  <title>Yelp API Example</title>
</head>
<body>
  <h1>Restaurants in Australia</h1>
  <?php include('yelp_api.php'); ?>
<script>var rocket_lcp_data = {"ajax_url":"https:\/\/websolutions.express\/wp-admin\/admin-ajax.php","nonce":"e0edfa625f","url":"https:\/\/websolutions.express\/how-to-use-the-yelp-api-to-fetch-and-display-business-data-on-your-website-using-php","is_mobile":false,"elements":"img, video, picture, p, main, div, li, svg","width_threshold":1600,"height_threshold":700,"debug":null}</script><script data-name="wpr-lcp-beacon" src='//websolutions.express/wp-content/plugins/wp-rocket/assets/js/lcp-beacon.min.js' async></script><script>class RocketElementorAnimation{constructor(){this.deviceMode=document.createElement("span"),this.deviceMode.id="elementor-device-mode-wpr",this.deviceMode.setAttribute("class","elementor-screen-only"),document.body.appendChild(this.deviceMode)}_detectAnimations(){let t=getComputedStyle(this.deviceMode,":after").content.replace(/"/g,"");this.animationSettingKeys=this._listAnimationSettingsKeys(t),document.querySelectorAll(".elementor-invisible[data-settings]").forEach(t=>{const e=t.getBoundingClientRect();if(e.bottom>=0&&e.top<=window.innerHeight)try{this._animateElement(t)}catch(t){}})}_animateElement(t){const e=JSON.parse(t.dataset.settings),i=e._animation_delay||e.animation_delay||0,n=e[this.animationSettingKeys.find(t=>e[t])];if("none"===n)return void t.classList.remove("elementor-invisible");t.classList.remove(n),this.currentAnimation&&t.classList.remove(this.currentAnimation),this.currentAnimation=n;let s=setTimeout(()=>{t.classList.remove("elementor-invisible"),t.classList.add("animated",n),this._removeAnimationSettings(t,e)},i);window.addEventListener("rocket-startLoading",function(){clearTimeout(s)})}_listAnimationSettingsKeys(t="mobile"){const e=[""];switch(t){case"mobile":e.unshift("_mobile");case"tablet":e.unshift("_tablet");case"desktop":e.unshift("_desktop")}const i=[];return["animation","_animation"].forEach(t=>{e.forEach(e=>{i.push(t+e)})}),i}_removeAnimationSettings(t,e){this._listAnimationSettingsKeys().forEach(t=>delete e[t]),t.dataset.settings=JSON.stringify(e)}static run(){const t=new RocketElementorAnimation;requestAnimationFrame(t._detectAnimations.bind(t))}}document.addEventListener("DOMContentLoaded",RocketElementorAnimation.run);</script></body>
</html>

				
			

This will display the results of the Yelp API on your webpage.

I hope this example helps you understand how to connect to a third-party API in PHP and display the data on a webpage. Don’t forget to replace “YOUR_API_KEY” with your actual Yelp API key in the PHP code.

Let me help you with your PHP code projects.