从JSON字符串/数组中提取第一个图像

时间:2011-05-02 14:07:24

标签: php json

我正试图让一个假电影网站参加一个大学项目。但是因为我是一名设计师,我在php上很糟糕(而且我的术语可能很偏僻)。如果某些事情没有意义或者解释得不好,请告诉我。

我正在使用http://themoviedb.org的API来引入电影数据,并在其中一个网页上挣扎。

我正在从他们的API中提取一个JSON字符串,并试图获得第一个'背景'图像(每个不同的电影有大量的背景幕,从无到约20个)。 “背景”也有不同的尺寸,从“原始”尺寸到“拇指”不等。我试图引入第一个'背景',称为尺寸'海报'。

所以这就是我所拥有的。问题是 - 对我来说 - 它看起来结构合理,但只是返回后备'no-backdrop.gif'。

<?php
    //TMDb id for a movie
    $tmdb_id = $_GET["id"];

    //Search Movie with default return format
    $movies_result = $tmdb->getMovie($tmdb_id);

    // Decode JSON into PHP array
    $movies = json_decode($movies_result);
?>
<?php
    // Set default poster image to use if movie doesn't have one
    $backdrop_url = 'no-backdrop.gif';

    if ($movies[0]->backdrops->image->size == 'poster') {
      $backdrop_url = $movies[0]->backdrops->image->url;
    }

    echo '<img src="' . $backdrop_url . '" />';
?>


这是json字符串。不幸的是,这很重要,所以我不得不删除与我无关的内容。

完整的字符串可以在下面链接页面的底部找到。

Array ( [0] => stdClass Object ( ......... [backdrops] => Array ( [0] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => original [height] => 1080 [width] => 1920 [url] => http://cf1.imgobject.com/backdrops/9b1/4d0095bd7b9aa1514a0069b1/black-swan-original.jpg [id] => 4d0095bd7b9aa1514a0069b1 ) ) [1] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => poster [height] => 439 [width] => 780 [url] => http://cf1.imgobject.com/backdrops/9b1/4d0095bd7b9aa1514a0069b1/black-swan-poster.jpg [id] => 4d0095bd7b9aa1514a0069b1 ) ) [2] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => thumb [height] => 169 [width] => 300 [url] => http://cf1.imgobject.com/backdrops/9b1/4d0095bd7b9aa1514a0069b1/black-swan-thumb.jpg [id] => 4d0095bd7b9aa1514a0069b1 ) ) [3] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => w1280 [height] => 720 [width] => 1280 [url] => http://cf1.imgobject.com/backdrops/9b1/4d0095bd7b9aa1514a0069b1/black-swan-w1280.jpg [id] => 4d0095bd7b9aa1514a0069b1 ) ) [4] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => original [height] => 1080 [width] => 1920 [url] => http://cf1.imgobject.com/backdrops/9bb/4d00977f7b9aa1514a0069bb/black-swan-original.jpg [id] => 4d00977f7b9aa1514a0069bb ) ) [5] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => poster [height] => 439 [width] => 780 [url] => http://cf1.imgobject.com/backdrops/9bb/4d00977f7b9aa1514a0069bb/black-swan-poster.jpg [id] => 4d00977f7b9aa1514a0069bb ) ) [6] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => thumb [height] => 169 [width] => 300 [url] => http://cf1.imgobject.com/backdrops/9bb/4d00977f7b9aa1514a0069bb/black-swan-thumb.jpg [id] => 4d00977f7b9aa1514a0069bb ) ) [7] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => w1280 [height] => 720 [width] => 1280 [url] => http://cf1.imgobject.com/backdrops/9bb/4d00977f7b9aa1514a0069bb/black-swan-w1280.jpg [id] => 4d00977f7b9aa1514a0069bb ) ) [8] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => original [height] => 1080 [width] => 1920 [url] => http://cf1.imgobject.com/backdrops/172/4d06bcb07b9aa11bc4003172/black-swan-original.jpg [id] => 4d06bcb07b9aa11bc4003172 ) ) [9] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => poster [height] => 439 [width] => 780 [url] => http://cf1.imgobject.com/backdrops/172/4d06bcb07b9aa11bc4003172/black-swan-poster.jpg [id] => 4d06bcb07b9aa11bc4003172 ) ) [10] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => thumb [height] => 169 [width] => 300 [url] => http://cf1.imgobject.com/backdrops/172/4d06bcb07b9aa11bc4003172/black-swan-thumb.jpg [id] => 4d06bcb07b9aa11bc4003172 ) ) [11] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => w1280 [height] => 720 [width] => 1280 [url] => http://cf1.imgobject.com/backdrops/172/4d06bcb07b9aa11bc4003172/black-swan-w1280.jpg [id] => 4d06bcb07b9aa11bc4003172 ) ) [12] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => original [height] => 1080 [width] => 1920 [url] => http://cf1.imgobject.com/backdrops/c50/4d4708625e73d65704003c50/black-swan-original.jpg [id] => 4d4708625e73d65704003c50 ) ) [13] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => poster [height] => 439 [width] => 780 [url] => http://cf1.imgobject.com/backdrops/c50/4d4708625e73d65704003c50/black-swan-poster.jpg [id] => 4d4708625e73d65704003c50 ) ) [14] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => thumb [height] => 169 [width] => 300 [url] => http://cf1.imgobject.com/backdrops/c50/4d4708625e73d65704003c50/black-swan-thumb.jpg [id] => 4d4708625e73d65704003c50 ) ) [15] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => w1280 [height] => 720 [width] => 1280 [url] => http://cf1.imgobject.com/backdrops/c50/4d4708625e73d65704003c50/black-swan-w1280.jpg [id] => 4d4708625e73d65704003c50 ) ) [16] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => original [height] => 1080 [width] => 1920 [url] => http://cf1.imgobject.com/backdrops/452/4d0a52997b9aa162af000452/black-swan-original.jpg [id] => 4d0a52997b9aa162af000452 ) ) [17] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => poster [height] => 439 [width] => 780 [url] => http://cf1.imgobject.com/backdrops/452/4d0a52997b9aa162af000452/black-swan-poster.jpg [id] => 4d0a52997b9aa162af000452 ) ) [18] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => thumb [height] => 169 [width] => 300 [url] => http://cf1.imgobject.com/backdrops/452/4d0a52997b9aa162af000452/black-swan-thumb.jpg [id] => 4d0a52997b9aa162af000452 ) ) [19] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => w1280 [height] => 720 [width] => 1280 [url] => http://cf1.imgobject.com/backdrops/452/4d0a52997b9aa162af000452/black-swan-w1280.jpg [id] => 4d0a52997b9aa162af000452 ) ) [20] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => original [height] => 1080 [width] => 1920 [url] => http://cf1.imgobject.com/backdrops/443/4d0a514c7b9aa162af000443/black-swan-original.jpg [id] => 4d0a514c7b9aa162af000443 ) ) [21] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => poster [height] => 439 [width] => 780 [url] => http://cf1.imgobject.com/backdrops/443/4d0a514c7b9aa162af000443/black-swan-poster.jpg [id] => 4d0a514c7b9aa162af000443 ) ) [22] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => thumb [height] => 169 [width] => 300 [url] => http://cf1.imgobject.com/backdrops/443/4d0a514c7b9aa162af000443/black-swan-thumb.jpg [id] => 4d0a514c7b9aa162af000443 ) ) [23] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => w1280 [height] => 720 [width] => 1280 [url] => http://cf1.imgobject.com/backdrops/443/4d0a514c7b9aa162af000443/black-swan-w1280.jpg [id] => 4d0a514c7b9aa162af000443 ) ) [24] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => original [height] => 1080 [width] => 1920 [url] => http://cf1.imgobject.com/backdrops/1af/4cd5d8597b9aa11b270001af/black-swan-original.jpg [id] => 4cd5d8597b9aa11b270001af ) ) [25] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => poster [height] => 439 [width] => 780 [url] => http://cf1.imgobject.com/backdrops/1af/4cd5d8597b9aa11b270001af/black-swan-poster.jpg [id] => 4cd5d8597b9aa11b270001af ) ) [26] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => thumb [height] => 169 [width] => 300 [url] => http://cf1.imgobject.com/backdrops/1af/4cd5d8597b9aa11b270001af/black-swan-thumb.jpg [id] => 4cd5d8597b9aa11b270001af ) ) [27] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => w1280 [height] => 720 [width] => 1280 [url] => http://cf1.imgobject.com/backdrops/1af/4cd5d8597b9aa11b270001af/black-swan-w1280.jpg [id] => 4cd5d8597b9aa11b270001af ) ) [28] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => original [height] => 1080 [width] => 1920 [url] => http://cf1.imgobject.com/backdrops/453/4d0a527d7b9aa162bf000453/black-swan-original.jpg [id] => 4d0a527d7b9aa162bf000453 ) ) [29] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => poster [height] => 439 [width] => 780 [url] => http://cf1.imgobject.com/backdrops/453/4d0a527d7b9aa162bf000453/black-swan-poster.jpg [id] => 4d0a527d7b9aa162bf000453 ) ) [30] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => thumb [height] => 169 [width] => 300 [url] => http://cf1.imgobject.com/backdrops/453/4d0a527d7b9aa162bf000453/black-swan-thumb.jpg [id] => 4d0a527d7b9aa162bf000453 ) ) [31] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => w1280 [height] => 720 [width] => 1280 [url] => http://cf1.imgobject.com/backdrops/453/4d0a527d7b9aa162bf000453/black-swan-w1280.jpg [id] => 4d0a527d7b9aa162bf000453 ) ) [32] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => original [height] => 1080 [width] => 1920 [url] => http://cf1.imgobject.com/backdrops/ed2/4d616c627b9aa154bf004ed2/black-swan-original.jpg [id] => 4d616c627b9aa154bf004ed2 ) ) [33] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => poster [height] => 439 [width] => 780 [url] => http://cf1.imgobject.com/backdrops/ed2/4d616c627b9aa154bf004ed2/black-swan-poster.jpg [id] => 4d616c627b9aa154bf004ed2 ) ) [34] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => thumb [height] => 169 [width] => 300 [url] => http://cf1.imgobject.com/backdrops/ed2/4d616c627b9aa154bf004ed2/black-swan-thumb.jpg [id] => 4d616c627b9aa154bf004ed2 ) ) [35] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => w1280 [height] => 720 [width] => 1280 [url] => http://cf1.imgobject.com/backdrops/ed2/4d616c627b9aa154bf004ed2/black-swan-w1280.jpg [id] => 4d616c627b9aa154bf004ed2 ) ) [36] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => original [height] => 1080 [width] => 1920 [url] => http://cf1.imgobject.com/backdrops/486/4d0a526e7b9aa162b7000486/black-swan-original.jpg [id] => 4d0a526e7b9aa162b7000486 ) ) [37] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => poster [height] => 439 [width] => 780 [url] => http://cf1.imgobject.com/backdrops/486/4d0a526e7b9aa162b7000486/black-swan-poster.jpg [id] => 4d0a526e7b9aa162b7000486 ) ) [38] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => thumb [height] => 169 [width] => 300 [url] => http://cf1.imgobject.com/backdrops/486/4d0a526e7b9aa162b7000486/black-swan-thumb.jpg [id] => 4d0a526e7b9aa162b7000486 ) ) [39] => stdClass Object ( [image] => stdClass Object ( [type] => backdrop [size] => w1280 [height] => 720 [width] => 1280 [url] => http://cf1.imgobject.com/backdrops/486/4d0a526e7b9aa162b7000486/black-swan-w1280.jpg [id] => 4d0a526e7b9aa162b7000486 ) ) ) ......... ) ) 


或者看到它失败了,你可以去这里 - http://rowancavanagh.com/getfilm/tmdb_movie.php?id=44214
(在底部印有json字符串)

1 个答案:

答案 0 :(得分:1)

backdrops属性是一个数组,您必须以数组形式访问它:

$backdrop_url = $movies[0]->backdrops[0]->image->url;

尝试开启error_reporting和/或display_errorslog_errors,这是一个容易被这些错误捕获的错误:“试图获取非对象的属性“

另外,在isset中使用if可防止在没有图片的情况下弹出E_NOTICE:

if (isset($movies[0]->backdrops[0]->image->size) &&
    $movies[0]->backdrops[0]->image->size == 'poster') 
{
    // stuff
}

如果backdrops中有多个元素,并且只想获取poster类型的元素,请使用foreach循环:

foreach ($movies[0]->backdrops as $backdrop) {
    if (isset($backdrop->image->size) && $backdrop->image->size == 'poster') {
        $backdrop_url = $backdrop->image->url;
        break;
    }
}