一、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. prompt() 方法

    定义和用法 prompt() 方法用于显示可提示用户进行输入的对话框. 语法 prompt(text,defaultText) 参数 描述 text 可选.要在对话框中显示的纯文本(而不是 HTML ...

  2. 0066 阿里云大学的几道Java基础测试题

    阿里云大学的几道Java基础测试题: https://edu.aliyun.com/clouder/exam/intro/15 https://edu.aliyun.com/clouder/exam/ ...

  3. tiny4412 u-boot 启动参数的设置

    参考 http://www.cnblogs.com/chenfulin5/p/5887552.html 制作SD卡 u-boot 编译完之后, 进入 u-boot 目录里面的 sd_fuse cd ~ ...

  4. hdu6134 Battlestation Operational 莫比乌斯第一种形式

    /** 题目:hdu6134 Battlestation Operational 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6134 题意:f(n) = ...

  5. crontab中运行python程序出错,提示ImportError: No module named解决全过程

    将一个python脚本放入crontab执行时,提示如下错:ImportError: No module named hashlib但是在shell中直接执行时没有任何问题,google之后,得到线索 ...

  6. MySQL 5.6的72个新特性(译)

    一,安全提高 1.提供保存加密认证信息的方法,使用.mylogin.cnf文件.使用 mysql_config_editor可以创建此文件.这个文件可以进行连接数据库的访问授权. mysql_conf ...

  7. 将php和mysql命令加入到环境变量中

    方法一:直接运行命令export PATH=$PATH:/usr/local/webserver/php/bin 和 export PATH=$PATH:/usr/local/webserver/my ...

  8. python手册

    https://www.crummy.com/software/BeautifulSoup/bs4/doc.zh/

  9. 京东阅读PDF导出

    适用平台:windows 需要软件:FastStone Capture(截图软件),TinyTask(操作录制软件) 1.打开京东阅读 2.设置截图软件 (1)设置截图区域(FastStone Cap ...

  10. linux -- Ubuntu14.04及之后版本重启网卡不生效

    Ubuntu14.04修改配置,重启网卡没有生效,出现如下问题: service networking restart //重启网络服务 stop: Job failed while stopping ...