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:

16 Comments

  1. manishapr3 years ago

    Can I use XMLHttpRequest() to retrieve the JSON data? If so, could you send me a small sample. Thanks.

  2. ODH3 years 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...

  3. Steve N.3 years 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.

  4. ODH3 years 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!

  5. ODH3 years ago

    Do you think it's possible that my PHP settings are the problem?

  6. Steve N.3 years 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

  7. flik3 years ago

    "Gone with the wind" is coming three times. I want only latest "Gone with the wind." How do that?

  8. Lithin Raj2 years ago

    Not able to get synopsis for some movies...(Toy story, toy story 2 etc..)

  9. Lithin Raj2 years ago

    Not able to get synopsis for some movies...(Toy story, toy story 2 etc..)

  10. Michael2 years ago

    Is there any JSONP support?

    I don't see any headers in your response that would permit cross-domain requests (seems like this would be required for your javascript sample to work).

  11. Steve N.2 years ago

    Hi Michael,

    Yes, there is JSONP support. That is how the example works. Please see http://developer.rottentomatoes.com/docs/read/JSON under the heading "JSONP Support" for more information

    -Steve

  12. wampir2 years ago

    Hi,

    is there any possibility to get recommendations for indicated user using API?

    Thanks ;)

  13. ODH2 years ago

    How do you access the genre in the PHP code?

    i.e. to return the title you use $movie->title

    What is the equivalent code to return the FIRST item in the array of genres?

  14. ODH2 years ago

    Nevermind. Genre isn't in movies.json. You can only get it from the specific movie's json

    See http://developer.rottentomatoes.com/docs/read/json/v10/Movie_Info

  15. schwarzenneger11 months ago

    A complete jQuery+UI autocomplete example that uses RT API: http://salman-w.blogspot.com/2013/12/jquery-ui-autocomplete-examples.html#example-5

  16. nobitaaa10 months ago

    this API soo cool, you have fix search problem too

Please sign in to post a comment.