通过PHP从URL获取JSON

Moh*_*ast 2 php json getjson

我有一个返回JSON对象的URL,如下所示:

[
    {
        "idIMDB": "tt0111161",
        "ranking": 1,
        "rating": "9.2",
        "title": "The Shawshank Redemption",
        "urlPoster": "http:\/\/ia.media-imdb.com\/images\/M\/MV5BODU4MjU4NjIwNl5BMl5BanBnXkFtZTgwMDU2MjEyMDE@._V1_UX34_CR0,0,34,50_AL_.jpg",
        "year": "1994"
    }
]
Run Code Online (Sandbox Code Playgroud)

网址:http://www.myapifilms.com/imdb/top

我想得到所有的urlPoster值并设置在数组的元素中,并将数组转换为JSON以便回显它.

我怎么能通过PHP来做到这一点?

Git*_* B. 9

你可以这样做:

<?php 
$json_url = "http://www.myapifilms.com/imdb/top";
$json = file_get_contents($json_url);
$data = json_decode($json, TRUE);
echo "<pre>";
print_r($data);
echo "</pre>";
?>
Run Code Online (Sandbox Code Playgroud)


小智 5

$json = file_get_contents('http://www.myapifilms.com/imdb/top');

$array = json_decode($json);

$urlPoster=array();
foreach ($array as $value) { 
    $urlPoster[]=$value->urlPoster;
}

print_r($urlPoster);
Run Code Online (Sandbox Code Playgroud)