简体   繁体   中英

How to get specific values from api response to php variable?

I have used an api service from themoviedb.org, https://api.themoviedb.org/3/find/tt0986233?api_key= redacted &language=en-US&external_source=imdb_id

This API is presenting the data in a JSON ARRAY format. like this

{"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":[]}

Now I would like to get the values of original_title and overview in two php variable.
Note: I have already looked tons of code and didn't find any good results and my question was deemed as duplicate once and thus I would like to request anyone with enough skill to give me a solution.
Thank you very much in Advance.

You can use 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']);

Note that passing TRUE as a second arguments will convert the JSON object to associated Array

assoc: When TRUE, returned objects will be converted into associative arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM