David Williams
David Williams

Reputation: 1

How to get a particular string from and api response?

I have been on a little project of mine now i want to find imdb_id using tmdb_id so for that I have been trying to use the API. https://api.themoviedb.org/3/tv/67026?api_key=myapikey&append_to_response=external_ids

which brings up the results like this

{
    "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"
    }
}

So now if I want to get just the IMDb_ID form the external_ids then what code should i use in PHP and store it in a variable.

Thank you very much in advance.

Upvotes: 0

Views: 142

Answers (1)

Milan Lakhani
Milan Lakhani

Reputation: 524

use the json_decode method to treat JSON like a PHP object

$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

hope this helps!!!

Upvotes: 1

Related Questions