TMDB API PHP - 全部显示和流派

时间:2014-03-18 17:32:10

标签: php api

<?php
$ca = curl_init();
curl_setopt($ca, CURLOPT_URL, "http://api.themoviedb.org/3/configuration?api_key=5094e4539de46c1abd1461920f3a3fb9");
curl_setopt($ca, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ca, CURLOPT_HEADER, FALSE);
curl_setopt($ca, CURLOPT_HTTPHEADER, array("Accept: application/json"));
$response = curl_exec($ca);
curl_close($ca);
//var_dump($response);
$config = json_decode($response, true);


$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://api.themoviedb.org/3/movie/106646-the-wolf-of-wall-street?api_key=5094e4539de46c1abd1461920f3a3fb9");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("Accept: application/json"));
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);

//print_r($result);

$x=0; 
while($x<=0)
  {
  echo("<a href=\"#\"><img src='" . $config['images']['base_url'] . $config['images']['poster_sizes'][1] . $result['results'][$x]['poster_path'] . "'/></a>");
  echo (" ". $result['results'][$x]['title'] . "<br/>");
  echo ("<a target=\"_blank\" href=\"https://www.themoviedb.org/\">TMDb</a> ".         $result['results'][$x]['vote_average'] . "/10 <br/>");
  echo (" ". $result['results'][$x]['release_date']);
  echo (" ". $result['results'][$x]['genre']);
  $x++;
  }


?>

我试图展示'The Wall Of Wall Streets'的信息。这不起作用,我不太确定如何让它工作我已经环顾四周,这只是有点太混乱。请帮忙。

我还希望有一个页面,您可以选择一个类别,然后显示该类别中的电影(限制为每页15个) 如果有人能帮助我,我会说谢谢。谢谢

1 个答案:

答案 0 :(得分:0)

首先,您不需要API调用中的电影标题。你可以这样做:

http://api.themoviedb.org/3/movie/106646?api_key=5094e4539de46c1abd1461920f3a3fb9

这个调用从1个电影中返回信息,所以你不需要while循环来显示这些信息:

echo ("<a href=\"#\"><img src='" . $config['images']['base_url'] . $config['images']['poster_sizes'][1] . $result['poster_path'] . "'/></a>");
echo (" ". $result['title'] . "<br/>");
echo ("<a target=\"_blank\" href=\"https://www.themoviedb.org/\">TMDb</a> ".         $result['vote_average'] . "/10 <br/>");
echo (" ". $result['release_date']);
echo (" ". $result['genre']);

对于您的列表,首先使用ID:

构建一个类型列表
http://api.themoviedb.org/3/genre/movie/list?api_key=5094e4539de46c1abd1461920f3a3fb9

然后列出你的电影:

http://api.themoviedb.org/3/genre/28/movies?api_key=5094e4539de46c1abd1461920f3a3fb9&page_size=2

而不是while循环,你可以这样做foreach

foreach ($results as $movie) {
    echo '<li>'.$movie['title'].'</li>
}

您可以在此处找到有关TMDB API的所有详细信息: http://docs.themoviedb.apiary.io/reference/genres/genreidmovies/