从PHP

时间:2015-06-28 13:45:41

标签: php json api

大家好!

我正在电影网站上工作(显然没有完成)。我想知道如何从API导出数据并从PHP上的JSON数据中选择一个数组。在这种情况下,我想输出电影的标题,但似乎无法正常工作。

我收到此错误:

Notice: Trying to get property of non-object. 

从错误中,我知道我试图输出一个对象,而不是一个字符串,但我不知道如何解决它。

这是我的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"
   }
}

2 个答案:

答案 0 :(得分:1)

确保输入正确的密钥名称。这个标题的引用是有效的:

<?php
$movieNameList->data->movies[1]->title;

您也可以通过将第二个参数设置为true来将json解码为数组:

json_decode($getMovieList, true);

或将属性转换为字符串:

echo (string) $object->property;

编辑:

这一行:

$movieNameList = json_decode($getMovieList[0]);

应该是:

$movieNameList = json_decode($getMovieList);

答案 1 :(得分:1)

这是你的错误

 <?php
 $getMovieList = file_get_contents('https://yts.to/api/v2/list_movies.json');
 $movieNameList = json_decode($getMovieList[0]);  // <-- Error line
 ?>

应该是

 <?php
 $getMovieList = file_get_contents('https://yts.to/api/v2/list_movies.json');
 $movieNameList = json_decode($getMovieList);
 ?>

file_get_content的结果是一个简单的字符串而不是数组