2012-12-31 94 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