一、file_get_contents

1.定义

file_get_contents() 函数将指定 URL 的文件读入一个字符串并返回。

2.语法

file_get_contents(path, include_path, context, start, max_length)
  • path:要读取的路径或链接。
  • include_path:是否在路径中搜索文件,搜索则设为 1,默认为 false。
  • context:修改流的行为,如超时时间,GET / POST 等。
  • start:开始读文件的位置。
  • max_length:读取文件的字节数。

3.示例

test.txt

<?php
echo "i'm a test php";
?>

index.php

<?php
$testTxt = file_get_contents('./test.txt');
var_dump($testTxt); // string(15) "i'm a test txt." $ctx = stream_context_create(
array(
'http' => array(
'method' => 'get',
'timeout' => 30
)
)
);
$testTxt = file_get_contents('./test.txt', false, $ctx, 4, 6);
var_dump($testTxt); // string(6) "a test"
?>

二、curl

1.定义

PHP 支持 Daniel Stenberg 创建的 libcurl 库,能够连接通讯各种服务器、使用各种协议。libcurl 目前支持的协议有 http、https、ftp、gopher、telnet、dict、file、ldap。 libcurl 同时支持 HTTPS 证书、HTTP POST、HTTP PUT、 FTP 上传(也能通过 PHP 的 FTP 扩展完成)、HTTP 基于表单的上传、代理、cookies、用户名+密码的认证。

2.语法

  1. curl_init:初始化 cURL 会话。
  2. curl_setopt:设置 cURL 传输选项。
  3. curl_exec:返回 true / false,curl_setopt 设置 CURLOPT_RETURNTRANSFER 为 TRUE 时将 curl_exec() 获取的信息以字符串返回。
  4. curl_close:关闭 cURL 会话。

3.示例

test.php

<?php
echo "i'm a test php";
?>

index.php

<?php
// 创建一个新 cURL 资源
$ch = curl_init(); // 设置URL和相应的选项
curl_setopt($ch, CURLOPT_URL, "http://localhost/test.php"); // 需要获取的 URL 地址,也可以在 curl_init() 初始化会话的时候。
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_HEADER, false); // 启用时会将头文件的信息作为数据流输出。
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); // 在尝试连接时等待的秒数。设置为 0,则无限等待。
curl_setopt($ch, CURLOPT_TIMEOUT, 10); // 允许 cURL 函数执行的最长秒数。
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // TRUE 将 curl_exec() 获取的信息以字符串返回,而不是直接输出。 // 抓取 URL 并把它传递给浏览器
$ret = curl_exec($ch); var_dump($ret); // 关闭 cURL 资源,并且释放系统资源
curl_close($ch);
?>

三、file_get_contents 和 curl 区别

1.curl 支持更多功能

curl 支持更多协议,有http、https、ftp、gopher、telnet、dict、file、ldap;模拟 Cookie 登录,爬取网页;FTP 上传下载。

fopen / file_get_contents 只能使用 GET 方式获取数据。

2.性能

curl 可以进行 DNS 缓存,同一个域名下的图片或其它资源只需要进行一次DNS查询。

curl 相对来说更加快速稳定,访问量高的时候首选 curl,缺点就是相对于 file_get_contents 配置繁琐一点,file_get_contents 适用与处理小访问的应用。

PHP file_get_contents和curl区别

PHP file_get_contents和curl区别的更多相关文章

  1. nginx+fastcgi php 使用file_get_contents、curl、fopen读取localhost本站点.php异常的情况

    原文:http://www.oicto.com/nginx_fastcgi_php_file_get_contents/ 参考:http://os.51cto.com/art/201408/44920 ...

  2. php执行与curl区别

            如执行一个文件写入 Linux服务器,分别php **/a.php与 curl http://**/a.php 结果:php执行写入到/root/test.txt, curl与浏览器运 ...

  3. 使用file_get_contents()和curl()抓取网络资源的效率对比

    使用file_get_contents()和curl()抓取网络资源的效率对比 在将小程序用户头像合成海报的时候,用到了抓取用户头像对应的网络资源,那么抓取方式有很多,比如 file_get_cont ...

  4. PHP fopen/file_get_contents与curl性能比较

    PHP中fopen,file_get_contents,curl 函数的区别: 1.fopen/file_get_contents 每次请求都会重新做 DNS 查询,并不对 DNS 信息进行缓存. 但 ...

  5. php获得远程信息到本地使用的3个函数:file_get_contents和curl函数和stream_get_contents

    1:file_get_contents echo file_get_contents("http://www.php.com/index.php");   2:curl funct ...

  6. php file_get_contents与curl性能比较

    1.fopen /file_get_contents 每次请求都会重新做DNS查询,并不对 DNS信息进行缓存.但是CURL会自动对DNS信息进行缓存.对同一域名下的网页或者图片的请求只需要一次DNS ...

  7. PHP file_get_contents于curl性能效率比较

    说明大部分内容整理来源于网络,期待你的补充.及不当之处的纠正: 1)fopen/file_get_contents 每次请求远程URL中的数据都会重新做DNS查询,并不对DNS信息进行缓存.但是CUR ...

  8. file_get_contents和curl对于post方式的解决办法

    post方式解决办法 其实很简单,我们只要仔细看看就知道了... file_get_contents: $content=$_POST['content'];$access_token=$_POST[ ...

  9. php使用file_get_contents 或者curl 发送get/post 请求 的方法总结

    file_get_contents模拟GET/POST请求 模拟GET请求: <?php $data = array( 'name'=>'zhezhao', 'age'=>'23' ...

随机推荐

  1. 每日英语:Asia Has World's Biggest Pay Gap, Study Finds

    In Asia, middle managers such as department heads make more than 14 times as much as operational emp ...

  2. You-Get 视频下载工具 Python命令行下载工具

    You-Get 是一个命令行工具, 用来下载各大视频网站的视频, 是我目前知道的命令行下载工具中最好的一个, 之前使用过 youtube-dl, 但是 youtube-dl 吧, 下载好的视频是分段的 ...

  3. RP2837 IN1-IN2 对应关系 2路DI

    RP2837 IN1-IN2 对应关系: IN1   ARM-IO2   PA16 IN2   ARM-IO6   PA4 root@sama5d3-linux:~ echo 16  > /sy ...

  4. signal基础

    signal man 7 signal 1.kill -l 显示所有信号 kill -signal PID killall -signal name 2.产生信号 ctrl+c => SIGIN ...

  5. keepalive脑裂的处理,从节点发现访问的虚拟IP就报警,同时尝试发送内容到主节点服务器关闭keepalive和nginx,或者关机

    解决keepalived脑裂问题   检测思路:正常情况下keepalived的VIP地址是在主节点上的,如果在从节点发现了VIP,就设置报警信息 脚本如下: 1 2 3 4 5 6 7 8 9 10 ...

  6. Spring MVC学习之三:处理方法返回值的可选类型

    http://flyer2010.iteye.com/blog/1294400 ———————————————————————————————————————————————————————————— ...

  7. word调整技巧

    a4纸如何留白: 

  8. The Properties of Posterior of Topic Model

    1.Tang, Jian, et al. "Understanding the Limiting Factors of Topic Modeling via Posterior Contra ...

  9. $-------JSP中表达式语言的$特殊字符的作用

    JSP 中EL表达式用法详解 EL 全名为Expression Language EL 语法很简单,它最大的特点就是使用上很方便.接下来介绍EL主要的语法结构: ${sessionScope.user ...

  10. 纪念伟大的sb错-noip滚粗

    人弱就是弱,被sb错虐翻. 手一抖一生就毁了 开此博文纪念这个伟大的sb错! noip2014 d2t2逆bfs后删点手残没考虑后效性,完美爆80 愿省选rp++,求进noi,orz