YouTube API v3 get latest video uploads for a username with PHP
Use the channels endpoint with a forUsername
attribute (see the documentation for the endpoint).
GET https://www.googleapis.com/youtube/v3/channels?part=id&forUsername={USERNAME}&key={YOUR_API_KEY}
It will give you this kind of response:
{ "kind": "youtube#channelListResponse", "etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/7Q8DJLb6b2hwYAXUQLI3ftt3R8U\"", "pageInfo": { "totalResults": 1, "resultsPerPage": 5 }, "items": [ { "kind": "youtube#channel", "etag": "\"tbWC5XrSXxe1WOAx6MK9z4hHSU8/pDjr9xPn51KtRM3gbMOq2aHBIbY\"", "id": "UCr3LuetcEeidWiuhxaw4B2g" } ]}
The channel ID of that user can be found at items[0]['id']
if you convert the JSON to array.
As for your edit, you’re accessing the JSON object incorrectly. You need to use something like this.
if (array_key_exists('items', $json) && count($json['items']) === 1) { $item = $json['items'][0]; $channelId = $item['id']; // The users channel ID.}