1

How can I get videoId (selected on image) in PHP? It's my code and it's not working (I'm talking about echo [...]). It returns:

Fatal error: Cannot access empty property in /home/****/public_html/ytshots/index.php on line 38

$url = 'https://www.googleapis.com/youtube/v3/search?maxResults=4&type=video&order=date&part=snippet&forUsername=MKL&key=****';
$data = file_get_contents($url);
$characters = json_decode($data);

echo $characters->$items['videoId'];

image

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71

1 Answers1

1

Assuming that $characters represents the JSON object in your image, you're looking for the following:

echo $characters->items[0]->id->videoId;

This is because items is a key in the JSON array. items is actually an array, and you're trying to access the first index of it, so you need [0]. Then you need to target the next key down, which is id, not videoID. Then once you are inside the id object, you can access videoID as another key.

Hope this helps! :)

Obsidian Age
  • 41,205
  • 10
  • 48
  • 71
  • It's working! Thank you very much :)! Sorry, but I can't give the vote for your answer :(. – Tomasz Chwicewski Jul 31 '17 at 00:24
  • @TomaszChwicewski - you can accept the answer though. – Alex Jul 31 '17 at 00:26
  • Awesome! Glad to hear this solution appears to have helped you. Once you've confirmed this solution solves your problem, please don't forget to mark the solution as correct by clicking on the grey check below the vote buttons -- this removes it from the 'Unanswered Questions' queue, and awards reputation to both the question asker and question answerer. You'll be able to do that 15 minutes after asking any question. Of course, in saying that, you are under no obligation to mark my answer (or any other answer) as correct, though it does help to keep things on StackOverflow flowing smoothly :) – Obsidian Age Jul 31 '17 at 00:27