如何从 api 响应中获取特定的字符串?
Posted
技术标签:
【中文标题】如何从 api 响应中获取特定的字符串?【英文标题】:How to get a particular string from and api response? 【发布时间】:2020-06-27 04:53:32 【问题描述】:我一直在做我的一个小项目,现在我想使用 tmdb_id 找到 imdb_id,因此我一直在尝试使用 API。 https://api.themoviedb.org/3/tv/67026?api_key=myapikey&append_to_response=external_ids
这会带来这样的结果
"backdrop_path": "/hcFbIbDzsB9aSSw9VkSGFEl5sGO.jpg",
"created_by": [
"id": 230174,
"credit_id": "577eb8e5c3a368694a0027ac",
"name": "David Guggenheim",
"gender": 2,
"profile_path": "/hqSydaadHO6EsBIn3BQEzzfxNUY.jpg"
],
"episode_run_time": [42],
"first_air_date": "2016-09-21",
"genres": [
"id": 18,
"name": "Drama"
,
"id": 10768,
"name": "War & Politics"
],
"homepage": "https://www.netflix.com/title/80113647",
"id": 67026,
"in_production": false,
"languages": ["en"],
"last_air_date": "2019-06-07",
"last_episode_to_air":
"air_date": "2019-06-07",
"episode_number": 10,
"id": 1809432,
"name": "#truthorconsequences",
"overview": "On election day, Kirkman turns to his therapist to assuage his conscience about the events -- and his own decisions -- of the momentous prior 36 hours.",
"production_code": "",
"season_number": 3,
"show_id": 67026,
"still_path": "/cpy3uV100RyZuvJN535JLTrj4Nz.jpg",
"vote_average": 7.0,
"vote_count": 1
,
"name": "Designated Survivor",
"next_episode_to_air": null,
"networks": [
"name": "ABC",
"id": 2,
"logo_path": "/ndAvF4JLsliGreX87jAc9GdjmJY.png",
"origin_country": "US"
,
"name": "Netflix",
"id": 213,
"logo_path": "/wwemzKWzjKYJFfCeiB57q3r4Bcm.png",
"origin_country": ""
],
"number_of_episodes": 53,
"number_of_seasons": 3,
"origin_country": ["US"],
"original_language": "en",
"original_name": "Designated Survivor",
"overview": "Tom Kirkman, a low-level cabinet member is suddenly appointed President of the United States after a catastrophic attack during the State of the Union kills everyone above him in the Presidential line of succession.",
"popularity": 30.031,
"poster_path": "/5R125JAIh1N38pzHp2dRsBpOVNY.jpg",
"production_companies": [
"id": 28788,
"logo_path": null,
"name": "Genre Films",
"origin_country": "US"
,
"id": 19366,
"logo_path": "/vOH8dyQhLK01pg5fYkgiS31jlFm.png",
"name": "ABC Studios",
"origin_country": "US"
,
"id": 78984,
"logo_path": null,
"name": "Entertainment 360",
"origin_country": "US"
],
"seasons": [
"air_date": "2016-09-20",
"episode_count": 21,
"id": 78328,
"name": "Season 1",
"overview": "Tom Kirkman, a low-level cabinet member is suddenly appointed President of the United States after a catastrophic attack during the State of the Union kills everyone above him in the Presidential line of succession.",
"poster_path": "/1QHlD6z9FnXuuTDVLJnjrtLfVyq.jpg",
"season_number": 1
,
"air_date": "2017-09-27",
"episode_count": 22,
"id": 91130,
"name": "Season 2",
"overview": "",
"poster_path": "/z4hdj8cYyqCO9lVBOGm6YZsnMho.jpg",
"season_number": 2
,
"air_date": "2019-06-07",
"episode_count": 10,
"id": 122914,
"name": "Season 3",
"overview": "",
"poster_path": "/wn310FWQhjjpHbqsMRBcXr28EHc.jpg",
"season_number": 3
],
"status": "Canceled",
"type": "Scripted",
"vote_average": 7.2,
"vote_count": 408,
"external_ids":
"imdb_id": "tt5296406",
"freebase_mid": null,
"freebase_id": null,
"tvdb_id": 311876,
"tvrage_id": 51115,
"facebook_id": "DesignatedSurvivor",
"instagram_id": "designatedsurvivor",
"twitter_id": "DesignatedNFLX"
所以现在如果我只想从 external_ids 中获取 IMDb_ID,那么我应该在 php 中使用什么代码并将其存储在变量中。
非常感谢您。
【问题讨论】:
如果您能够在没有问题的情况下获得响应,只需使用json_decode
,然后像往常一样将其视为对象/数组并将其分配到变量中
对不起,您在问 PHP - javascript 标签让我失望
可能应该删除javascript(和html)标签,也许用JSON替换它。
这可能与以下内容重复:***.com/questions/29308898/…
【参考方案1】:
使用 json_decode
方法将 JSON 视为 PHP 对象
$data = 'your JSON object';//raw data as string in $data
$decoded = json_decode($data);//decoded data as PHP object
echo $decoded->external_ids->imdb_id; //access any property you like
希望对你有帮助!!!
【讨论】:
以上是关于如何从 api 响应中获取特定的字符串?的主要内容,如果未能解决你的问题,请参考以下文章