一、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. java 高精度 四则运算

    java的大数处理对于ACM中的大数来说,相当的简单啊: 整数的运算   BigInteger 小数的运算   BigDecimal 导入类: import java.util.Scanner; im ...

  2. Eclipse上Hadoop插件中Run On Hadoop原理[转]

    通过Eclipse的hadoop插件中的"run on hadoop"命令的原理:它不是把jar包发送到hadoop集群上去运行,而只是使用了hadoop集群上的hdfs,从hdf ...

  3. [转]James Bach:测试人员的角色

    [转]James Bach:测试人员的角色 2015-05-13 以前,我是个开发人员.我不喜欢这个工作,无尽的压力让我疲惫.我几乎从未感觉到自己的工作做得足够好.我从未有过真正的休息.如果我没做好, ...

  4. shiro身份认证

    pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...

  5. sql语句判断是否为数字、字母、中文

    1. sql语句判断是否为数字.字母.中文 select ascii(字段) 数字:48-57字母:65-123汉字:123+ 如,要删除某个全为数字的字段 DELETE FROM table  WH ...

  6. dbcp 详细配置

    1.配置参数 username : 连接用户名 password:  连接密码 url :  连接 url( 如果连接 mysql ,格式为 jdbc:mysql://ip:port/dbname) ...

  7. dp之多重背包2191

    水题........ #include<iostream> #include<stdio.h> #include<string.h> using namespace ...

  8. Delphi之Raise抛出异常

    相关资料: http://blog.csdn.net/a20071426/article/details/10160171 实例代码: unit Unit1; interface uses Windo ...

  9. Genymotion常见问题整合与解决方案(转)

    常见问题1:Genymotion在开启模拟器时卡在了starting virtual device(注意只有tarting virtual device窗口,没有模拟器的黑屏窗口)    原因:Vir ...

  10. Dom监听组合按键

    JS监听组合按键   有些时候,我们需要在网页上,增加一些快捷按键,方便用户使用一些常用的操作,比如:保存,撤销,复制.粘贴等等. 下面简单梳理一下思路: 我们所熟悉的按键有这么集中类型: 单独的按键 ...