Home  >  Article  >  Backend Development  >  php中file_get_content 和curl以及fopen 效率分析_php技巧

php中file_get_content 和curl以及fopen 效率分析_php技巧

WBOY
WBOYOriginal
2016-05-16 20:35:561107browse

三个函数虽然都是读取资源的函数,但各自的应用场景不同。

curl多用于互联网网页之间的抓取,fopen多用于读取文件,而file_get_contents多用于获取静态页面的内容。

1. fopen /file_get_contents 每次请求都会重新做DNS查询,并不对DNS信息进行缓存。但是CURL会自动对DNS信息进行缓存。对同一域名下的网页或者图片的请求只需要一次DNS查询。这大大减少了DNS查询的次数。所以CURL的性能比fopen /file_get_contents 好很多。

2. fopen /file_get_contents在请求HTTP时,使用的是http_fopen_wrapper,不会keeplive。而curl却可以。这样在多次请求多个链接时,curl效率会好一些。

3. curl可以模拟多种请求,例如:POST数据,表单提交等,用户可以按照自己的需求来定制请求。而fopen / file_get_contents只能使用get方式获取数据。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn