简体   繁体   English

如何在PHP中使用Ajax从网站(例如IMDB)中获取数据

[英]How can I fetch data from a website (e.g. IMDB ) using Ajax in PHP

I am developing a site for thriller movies. 我正在开发一个惊悚电影网站。 I want to fetch data from IMDB. 我想从IMDB中获取数据。 I need to use Ajax to fetch data asynchronously. 我需要使用Ajax异步获取数据。 My code does not work well. 我的代码运行不正常。

The movie info is not appearing in the carousal. 电影信息未出现在转盘中。 The html file which fetch data : 提取数据的html文件:

  <?php
       require_once('functions.php'); 
       $triangle = getMovieInfo('triangle');
       $Predestination = getMovieInfo('Predestination');
       $silence_of_the_lambs = getMovieInfo('The Silence of the Lambs');
       $shutter_island = getMovieInfo('Shutter Island');
    ?>
    <br>
  <!-- Text which has greater value -->
    <div>
      <div class = "row">
        <div class="alert alert-success text-center" role="alert"><h3>If you are looking for a site, where you can see the list of all verified thrillers, then you are at the right place.</h3></div>
      </div>
    </div>
    <!-- slider for the home page -->

        <div class="container">

      <div class="row">
        <div class = "col-md-6 col-lg-6 col-sm-6">
          <div id="myCarousel" class="carousel slide" data-ride="carousel">
            <!-- Indicators -->
            <ol class="carousel-indicators">
              <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
              <li data-target="#myCarousel" data-slide-to="1"></li>
              <li data-target="#myCarousel" data-slide-to="2"></li>
              <li data-target="#myCarousel" data-slide-to="3"></li>
            </ol>

            <!-- Wrapper for slides -->
            <div class="carousel-inner" role="listbox">

              <div class="item active">
                <?php  echo "<img src=\"$triangle->Poster\">"; ?>
                <!-- <span> <?php echo "Plot : ".$triangle->Plot; ?> </span> -->
              </div>
              <div class="item">
                <?php  echo "<img src=\"$silence_of_the_lambs->Poster\">"; ?>
                 <!-- <span> <?php echo "Plot : ".$silence_of_the_lambs->Plot; ?> </span> -->
              </div>
              <div class="item">
                <?php  echo "<img src=\"$Predestination->Poster\">"; ?>
                 <!-- <span> <?php echo "Plot : ".$Predestination->Plot; ?> </span> -->
              </div>
              <div class="item">
                <?php  echo "<img src=\"$shutter_island->Poster\">"; ?>
                 <!-- <span> <?php echo "Plot : ".$shutter_island->Plot; ?> </span> -->
              </div>
            </div>

            <!-- Left and right controls -->
            <a class="left carousel-control" href="#myCarousel" role="button" data-slide="prev">
              <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
              <span class="sr-only">Previous</span>
            </a>
            <a class="right carousel-control" href="#myCarousel" role="button" data-slide="next">
              <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
              <span class="sr-only">Next</span>
            </a>
          </div>  
          </div>
          <div class ="col-md-6 col-lg-6 col-sm-6">
             <div class="well">
              <div class="thriller">
                  <h3>What is a thriller movie?</h3>
                  <h4>If the genre is to be defined strictly, a genuine thriller is a film that rentlessly pursues a single-minded goal - to provide thrills and keep the audience cliff-hanging at the 'edge of their seats' as the plot builds towards a climax.</h4>
                  <h4>
                   Believe me, I love watching thriller movies more than anything. These movies are real movies, where you can't guess the last scene. I enjoyed a lot of thriller movies and then thought to make a site, where you can easily watch a movie.
                  </h4>
              </div>
             </div>
          </div>
        </div>

    </div>

The getMovieInfo function is here: function getMovieInfo($title) { $title = urlencode($title); getMovieInfo函数在此处:function getMovieInfo($ title){$ title = urlencode($ title);

    //Call the omdb api
    $json=file_get_contents("http://www.omdbapi.com/?t=$title");

    $details=json_decode($json);

    //Check if response contains the movie information
    if($details->Response=='True')
    {   

        return $details;

    }

    else 
    {
         echo "Movie information not available.Please confirm title";
    }

  }

My java script file is: 我的Java脚本文件是:

$(document).ready(function() 
{
  $("#home").click(function(event)
  {
      alert("home is clicked");
      $('#contents').load('home.html');
  });


});

电影信息未打印。 Please help me to solve this. 请帮我解决这个问题。

Your file needs to be .php, not .html as you are using PHP in your code. 您的文件必须是.php,而不是.html,因为您在代码中使用PHP。

Don't worry though, HTML will still be parsed as normal. 不过请放心,HTML仍将被正常解析。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 python 编码读取特定网站的控制台日志(例如,ok、connected 之类的消息)? - How can I read console log (e.g. messages like ok, connected..) of particular website by using a python coding? 我如何以编程方式查看网站发出的请求(例如 API 或资源请求) - How can I programmatically see what requests (e.g. API or resource requests) are being made by a website 如何使用 JavaScript 验证图像尺寸(表示为“5X4”)? - How can I validate image sizes (expressed as e.g. “5X4”) using JavaScript? 使用AJAX从IMDB API提取数据 - Using AJAX to Extract Data from IMDB API 我如何在jQuery中的页面加载时播放动画,以便它播放index.php而不播放other_page.php - How can I play an animation on page load in jQuery so that it plays for index.php but not e.g. other_page.php 如何更改网站上的滚动行为(例如速度、加速度)? - How to change scroll behavior (e.g. speed, acceleration) on website? 如何自定义FancyUpload以上传多个文件,每个文件都有自己的数据(例如标题,标题)? - How can I customize FancyUpload to upload multiple files, each with its own data (e.g. caption, title)? 如何限制 AJAX API 不被不必要的使用(例如某人执行 SELECT *) - How to restrict AJAX API from unwanted use (e.g. someone performing a SELECT *) 如何从ListView中包含(或扩展)Odoo核心功能(例如Instance.web.ListView.extend) - How can I include(or extend) Odoo core function from ListView (e.g. Instance.web.ListView.extend) 如何阻止emacs缩进javascript逗号分隔列表的第二行(例如array或json)? - How can I stop emacs from indenting the 2nd line of a javascript comma-separated list (e.g. array or json)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM