2012-12-31 95 views
4

我想从YouTube获取数据,我正在使用file_get_contents()方法。file_get_contents()与youtube无法正常工作

有时它工作正常,但有时它不工作,并显示以下错误

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 

是什么上述错误是什么意思?

什么是SSL: crypto enabling timeout

+0

你可以在浏览器中打开网址吗? –

+0

是的。它在浏览器中正常显示 –

+1

请阅读我的回答@Rana。 –

回答

5

你不能有时是因为SSL的。使用cURL

这应该足够了:

$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); 
-2

此错误意味着对于PHP页面的执行时间已经走到了尽头。默认情况下,php模块为执行任何php页面设置30秒。你可以使用它来增加它

ini_set('max_execution_time', time in seconds as per your need); 

请在php页面的开头写这个函数,并根据需要设置时间。它会覆盖php为该特定页面设置的默认执行时间。

+0

与OPs错误没有直接关系,只有他没有任何问题。 –

0

您看到的错误是您连接的服务器与系统上的SSL库之间的不兼容性。

请检查您使用的OpenSSL库是否过时。要验证这一点,您可能需要联系您的系统管理员。

如果curl库使用相同的SSL库,David Harris建议的解决方法也不起作用。

或者,您可以尝试显式TLS1,这有时可以工作。我建议先在命令行上进行测试,例如通过使用curl的命令行版本。

另外,您也可以查看https://包装(HTTP onesDocs旁边)使用的the SSL context options in PHPDocs