2017-06-19 29 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。通過這個會得到一個關聯數組而不是一個對象,那麼你可以像上面那樣索引它。