Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

file_get_contents() is not working properly with youtube

Tags:

php

ssl

I want to fetch data from youtube and I am using file_get_contents() method.

sometime it works fine but sometime it do not works and shows the following error

file_get_contents() [function.file-get-contents]: SSL: crypto enabling timeout
file_get_contents() [function.file-get-contents]: Failed to enable crypto
file_get_contents(https://gdata.youtube.com/feeds/api/users/default?access_token=token&v=2&alt=json) [function.file-get-contents]: failed to open stream: operation failed
Maximum execution time of 30 seconds exceeded

What does the above errors mean?

What is SSL: crypto enabling timeout

like image 211
Muhammad Usman Avatar asked Oct 21 '22 20:10

Muhammad Usman


1 Answers

You can't sometimes, because of SSL. Use cURL

This should suffice:

$ch = curl_init('https://youtube.com');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux i686; rv:20.0) Gecko/20121230 Firefox/20.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
echo curl_exec($ch);
like image 95
David Harris Avatar answered Oct 24 '22 17:10

David Harris