2017-06-19 63 views
-2

显示数据这一切都是我的理解至今:如何使用电影DB API在PHP

如果我打开这个网址:

https://api.themoviedb.org/3/movie/tt0137523?api_key=522cec782xxxxxxxxxxxxxxxxxxxx 

我会在屏幕上得到以下数据:

{"adult":false,"backdrop_path":"/87hTDiay2N2qWyX4Ds7ybXi9h8I.jpg","belongs_to_collection":null,"budget":63000000,"genres":[{"id":18,"name":"Drama"}],"homepage":"http://www.foxmovies.com/movies/fight-club","id":550,"imdb_id":"tt0137523","original_language":"en","original_title":"Fight Club","overview":"A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy. Their concept catches on, with underground \"fight clubs\" forming in every town, until an eccentric gets in the way and ignites an out-of-control spiral toward oblivion.","popularity":9.884594999999999,"poster_path":"/adw6Lq9FiC9zjYEpOqfq03ituwp.jpg","production_companies":[{"name":"Regency Enterprises","id":508},{"name":"Fox 2000 Pictures","id":711},{"name":"Taurus Film","id":20555},{"name":"Linson Films","id":54050},{"name":"Atman Entertainment","id":54051},{"name":"Knickerbocker Films","id":54052}],"production_countries":[{"iso_3166_1":"DE","name":"Germany"},{"iso_3166_1":"US","name":"United States of America"}],"release_date":"1999-10-15","revenue":100853753,"runtime":139,"spoken_languages":[{"iso_639_1":"en","name":"English"}],"status":"Released","tagline":"How much can you know about yourself if you've never been in a fight?","title":"Fight Club","video":false,"vote_average":8.199999999999999,"vote_count":8036} 

它显示搏击俱乐部的数据,因为我把搏击俱乐部IMDB ID的URL tt0137523

现在,如果我去这个网址:

https://image.tmdb.org/t/p/w500/adw6Lq9FiC9zjYEpOqfq03ituwp.jpg 

我会得到搏击俱乐部的海报,因为我插入adw6Lq9FiC9zjYEpOqfq03ituwp这个关键的URL,这是我从数据中找到。

但如何使用它使用PHP代码?

只是给我看一个小小的演示:

如何在此代码中显示图像?

<?php 

$posterkey = somethinghere, which gets poster key 
echo "<img src='https://image.tmdb.org/t/p/w500/".$posterkey.".jpg 
'></img"; 
?> 
</body> 

编辑:我也做了这个代码,但不知道如何使用它

<?php 
$requestsDone = 0; 
$maxRequests = 2; 

while ($requestsDone < $maxRequests) { 
     $requestsDone++; 

    echo "Request number: ".$requestsDone."<br>"; 

    $response = file_get_contents("https://api.themoviedb.org/3/movie/".mt_rand(500,996)."?api_key=522xxxxxxxxxxxxxxxxxxxxx"); 
    $response = json_decode($response); 
    print_r ($response); 
    echo "<br><br><br>"; 
} 
?> 
+0

这不是一个让别人为你编写代码的好地方。尝试自己编写代码,并在出现问题时提出具体问题。 – smarx

+0

我会写整个代码。但给我一个小例子,如何使用它。我很困惑。我从1周,白天和黑夜尝试这个。请 – Josh

+0

我不知道,下一步该怎么做。我试图搜索很多,但没有任何帮助。只是小演示,这将给我一些想法@smarx – Josh

回答

0

此代码的工作。如果你还没有,试着弄清楚有什么不同。

$key = "<REDACTED>"; 

$json = file_get_contents("https://api.themoviedb.org/3/movie/tt0137523?api_key=$key"); 

$result = json_decode($json, true); 

$poster_path = $result["poster_path"]; 

echo "<img src=\"https://image.tmdb.org/t/p/w500$poster_path\">"; 

// Output: 
// <img src="https://image.tmdb.org/t/p/w500/adw6Lq9FiC9zjYEpOqfq03ituwp.jpg"> 

UPDATE

我认为这个问题是,你缺少您的来电json_decode第二个参数true。通过这个会得到一个关联数组而不是一个对象,那么你可以像上面那样索引它。