alaouy / YouTube

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

Cannot execute getVideoInfo when running with Apache #138

Closed gilmorej closed 2 years ago

gilmorej commented 4 years ago

I'm having issues when running in Apache. All of the headers are being added before all of the response JSON, and when the JSON is being decoded here https://github.com/alaouy/Youtube/blob/master/src/Youtube.php#L597 The whole thing fails:

    Error: Trying to get property 'items' of non-object

Works using Artisan Tinker, because it is running in CLI mode

    if (isset($_SERVER['HTTP_HOST'])) {
        curl_setopt($tuCurl, CURLOPT_HEADER, array('Referer' => $_SERVER['HTTP_HOST']));
    }

Related commits: https://github.com/alaouy/Youtube/commit/2519161136f9051aba042c41b59d4a94c38239c4#diff-5698570ef0b86d45f1c9c9561e8c1f32 https://github.com/alaouy/Youtube/commit/64ef9d3f265f156b08d306fdd214d1c50b735d58#diff-5698570ef0b86d45f1c9c9561e8c1f32

gilmorej commented 4 years ago

Error from my logs:

[2019-12-17 18:05:39] production.INFO: Unable to unfurl `https://www.youtube.com/watch?v=6kYco2Zt-cM `with Youtube! Error: `Trying to get property 'items' of non-object` --> #0 /var/app/current/vendor/alaouy/youtube/src/Youtube.php(597): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(8, 'Trying to get p...', '/var/app/curren...', 597, Array)
#1 /var/app/current/vendor/alaouy/youtube/src/Youtube.php(156): Alaouy\Youtube\Youtube->decodeSingle('HTTP/1.1 200 OK...')
#2 /var/app/current/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(237): Alaouy\Youtube\Youtube->getVideoInfo('6kYco2Zt-cM')
#3 /var/app/current/app/Service/Embed/Youtube.php(40): Illuminate\Support\Facades\Facade::__callStatic('getVideoInfo', Array)

After adding some logging statements you can see the headers appear:

[2019-12-17 19:09:49] production.INFO: Decoding Response HTTP/1.1 200 OK
Expires: Tue, 17 Dec 2019 19:09:49 GMT
Date: Tue, 17 Dec 2019 19:09:49 GMT
Cache-Control: private, max-age=0, must-revalidate, no-transform
ETag: "p4VTdlkQv3HQeTEaXgvLePAydmU/t272dGTrayS4EPRB_DgiwuI9FF8"
Vary: Origin
Vary: X-Origin
Content-Type: application/json; charset=UTF-8
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block
Content-Length: 4517
Server: GSE
Alt-Svc: quic=":443"; ma=2592000; v="46,43",h3-Q050=":443"; ma=2592000,h3-Q049=":443"; ma=2592000,h3-Q048=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000

{
 "kind": "youtube#videoListResponse",
 "etag": "\"p4VTdlkQv3HQeTEaXgvLePAydmU/t272dGTrayS4EPRB_DgiwuI9FF8\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#video",
   "etag": "\"p4VTdlkQv3HQeTEaXgvLePAydmU/NOOfqGwPLJkB7bfMdRAHR-tIpkQ\"",
   "id": "AuVw4oEMDgA",
   "snippet": {
    "publishedAt": "2019-12-17T13:30:02.000Z",
    "channelId": "UCcCYOO2uYPnG-21WDOWdwew",
    "title": "LG E9 vs Sony AG9 (A9G) 2019 OLED TV Comparison",
    "description": "We compare the picture quality of the LG E9 and Sony AG9 (also marketed as the Bravia A9G) 2019 OLED TVs. The results of this comparison can also be applied to the LG C9, since it uses the same panel and video processor as the E9.\n\nTopics covered include near-black handling, colour accuracy, motion, video processing, HDR and gaming.\n\nThanks to UK electrical retailer Crampton & Moore for sponsoring this video. If you're thinking about buying a new TV, even if it's not the LG E9 or Sony AG9, please support this channel by considering buying from them. Call 0113 244 6607, mention HDTVTest, and you'll receive great price and service:\n\nhttps://www.cramptonandmoore.co.uk/leeds\n\n========================\n**Click Below to SUBSCRIBE for More Reviews, Sneak Peeks & Tips:\nhttps://www.youtube.com/channel/UCcCYOO2uYPnG-21WDOWdwew?sub_confirmation=1\n========================\n\nVISIT HDTV Test\nhttp://www.hdtvtest.co.uk/\n\nFOLLOW US!\nhttps://www.facebook.com/HDTVTest\nhttps://twitter.com/hdtvtest\nhttps://www.instagram.com/hdtvtest\nhttps://twitter.com/vincent_teoh",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/AuVw4oEMDgA/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/AuVw4oEMDgA/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/AuVw4oEMDgA/hqdefault.jpg",
      "width": 480,
      "height": 360
     },
     "standard": {
      "url": "https://i.ytimg.com/vi/AuVw4oEMDgA/sddefault.jpg",
      "width": 640,
      "height": 480
     },
     "maxres": {
      "url": "https://i.ytimg.com/vi/AuVw4oEMDgA/maxresdefault.jpg",
      "width": 1280,
      "height": 720
     }
    },
    "channelTitle": "HDTVTest",
    "tags": [
     "lg e9 vs sony ag9",
     "lg e9 vs sony a9g",
     "lg e9 vs c9",
     "lg c9 vs sony ag9",
     "lg c9 vs sony a9g"
    ],
    "categoryId": "20",
    "liveBroadcastContent": "none",
    "localized": {
     "title": "LG E9 vs Sony AG9 (A9G) 2019 OLED TV Comparison",
     "description": "We compare the picture quality of the LG E9 and Sony AG9 (also marketed as the Bravia A9G) 2019 OLED TVs. The results of this comparison can also be applied to the LG C9, since it uses the same panel and video processor as the E9.\n\nTopics covered include near-black handling, colour accuracy, motion, video processing, HDR and gaming.\n\nThanks to UK electrical retailer Crampton & Moore for sponsoring this video. If you're thinking about buying a new TV, even if it's not the LG E9 or Sony AG9, please support this channel by considering buying from them. Call 0113 244 6607, mention HDTVTest, and you'll receive great price and service:\n\nhttps://www.cramptonandmoore.co.uk/leeds\n\n========================\n**Click Below to SUBSCRIBE for More Reviews, Sneak Peeks & Tips:\nhttps://www.youtube.com/channel/UCcCYOO2uYPnG-21WDOWdwew?sub_confirmation=1\n========================\n\nVISIT HDTV Test\nhttp://www.hdtvtest.co.uk/\n\nFOLLOW US!\nhttps://www.facebook.com/HDTVTest\nhttps://twitter.com/hdtvtest\nhttps://www.instagram.com/hdtvtest\nhttps://twitter.com/vincent_teoh"
    },
    "defaultAudioLanguage": "en"
   },
   "contentDetails": {
    "duration": "PT18M23S",
    "dimension": "2d",
    "definition": "hd",
    "caption": "false",
    "licensedContent": true,
    "projection": "rectangular"
   },
   "status": {
    "uploadStatus": "processed",
    "privacyStatus": "public",
    "license": "youtube",
    "embeddable": true,
    "publicStatsViewable": true
   },
   "statistics": {
    "viewCount": "4112",
    "likeCount": "509",
    "dislikeCount": "5",
    "favoriteCount": "0",
    "commentCount": "172"
   },
   "player": {
    "embedHtml": "\u003ciframe width=\"480\" height=\"270\" src=\"//www.youtube.com/embed/AuVw4oEMDgA\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen\u003e\u003c/iframe\u003e"
   }
  }
 ]
}

This code fails (https://github.com/alaouy/Youtube/blob/master/src/Youtube.php#L597):

$resObj = json_decode($apiData);
$itemsArray = $resObj->items;

When I comment out the latest 2 commits the headers are no longer present. And the code works as it did previously.

[2019-12-17 19:11:18] production.INFO: Decoding Response {
 "kind": "youtube#videoListResponse",
 "etag": "\"p4VTdlkQv3HQeTEaXgvLePAydmU/yHa2NZIzH4Uir5Jwgsu5xUDDHSc\"",
 "pageInfo": {
  "totalResults": 1,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#video",
   "etag": "\"p4VTdlkQv3HQeTEaXgvLePAydmU/NVTY1E4xBIWIPh0irnyxU_fqQIY\"",
   "id": "6kYco2Zt-cM",
   "snippet": {
    "publishedAt": "2018-09-29T06:56:25.000Z",
    "channelId": "UCUjW3ZYLrsnlXWJMnQ4TbGQ",
    "title": "Rage Against Vanessa Carlton",
    "description": "subscribe for more stuff that nobody asked for nor needed",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/6kYco2Zt-cM/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/6kYco2Zt-cM/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/6kYco2Zt-cM/hqdefault.jpg",
      "width": 480,
      "height": 360
     },
     "standard": {
      "url": "https://i.ytimg.com/vi/6kYco2Zt-cM/sddefault.jpg",
      "width": 640,
      "height": 480
     },
     "maxres": {
      "url": "https://i.ytimg.com/vi/6kYco2Zt-cM/maxresdefault.jpg",
      "width": 1280,
      "height": 720
     }
    },
    "channelTitle": "groboclone",
    "tags": [
     "rage against the machine",
     "mashup",
     "killing in the name",
     "vanessa carlton",
     "a thousand miles",
     "killing in the name of",
     "vanessa carlton but",
     "rage against the machine but",
     "ratm",
     "ratm but",
     "neil cicierega",
     "acvc",
     "ac/vc"
    ],
    "categoryId": "10",
    "liveBroadcastContent": "none",
    "localized": {
     "title": "Rage Against Vanessa Carlton",
     "description": "subscribe for more stuff that nobody asked for nor needed"
    }
   },
   "contentDetails": {
    "duration": "PT3M28S",
    "dimension": "2d",
    "definition": "sd",
    "caption": "false",
    "licensedContent": false,
    "projection": "rectangular"
   },
   "status": {
    "uploadStatus": "processed",
    "privacyStatus": "public",
    "license": "youtube",
    "embeddable": true,
    "publicStatsViewable": true
   },
   "statistics": {
    "viewCount": "1228266",
    "likeCount": "43392",
    "dislikeCount": "1023",
    "favoriteCount": "0",
    "commentCount": "2343"
   },
   "player": {
    "embedHtml": "\u003ciframe width=\"480\" height=\"360\" src=\"//www.youtube.com/embed/6kYco2Zt-cM\" frameborder=\"0\" allow=\"accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen\u003e\u003c/iframe\u003e"
   }
  }
 ]
}
alaouy commented 4 years ago

@gilmorej can you provide the versions of Php, curl, apache where you encountered this issue, and any more details to be able to reproduce it. I won't be able to debug this till the weekend, meanwhile any PRs are welcome ;)

gilmorej commented 4 years ago

Amazon Linux 2.8.15

Server version: Apache/2.4.39 (Amazon) Server built: Apr 5 2019 18:55:17

curl 7.61.1 (x86_64-redhat-linux-gnu) libcurl/7.61.1 OpenSSL/1.0.2k zlib/1.2.8 libidn2/0.16 libpsl/0.6.2 (+libicu/50.1.2) libssh2/1.4.2 nghttp2/1.21.1 Release-Date: 2018-09-05 Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp scp sftp smb smbs smtp smtps telnet tftp Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz HTTP2 UnixSockets HTTPS-proxy PSL

PHP 7.2.19 (cli) (built: Jun 12 2019 20:55:29) ( NTS ) Copyright (c) 1997-2018 The PHP Group Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies with Zend OPcache v7.2.19, Copyright (c) 1999-2018, by Zend Technologies

gilmorej commented 4 years ago

I'll just use 2.2.1 in the meantime; no need to immediately address.

Korko commented 4 years ago

The problem is obvisouly here https://github.com/alaouy/Youtube/blob/master/src/Youtube.php#L644 If for an unknown reason, you can't json_decode the apiData, you assume everthing's fine which you shouldn't. You are just missing an exception when $resObj if false.