如何从api响应中获取特定值到php变量?

时间:2020-04-28 11:07:14

标签: php json api json-api-response-converter

我使用了来自themoviedb.org的api服务, https://api.themoviedb.org/3/find/tt0986233?api_key=redacted&language=en-US&external_source=imdb_id

此API以JSON ARRAY格式显示数据。 像这样

{"movie_results":[{"adult":false,"backdrop_path":null,"genre_ids":[18,36],"id":10360,"original_language":"en","original_title":"Hunger","overview":"The story of Bobby Sands, the IRA member who led the 1981 hunger strike in which Republican prisoners tried to win political status. It dramatises events in the Maze prison in the six weeks prior to Sands’ death.","poster_path":"/84HdTM39G2MzyTl8N9R0wVU9I5b.jpg","release_date":"2008-05-15","title":"Hunger","video":false,"vote_average":7.3,"vote_count":655,"popularity":7.829}],"person_results":[],"tv_results":[],"tv_episode_results":[],"tv_season_results":[]}

现在,我想在两个php变量中获取original_title和overview的值。
注意:我已经看了很多代码,没有找到任何好的结果,我的问题一次被视为重复,因此我想请任何有足够技能的人给我解决方案。 / i>
非常感谢您。

1 个答案:

答案 0 :(得分:0)

您可以使用json_decode()函数将JSON转换为ObjectArray

<?php

$json = '{"movie_results":[{"adult":false,"backdrop_path":null,"genre_ids":[18,36],"id":10360,"original_language":"en","original_title":"Hunger","overview":"The story of Bobby Sands, the IRA member who led the 1981 hunger strike in which Republican prisoners tried to win political status. It dramatises events in the Maze prison in the six weeks prior to Sands’ death.","poster_path":"/84HdTM39G2MzyTl8N9R0wVU9I5b.jpg","release_date":"2008-05-15","title":"Hunger","video":false,"vote_average":7.3,"vote_count":655,"popularity":7.829}],"person_results":[],"tv_results":[],"tv_episode_results":[],"tv_season_results":[]}';

$arr = json_decode($json, TRUE);

print_r($arr['movie_results'][0]['original_language']);

请注意,传递TRUE作为第二个参数会将JSON对象转换为关联的数组

关联: 如果为TRUE,则返回的对象将转换为关联对象 数组。