I am trying to display youtube videos on a website using API. In order to fetch data from youtube I am using file_get_contents and to decode the received data, I am using json_decode() but none of the functions are working. When I write the following function:
file_get_contents('https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId='.$channelID.'&maxResults='.$maxResults.'&key='.$API_key.'')
I get 500 error. Why am I getting internal server error?
And, in the error log file I am getting this
[2020-01-24 12:32:20] production.ERROR: file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0 (View: /home/project/project_laravel/resources/views/index.blade.php) {"exception":"[object] (ErrorException(code: 0): file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0 (View: /home/project/project_laravel/resources/views/index.blade.php) at /home/project/project_laravel/storage/framework/views/2ef7c353b55ce1b10f018d5d4c745d8de5c15831.php:91, ErrorException(code: 0): file_get_contents(): https:// wrapper is disabled in the server configuration by allow_url_fopen=0 at /home/project/project_laravel/storage/framework/views/2ef7c353b55ce1b10f018d5d4c745d8de5c15831.php:91) [stacktrace]
During the research I have tried this answer on StackOverflow Warning: file_get_contents(): https:// wrapper is disabled in the server configuration by all where the user has suggested us to enable allow_url_fopen, but, there is no allow_url_fopen=0 in my php.ini. And even after adding
allow_url_fopen = 1
and
allow_url_include = 1
I still get this error.
Enabling allow_url_fopen in php.ini according to this answer Warning: file_get_contents(): https:// wrapper is disabled in the server configuration by all did not helped me.
When I searched solution for this question again I found this article Enabling allow_url_fopen
However, since I am using a shared hosting, the support team said to me that this function in php posses security risk, and they do not enable this function in shared server environment and it could only be enabled at my own risk when I am running on vps servers.
So in order to fetch YouTube data via API I had to use cURL function and it worked for me.