-1

I have the following code:

<?php   
    $id = 'bsTY5cTi3nI';
$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=snippet&id=" . $id . "&key=...");
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->{"entry"}->{'yt$statistics'}->{'viewCount'};
echo $views;
?>

I want to get the number of views of a YT video with PHP. But I get the following error message:

Notice: Undefined property: stdClass::$entry in D:\XAMPP\htdocs\Project1\WatchToGether\test2.php on line 5
Notice: Trying to get property 'yt$statistics' of non-object in D:\XAMPP\htdocs\Project1\WatchToGether\test2.php on line 5
Notice: Trying to get property 'viewCount' of non-object in D:\XAMPP\htdocs\Project1\WatchToGether\test2.php on line 5

Could someone please help me with my problem. I would appreciate answers.

stvar
  • 6,551
  • 2
  • 13
  • 28
  • Does this answer your question? [Reference - What does this error mean in PHP?](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – El_Vanja Nov 29 '20 at 14:34
  • @El_Vanja No :( – Nils Schmidt Nov 29 '20 at 14:37
  • 1
    I believe you should start learning [how to debug your code](https://stackify.com/php-debugging-guide/). When dumped, `$JSON_Data` shows nothing like the properties you're trying to access and that should've tipped you off that you're using the wrong API. – El_Vanja Nov 29 '20 at 14:45
  • 1
    @El_Vanja: sorry to intervene, but the OP is using the correct API for obtaining the info that he needs -- view count --, only that the structure of the API's response doesn't match his expectation; moreover, he's not passing the right endpoint request parameter either. See [Ro Achterberg's answer](https://stackoverflow.com/a/65061356/8327971) below for a correct query of and access to the view count property. – stvar Nov 29 '20 at 15:32
  • 1
    @stvar Yes, my bad, that should've been "using the API wrong", not "using the wrong API", it's just a wrong parameter. – El_Vanja Nov 29 '20 at 15:44
  • @stvar is there a way to get the views of a YT video with Javascript? – Nils Schmidt Nov 29 '20 at 19:51
  • @Nils: Sure there is. For example using [`GAPI`](https://github.com/google/google-api-javascript-client) (i.e. Google’s Client Library for Browser-side JavaScript). Post a new question, giving some context with respect to your Javascript programming environment; then I'll try to respond to it. – stvar Nov 29 '20 at 20:59
  • @Nils: Here is the YouTube Data API's [Javascript Quickstart](https://developers.google.com/youtube/v3/quickstart/js) doc. – stvar Nov 29 '20 at 21:06
  • @stvar thanks i have found a way with Jquery ;) – Nils Schmidt Nov 30 '20 at 21:59

1 Answers1

1

You're looking for the statistics object from the video resource. You can get it like so:

<?php

$id = 'bsTY5cTi3nI';
$JSON = file_get_contents("https://www.googleapis.com/youtube/v3/videos?part=statistics&id=" . $id . "&key=...");

$JSON_Data = json_decode($JSON);
$views = $JSON_Data->items[0]->statistics->viewCount;
echo $views; // 264447
stvar
  • 6,551
  • 2
  • 13
  • 28
Ro Achterberg
  • 2,504
  • 2
  • 17
  • 17