当前位置: 首页 > news >正文

file_get_contents 与curl 的对比

在讲区别前大家对file_get_contents 只是停留在get 方法其实file_get_contents也可以进行post请求该方法如下

$content = []; 
$options = array('http' => array('method'  => 'POST',// header 需要设置为 JSON'header'  => 'Content-type:application/json','content' => json_encode($content,true),// 超时时间'timeout' => 60));$context = stream_context_create( $options );$result = file_get_contents( $url, false, $context );*

curl 的封装方法 post get form-post

/*** @param $url* @param $headers* @param $data* @return bool|string* curl-post*/function POST($url,$headers,$data){$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl,CURLOPT_HEADER,0);curl_setopt($curl, CURLOPT_NOBODY, 0);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_ENCODING, "gzip");curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);curl_setopt($curl, CURLOPT_POST, 1);curl_setopt($curl, CURLOPT_POSTFIELDS, $data);$html = curl_exec($curl);curl_close($curl);return $html;}/*** @param $url* @param $headers* @param $data* @return bool|string* curl-get*/function GET($url,$headers,$data){$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl,CURLOPT_HEADER,0);curl_setopt($curl, CURLOPT_NOBODY, 0);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);curl_setopt($curl, CURLOPT_ENCODING, "gzip");curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
//        curl_setopt($curl, CURLOPT_POST, 1);curl_setopt($curl, CURLOPT_POSTFIELDS, $data);$html = curl_exec($curl);curl_close($curl);return $html;}protected function curlForm($url, $post = '', $header = ''){$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);if (!empty($header)) {curl_setopt($ch,CURLOPT_HTTPHEADER,$header);}curl_setopt($ch, CURLOPT_FAILONERROR, false);curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);//读取超时curl_setopt($ch, CURLOPT_TIMEOUT, 30);//连接超时curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);//https 请求if(strlen($url) > 5 && strtolower(substr($url,0,5)) == "https" ) {curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);}if (!empty($post)){//设置post数据curl_setopt ( $ch, CURLOPT_POST, true );curl_setopt ( $ch, CURLOPT_POSTFIELDS, http_build_query($post));}$response = curl_exec($ch);if (curl_errno($ch)) {throw new Exception(curl_error($ch),0);} else {$httpStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);if (200 !== $httpStatusCode && 201 !== $httpStatusCode){throw new Exception($response,$httpStatusCode);}}curl_close($ch);return $response;}
这二者简单封装后 使用上有什么区别?

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

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

3. file_get_contents 函数会受到php.ini文件中allow_url_open选项配置的影响。如果该配置关闭了,则该函数也就失效了。而curl不受该配置的影响。

4. file_get_contents处理频繁小的时候,用它感觉挺好的。没什么异常。如果你的文件被1k+人处理。那么你的服务器cpu就等着高升吧。所以建议自己和大家在以后写php代码的时候使用curl库。

http://www.lryc.cn/news/193287.html

相关文章:

  • 两个el-date-picker进行互相关联
  • python openai playground使用教程
  • DOCKER本地仓库
  • python写着玩
  • K8s Kubernetes Namespave Pod Label Deployment Service 实战
  • SpringBoot使用随机端口启动
  • NewStarCTF2023week2-ez_sql
  • 力扣-434.字符串中的单词数
  • 【ALO-BP预测】基于蚁狮算法优化BP神经网络回归预测研究(Matlab代码实现)
  • 分布式存储系统Ceph应用详解
  • 人工智能轨道交通行业周刊-第63期(2023.10.9-10.15)
  • OJ项目——统一数据格式返回,我是如何处理的?
  • Open CV 3D Python 环境搭建
  • C#中lock 和 ReaderWriterLock 的使用总结
  • Mac下通过nvm管理node
  • 易点易动固定资产管理系统:RFID出入监控,保障固定资产安全
  • Vue封装组件并发布到npm仓库
  • python+深度学习+opencv实现植物识别算法系统 计算机竞赛
  • 基于springboot实现医院急诊平台系统项目【项目源码】
  • 【02】基础知识:React - jsx语法规则
  • C语言 —— 指针
  • 淘宝店铺所有商品数据接口,淘宝整店所有商品数据接口,淘宝店铺商品接口,淘宝API接口
  • 【Redis】Java客户端使用zset命令
  • 记录一个@Transaction注解引发的bug
  • 解决docker使用pandarallel报错OSError: [Errno 28] No space left on device
  • Javascript自定义页面复制事件
  • Nginx:反向代理(示意图+配置)
  • macbook笔记本电脑内存怎么清理才能干净流畅?
  • spark 与 mapreduce 对比
  • kafka 相关概念