设置本地代理以访问imdb

时间:2014-03-31 00:18:42

标签: javascript jquery html json imdb

我想在一个名称下搜索所有内容,例如像这样搜索的星球大战http://www.imdb.com/xml/find?json=1&nr=1&tt=on&q=star%20wars

我想以表格格式列出所有结果

这里是我到目前为止我不得不改变使用easy omdb api的代码,因为这样只能容纳十个结果

现在我不断获取javascript错误任何帮助PLZ我知道我需要设置localproxy需要帮助PLZ

愿爱的例子

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Sample</title>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script>
    $( document ).ready(function() {
        $("#SampleSearchButton").click(function() {
            getImdbInfo($("#title").val());
        })
    });

    // The function below takes the entered title and searchs imdb for a match then it displays as followed

    function getImdbInfo(Title) {
        $.ajax({
          url: "http://www.imdb.com/xml/find?json=1&nr=1&tt=on&q=" + Title,
          cache: false,
          dataType: "json",
          success: function(data) {
                // you get an object for iteration, the keys are Title, Type, Year, imdbID
                console.log(data);

                var str = '<table>';
                str += "<thead><th>Index</th><th>Title</th><th>Type</th><th>Year</th><th>imdbID</th></thead>"

                // iterate over the data result set
                $.each(data.Search, function(index, element) {
                    str += "<tr>";
                    str += "<td>" + index + "</td>";
                    str += "<td>" + element.Title + "</td>";
                    str += "<td>" + element.Type + "</td>";
                    str += "<td>" + element.Year + "</td>";
                    str += "<td>" + element.imdbID + "</td>";
                    str += "</tr>";
                });

                str += '</table>';

                // insert the html
                $("#SampleResults").html(str);
          },
          error: function (request, status, error) { alert(status + ", " + error); }
        });
    }
    </script>
</head>
<body>


<!-- search textbox -->
<input type="text" id="title" placeholder="Enter Name for search">

<!-- do search button -->
<button type="text" id="SampleSearchButton">Search</button>

<!-- display results container -->
<div id="SampleResults"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

  • 您输入标题
  • 标题附加到一个url,它调用你的本地php文件
  • 本地php文件接受标题并将其附加到您要调用的API网址
  • 发出请求并返回内容
  • 然后 接受
  • 返回的内容
  • 检查console.log以获取确切的数据结构
  • 到主键&#34; title_popular&#34;和&#34; title_exact&#34;,这就是为什么有两个表
  • 提防&#34;描述&#34;和&#34; title_description&#34;,两者似乎都是一样的(API BUG?),所以它们打印两次!
  • 我没有时间完全构建表格
  • 也许你应该问别人,如何打印多层次的物品更优雅

PHP

IMDB-fetcher.php

 <?php
 $title = $_GET['title']; // <- you need to secure this
 echo file_get_contents(
    'http://www.imdb.com/xml/find?json=1&nr=1&tt=on&q=' . $title
 );

<强> HTML

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Sample</title>

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
    <script>
    $( document ).ready(function() {
        $("#SampleSearchButton").click(function() {
            getImdbInfo($("#title").val());
        })
    });

    // The function below takes the entered title and searchs imdb for a match then it displays as followed

    function getImdbInfo(title) {
         $.ajax({
          type: 'GET',
          url: "imdb-fetcher.php?title=" + title, // <-- request to PHP data fetcher
          dataType: "json",
          success: function(data) {
                // you get an object for iteration, see console for keys
                console.log(data);

                // table for title_popular
                var str = '<table>';
                str += "<thead><th>Index</th><th>id</th><th>title</th><th>title_desc</th><th>year</th><th>description</th></thead>";

                $.each(data.title_popular, function(index, element) {
                    str += "<tr>";
                    str += "<td>" + index + "</td>";
                    $.each(element, function(key, element) {
                        str += "<td>" + element + "</td>";
                    });
                    str += "</tr>";
                });

                str += '</table>';

                // table for title_exact
                str += '<table>';
                str += "<thead><th>Index</th><th>id</th><th>title</th><th>title_desc</th><th>year</th><th>description</th></thead>";

                $.each(data.title_exact, function(index, element) {
                    str += "<tr>";
                    str += "<td>" + index + "</td>";
                    $.each(element, function(key, element) {
                        str += "<td>" + element + "</td>";
                    });
                    str += "</tr>";
                });


                // insert the html
                $("#SampleResults").html(str);
          },
          error: function (request, status, error) { alert(status + " - " + error); }
        });
    }
    </script>
</head>
<body>


<!-- search textbox -->
<input type="text" id="title" placeholder="Enter Name for search">

<!-- do search button -->
<button type="text" id="SampleSearchButton">Search</button>

<!-- display results container -->
<div id="SampleResults"></div>
</body>
</html>

<强>结果

enter image description here