我正在製作一個電影網站(顯然沒有完成)。我想知道如何從API導出數據並從PHP上的JSON數據中選擇一個數組。在這種情況下,我想輸出電影的標題,但它似乎不起作用。
我得到這個錯誤:
從錯誤中,我知道,我試圖輸出對象,而不是一個字符串,但我不知道如何解決它。
這是我的index.php文件:
<?php
require_once('includes/variables.php');
?>
<!DOCTYPE html>
<html>
<body>
<?php
echo $movieNameList->data->movies[1]->id;
?>
</body>
</html>
包括/ variables.php文件:
<?php
$getMovieList = file_get_contents('https://yts.to/api/v2/list_movies.json');
$movieNameList = json_decode($getMovieList[0]);
?>
https://yts.to/api/v2/list_movies.json文件,如果你懶:
{
"status":"ok",
"status_message":"Query was successful",
"data":{
"movie_count":4220,
"limit":2,
"page_number":1,
"movies":[
{
"id":4247,
"url":"https:\/\/yts.to\/movie\/rem-by-mtv-2014",
"imdb_code":"tt4066748",
"title":"R.E.M. by MTV",
"title_long":"R.E.M. by MTV (2014)",
"slug":"rem-by-mtv-2014",
"year":2014,
"rating":8,
"runtime":107,
"genres":[
"Documentary"
],
"language":"English",
"mpa_rating":"Unknown",
"background_image":"https:\/\/s.ynet.io\/assets\/images\/movies\/rem_by_mtv_2014\/background.jpg",
"small_cover_image":"https:\/\/s.ynet.io\/assets\/images\/movies\/rem_by_mtv_2014\/small-cover.jpg",
"medium_cover_image":"https:\/\/s.ynet.io\/assets\/images\/movies\/rem_by_mtv_2014\/medium-cover.jpg",
"state":"ok",
"torrents":[
{
"url":"https:\/\/yts.to\/torrent\/download\/1F28D13F40AE91AECC58D649F5F9D84D29321632.torrent",
"hash":"1F28D13F40AE91AECC58D649F5F9D84D29321632",
"quality":"720p",
"seeds":1063,
"peers":544,
"size":"812.23 MB",
"size_bytes":851680192,
"date_uploaded":"2015-06-28 08:49:09",
"date_uploaded_unix":1435438149
},
{
"url":"https:\/\/yts.to\/torrent\/download\/DED26397FD36DFC932BB5EEEC82D25204699943C.torrent",
"hash":"DED26397FD36DFC932BB5EEEC82D25204699943C",
"quality":"1080p",
"seeds":247,
"peers":419,
"size":"1.65 GB",
"size_bytes":1766838835,
"date_uploaded":"2015-06-28 22:55:41",
"date_uploaded_unix":1435488941
}
],
"date_uploaded":"2015-06-28 08:49:06",
"date_uploaded_unix":1435438146
},
{
"id":4245,
"url":"https:\/\/yts.to\/movie\/bigfoot-county-2012",
"imdb_code":"tt2108605",
"title":"Bigfoot County",
"title_long":"Bigfoot County (2012)",
"slug":"bigfoot-county-2012",
"year":2012,
"rating":2.9,
"runtime":82,
"genres":[
"Horror",
"Mystery"
],
"language":"English",
"mpa_rating":"R",
"background_image":"https:\/\/s.ynet.io\/assets\/images\/movies\/bigfoot_county_2012\/background.jpg",
"small_cover_image":"https:\/\/s.ynet.io\/assets\/images\/movies\/bigfoot_county_2012\/small-cover.jpg",
"medium_cover_image":"https:\/\/s.ynet.io\/assets\/images\/movies\/bigfoot_county_2012\/medium-cover.jpg",
"state":"ok",
"torrents":[
{
"url":"https:\/\/yts.to\/torrent\/download\/25E5FBDAD49BFD067EEB7778EF1CED753E0E608C.torrent",
"hash":"25E5FBDAD49BFD067EEB7778EF1CED753E0E608C",
"quality":"720p",
"seeds":324,
"peers":183,
"size":"693.01 MB",
"size_bytes":726671831,
"date_uploaded":"2015-06-27 14:02:07",
"date_uploaded_unix":1435370527
}
],
"date_uploaded":"2015-06-27 14:02:06",
"date_uploaded_unix":1435370526
}
]
},
"@meta":{
"server_time":1435498431,
"server_timezone":"Pacific\/Auckland",
"api_version":2,
"execution_time":"12.3 ms"
}
}
好,f *王謝天謝地:D。我忘記了file_get_contents函數。 –
另外,如何選擇,比如說第二個數組?例如,Bigfoot Country電影。 echo $ movieNameList-> data-> movies [1] - > title;我應該在哪裏改變? –
使用[1]時,您正在選擇第二個陣列發生記住陣列從[0]開始 – RiggsFolly