0

I'm new to JSON API and i was just testing it out to understand how this works so this is an javascript example of json

$(document).ready(function() {
    var url = 'https://api.themoviedb.org/3/movie/',
        mode = 'search/',
        input,
        movieName,
        key = '?api_key=e9dfeccf734a61b9a52d9d7660f0d0a1';

    $('button').click(function() {
        var input = $('#movie').val(),
            movieName = encodeURI(input);
        $.ajax({
            url: url + mode + movieName + key, 
            dataType: 'jsonp',
            success: function(data) {
             console.log(data);
            }
        });
    });
});

And HTML

<input id="movie" type="text" /><button>Search</button>

I'm trying to use that API using PHP but failed..

<?php
$json = file_get_contents("url");
$obj = json_decode($json);
$response = array();
$json_response = json_encode($response);
echo $json_response;
header("Content-type: application/json");
?>

Anyone can help me with using search query as a name of movie to fetch out the details..

Example: https://api.themoviedb.org/3/movie/297762?api_key=e9dfeccf734a61b9a52d9d7660f0d0a1&language=en-US

I made this work but i need to style it..

<?php
  //$url = file_get_contents('https://api.themoviedb.org/3/movie/297762?api_key=e9dfeccf734a61b9a52d9d7660f0d0a1&language=en-US');
  //echo $url;
  $json_url = "https://api.themoviedb.org/3/movie/297762?api_key=e9dfeccf734a61b9a52d9d7660f0d0a1&language=en-US";
  $json = file_get_contents($json_url);
  $data = json_decode($json, TRUE);
  echo "<pre>";
  print_r($data);
  echo "</pre>";
?>

1 Answer 1

1

The api is a little different when you search for a movie compared to when you grab information for a movie.

Here is the url for search for a movie http://api.themoviedb.org/3/search/movie?api_key=e9dfeccf734a61b9a52d9d7660f0d0a1&query=Jumanji:%20Welcome%20to%20the%20Jungle

With that in mind, you can change the javascript so it matches the url, no need for php for this api.

$(document).ready(function() {
    var url = 'https://api.themoviedb.org/3/',
        mode = 'search/movie/',
        key = 'e9dfeccf734a61b9a52d9d7660f0d0a1';

    $('button').click(function() {
        var input = $('#movie').val();

        $.ajax({
            url: url + mode, 
            type: "get",
            data: {
               "api_key": key,
               "query": input
            },
            dataType: 'jsonp',
            success: function(data) {
                console.log(data);
            }
        });
    });
});
Sign up to request clarification or add additional context in comments.

3 Comments

@NoorQureshi If you aren't capable of changing the console.log call to something that modifies the DOM then you are in over your head and need to go back to basics.
@LukePark I'm sorry bro but im not a web developer im just completing this code to update my small application.. I can't go to basics right now.. :/
@NoorQureshi I answered exactly what you asked about, so how about you "fix" your question instead. Or even better, post a new one, because it has nothing to do with the API. Link the new question to me, and I will answer it. Show the json structure, and how you want the html structure to be, I can't read your brain or guess it. Do you use a template engine, or do you want a plain jQuery solution? Provide all the necessary information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.