2012-01-03 17 views
4

我正在製作一個推送通知服務器,用於從外部(第三方)html頁面收集特定數據,如果我知道我需要的信息是在第一個例子中。 5000個字符,如果我聲明MAX_LENGTH,PHP實際上會使用更少的內存嗎?或者整個頁面完全加載到內存中?此外,是否下載了整個html頁面,或者一旦達到限制,就會斷開連接? (並進而節省數據傳輸成本)是PHP的file_get_contents內存和數據有效率?

$html = file_get_contents ("http://.....", false, null, -1, 5000); 

謝謝。

+1

不是一個答案,但你可能會發現這個有趣的:http://stackoverflow.com/questions/555523/file-get-contents-vs-curl-what-has-better - 性能 – 2012-01-03 15:09:35

+0

謝謝,我認爲,說服我切換到捲曲 – 2012-01-03 22:17:06

回答

2

是的,它確實節省了內存和帶寬......我也跑了速度測試(這與這個問題並不完全密切相關,但是很有用,並且表明它確實停止讀取數據流)以及一個內存測試演示。我沒有運行峯值內存測試,但至少您的$ html變量將存儲更少的信息並在那裏節省內存。

Time to get ALL characters of remote page 10 times: 6.0368211269379 
Time to get ALL characters of remote page 10 times: 6.0158920288086 
Time to get ALL characters of remote page 10 times: 5.8945140838623 
Time to get ALL characters of remote page 10 times: 8.867082118988 
Time to get ALL characters of remote page 10 times: 5.7686760425568 

Time to get first ten characters of page 10 times: 4.2118229866028 
Time to get first ten characters of page 10 times: 4.5816869735718 
Time to get first ten characters of page 10 times: 4.2146580219269 
Time to get first ten characters of page 10 times: 4.1949119567871 
Time to get first ten characters of page 10 times: 4.1788749694824 

Memory Useage First 10 characters:40048 
Memory Useage ALL characters:101064 
2

通過源頭開始here,下面來here,終於在_php_stream_copy_to_mem函數結束了挖,它看起來像file_get_contents()功能將真正停止閱讀流一旦達到所要求的最大長度。

2

是的,因爲它在引擎蓋下使用流函數,它會在達到極限時實際停止。在文檔頁面上,它說

「file_get_contents()是將文件內容讀入字符串的首選方式,如果操作系統支持,它將使用內存映射技術來提高性能。

所以它實際上應該給你提供你尋找的。