To use the Yelp API, you need to sign up for a Yelp account and create an API key. Here are the steps:
Here is an example PHP script that connects to the Yelp API and fetches data for a given business category in Australia.
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 '' . $business['name'] . '
';
echo 'Rating: ' . $business['rating'] . '
';
}
?>
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.
To display the results of the Yelp API on your webpage, you can simply add the PHP code to your webpage. For example:
Yelp API Example
Restaurants in Australia
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.
Copyright © Web Solutions Express
Icons created by Freepik – Flaticon