alaouy / YouTube

🔥 Laravel PHP Facade/Wrapper for the Youtube Data API
MIT License
778 stars 204 forks source link

Undefined property: stdClass::$items in public function decodeSingle() #148

Closed yankiva closed 4 years ago

yankiva commented 4 years ago

Somethimes $items not present in the response class stdClass#970 (3) { public $kind => string(27) "youtube#channelListResponse" public $etag => string(27) "XXX" public $pageInfo => class stdClass#971 (2) { public $totalResults => int(0) public $resultsPerPage => int(5) } } So, script crashes with error: In Youtube.php line 599:

Undefined property: stdClass::$items

In public function decodeSingle(&$apiData).

alaouy commented 4 years ago

@yankiva Can provide a code snippet of the method you are calling and the parameter to use in order to reproduce this? thanks

yankiva commented 4 years ago

We got a list of Youtube users/channels to retrieve stats. One of our channels were disabled, so we got this error and it breaks all further actions. It could be useful to have some log record here instead error.

-- $this->youtubeObject = new Youtube($config["youtube_apikey"]); foreach($accounts as $accountId){ $views = $this->youtubeObject->getChannelByName($accountId, false, ["statistics"]); ... }

In Youtube.php line 601:

[ErrorException]
Undefined property: stdClass::$items

Exception trace: at /var/www/project/vendor/alaouy/youtube/src/Youtube.php:601 Illuminate\Foundation\Bootstrap\HandleExceptions->handleError() at /var/www/project/vendor/alaouy/youtube/src/Youtube.php:601 Alaouy\Youtube\Youtube->decodeSingle() at /var/www/project/vendor/alaouy/youtube/src/Youtube.php:346 Alaouy\Youtube\Youtube->getChannelByName() at /var/www/project/app/Console/Commands/loadSocialData.php:157

alaouy commented 4 years ago

@yankiva The method getChannelByName and all other methods interacting with the youtube api will throw an exception if the api response is not what you would expect, so you can surround your code with a try catch block and handle the exceptions there.