Last active
September 14, 2017 12:39
-
-
Save jasonglisson/1eeeaae51aed02b8427a to your computer and use it in GitHub Desktop.
This is an example of json api connection with PHP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$apikey = 'insert_your_api_key_here'; | |
$q = urlencode('Toy Story'); // make sure to url encode an query parameters | |
// construct the query with our apikey and the query we want to make | |
$endpoint = 'http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=' . $apikey . '&q=' . $q; | |
// setup curl to make a call to the endpoint | |
$session = curl_init($endpoint); | |
// indicates that we want the response back | |
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); | |
// exec curl and get the data back | |
$data = curl_exec($session); | |
// remember to close the curl session once we are finished retrieveing the data | |
curl_close($session); | |
// decode the json data to make it easier to parse the php | |
$search_results = json_decode($data); | |
if ($search_results === NULL) die('Error parsing json'); | |
// play with the data! | |
$movies = $search_results->movies; | |
echo '<ul>'; | |
foreach ($movies as $movie) { | |
echo '<li><a href="' . $movie->links->alternate . '">' . $movie->title . " (" . $movie->year . ")</a></li>"; | |
} | |
echo '</ul>'; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment