如何从Laravel 5.4上的tmdb API获取发布日期?

时间:2017-05-18 12:07:28

标签: php api laravel-5.4 themoviedb-api

我正在使用Laravel PHP Framework,我想使用tmdb包。

我可以在json中看到发布日期。这是截图

http://prntscr.com/f98ydy

但我希望在我看来这样,我可以用代码获得标题:

{!! $movie->getTitle() !!}

如何获得发布日期?我也试过那个代码

{!! $movie->getReleaseDate() !!}

但是向我显示了这个错误

Object of class DateTime could not be converted to string (View: C:\xampp\htdocs\hallofsound\resources\views\welcome.blade.php)

这是我的完整Json输出

   ResultCollection {#1811 ▼
  -page: 1
  -totalPages: 36
  -totalResults: 702
  #data: array:20 [▼
    "000000004255a5960000000012a0a3a3" => Movie {#1816 ▼
      -adult: false
      -backdropPath: "/aJn9XeesqsrSLKcHfHP4u5985hn.jpg"
      -backdrop: BackdropImage {#1327 ▶}
      -belongsToCollection: null
      -budget: null
      -genres: Genres {#1832 ▶}
      -homepage: null
      -id: 283995
      -imdbId: null
      -originalTitle: "Guardians of the Galaxy Vol. 2"
      -originalLanguage: "en"
      -overview: "The Guardians must fight to keep their newfound family together as they unravel the mysteries of Peter Quill's true parentage."
      -popularity: 115.058584
      -poster: PosterImage {#1837 ▶}
      -posterPath: "/y4MBh0EjBlMuOzv9axM4qJlmhzz.jpg"
      -productionCompanies: GenericCollection {#1331 ▶}
      -productionCountries: GenericCollection {#1830 ▶}
      -releaseDate: DateTime {#1840 ▶}
      -revenue: null
      -runtime: null
      -spokenLanguages: GenericCollection {#1828 ▶}
      -status: null
      -tagline: null
      -title: "Guardians of the Galaxy Vol. 2"
      -voteAverage: 7.6
      -voteCount: 1539
      #alternativeTitles: GenericCollection {#1826 ▶}
      #changes: GenericCollection {#1829 ▶}
      #credits: CreditsCollection {#1821 ▶}
      #images: Images {#1319 ▶}
      #keywords: GenericCollection {#1824 ▶}
      #lists: GenericCollection {#1820 ▶}
      #releases: GenericCollection {#1316 ▶}
      #release_dates: GenericCollection {#1822 ▶}
      #similar: GenericCollection {#1819 ▶}
      #translations: GenericCollection {#1825 ▶}
      #reviews: null
      #videos: Videos {#1831 ▶}
    }
    "000000004255a5a00000000012a0a3a3" => Movie {#1838 ▶}
    "000000004255a5c90000000012a0a3a3" => Movie {#1863 ▶}
    "000000004255a5ee0000000012a0a3a3" => Movie {#1888 ▶}
    "000000004255a5f70000000012a0a3a3" => Movie {#1913 ▶}
    "000000004255a51f0000000012a0a3a3" => Movie {#1937 ▶}
    "000000004255a5240000000012a0a3a3" => Movie {#1962 ▶}
    "000000004255a54d0000000012a0a3a3" => Movie {#1987 ▶}
    "000000004255a5550000000012a0a3a3" => Movie {#2011 ▶}
    "000000004255a57d0000000012a0a3a3" => Movie {#2035 ▶}
    "000000004255aa840000000012a0a3a3" => Movie {#2058 ▶}
    "000000004255aaac0000000012a0a3a3" => Movie {#2082 ▶}
    "000000004255aab50000000012a0a3a3" => Movie {#2107 ▶}
    "000000004255aadb0000000012a0a3a3" => Movie {#2133 ▶}
    "000000004255aae00000000012a0a3a3" => Movie {#2158 ▶}
    "000000004255aa080000000012a0a3a3" => Movie {#2182 ▶}
    "000000004255aa110000000012a0a3a3" => Movie {#2207 ▶}
    "000000004255aa360000000012a0a3a3" => Movie {#2232 ▶}
    "000000004255aa5f0000000012a0a3a3" => Movie {#2257 ▶}
    "000000004255aa640000000012a0a3a3" => Movie {#2282 ▶}
  ]
}

1 个答案:

答案 0 :(得分:1)

JSON响应是一个集合。所以你需要循环它。见here

所以也许是这样的:

foreach($movies as $movie)
{
    var_dump($movie->getTitle());
}

方法getReleaseDate也将返回DateTime个对象。要格式化它,你可以做这样的事情:

$date   = new DateTime($movie->getReleaseDate());
$result = $date->format('Y-m-d H:i:s');

或在Laravel中,你可以这样做:

{!! $movie->getReleaseDate()->format('Y-m-d H:i:s') !!}