- Previous: Movie Alias
- Up: API Homepage - v1.0.json
- Next:
Example Code
Get started quickly with the examples below:
PHP
In this PHP code example, we are making a call to the search endpoint and searching for the string 'Toy Story'. With the search results, we then echo out an html list of the movies and their year.
<?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>';
?>
Once you load up this code, you should see something similar to the following:
JavaScript (with jQuery)
In this JavaScript w/ jQuery code, we are making a call to the search endpoint for 'Gone with the Wind'. With the search response, we then show the results of the search followed by a movie thumbnail.
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"></script>
<script>
var apikey = "myapikey";
var baseUrl = "http://api.rottentomatoes.com/api/public/v1.0";
// construct the uri with our apikey
var moviesSearchUrl = baseUrl + '/movies.json?apikey=' + apikey;
var query = "Gone with the Wind";
$(document).ready(function() {
// send off the query
$.ajax({
url: moviesSearchUrl + '&q=' + encodeURI(query),
dataType: "jsonp",
success: searchCallback
});
});
// callback for when we get back the results
function searchCallback(data) {
$(document.body).append('Found ' + data.total + ' results for ' + query);
var movies = data.movies;
$.each(movies, function(index, movie) {
$(document.body).append('<h1>' + movie.title + '</h1>');
$(document.body).append('<img src="' + movie.posters.thumbnail + '" />');
});
}
</script>
</head>
<body>
</body>
</html>
You should see something similar to the following screenshot:
- Previous: Movie Alias
- Up: API Homepage - v1.0.json
- Next:
7 Comments
manishapr – 11 months ago
Can I use XMLHttpRequest() to retrieve the JSON data? If so, could you send me a small sample. Thanks.
ODH – 8 months ago
Only the Javascript example code worked for me. The PHP code keeps returning "error parsing json." I'm positive that I inserted the api key correctly. Quite strange...
Steve N. – 8 months ago
Hi ODH, I just tweaked the example a bit: the endpoint url it was hitting was 'movies' and I changed it to include the extension as well, so it is now 'movies.json'. Give that a try and let us know if it doesn't work.
ODH – 8 months ago
I tried a bunch of combinations of endpoint urls. None of them seem to work (including your updated version). Let me know if you have other ideas. Thank you!
ODH – 8 months ago
Do you think it's possible that my PHP settings are the problem?
Steve N. – 8 months ago
Hi ODH, I'm moving the discussion over to http://developer.rottentomatoes.com/forum/read/123681 Let's troubleshoot there, and perhaps other community members can help
flik – 2 months ago
"Gone with the wind" is coming three times. I want only latest "Gone with the wind." How do that?
Please sign in to post a comment.