如何从 api 对 php 变量的响应中获取特定值?
How to get specific values from api response to php variable?
我使用了 themoviedb.org 的 api 服务,
https://api.themoviedb.org/3/find/tt0986233?api_key=redacted&language=en-US&external_source=imdb_id
此 API 以 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":[]}
现在我想在两个 php 变量中获取 original_title 和概述的值。
注意:我已经看了很多代码,没有找到任何好的结果,我的问题被认为是重复的一次,因此我想请有足够技能的人给我一个解决方案。
非常感谢您。
您可以使用json_decode()
function to convert your JSON to Object
or Array
<?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 对象转换为关联的数组
assoc:
When TRUE, returned objects will be converted into associative
arrays.
我使用了 themoviedb.org 的 api 服务, https://api.themoviedb.org/3/find/tt0986233?api_key=redacted&language=en-US&external_source=imdb_id
此 API 以 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":[]}
现在我想在两个 php 变量中获取 original_title 和概述的值。
注意:我已经看了很多代码,没有找到任何好的结果,我的问题被认为是重复的一次,因此我想请有足够技能的人给我一个解决方案。
非常感谢您。
您可以使用json_decode()
function to convert your JSON to Object
or Array
<?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 对象转换为关联的数组
assoc: When TRUE, returned objects will be converted into associative arrays.