php中请求url的五种方法总结

本文主要给大家介绍了关于PHP中请求url的五种方法分享出来供大家参考学习,下面话不多说,来一起看看详细的介绍:

五种方法

  • 前三种都是PHP基本的文件操作函数
  • curl()PHP扩展需要开启,linux下需要安装
  • exec()执行的是linux命令行下的命令wget下载远程文件

其中wget命令在本地虚机测试请求http://www.baidu.com时,没有成功,在远程服务器上却可以,考虑时DNS解析的问题,于是直接请求IP成功下载了index.html的文件

这里只提供了方法,其中的优缺点需要详细了解每一个方法功能和缺陷。

一、fopen()函数

rush:PHP;"> $file = fopen("//www.jb51.cc","r") or die("打开远程文件失败!"); while (!feof($file)) { $line = fgets($file,1024); //使用正则匹配标题标记 if (preg_match("/(.*)<\/title>/i",$line,$out)) { $title = $out[1]; //将<a href="https://www.jb51.cc/tag/biaoti/" target="_blank" class="keywords">标题</a><a href="https://www.jb51.cc/tag/biaoji/" target="_blank" class="keywords">标记</a>中的<a href="https://www.jb51.cc/tag/biaoti/" target="_blank" class="keywords">标题</a>字符取出 break; //<a href="https://www.jb51.cc/tag/tuichu/" target="_blank" class="keywords">退出</a>循环,结束远程<a href="https://www.jb51.cc/tag/wenjian/" target="_blank" class="keywords">文件</a>读取 } } fclose($file);</pre> </div> <p><h3>二、file()<a href="https://www.jb51.cc/tag/hanshu/" target="_blank" class="keywords">函数</a></h3></p> <div class="jb51code"> <pre class="b<a href="https://www.jb51.cc/tag/rush/" target="_blank" class="keywords">rush</a>:<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>;"> $li<a href="https://www.jb51.cc/tag/nes/" target="_blank" class="keywords">nes</a> = file("//www.jb51.cc/article/48866.htm"); readfile(<a href="//www.jb51.cc/article/48866.htm">//www.jb51.cc/article/48866.htm</a>);</pre> </div> <p><h3>三、file_get_contents()函数</h3></p> <div class="jb51code"> <pre class="brush:php;"> $content = file_get_contents(<a href="//www.jb51.cc/article/48866.htm">//www.jb51.cc/article/48866.htm</a>);</pre> </div> <p><h3>四、curl() 请求远程url数据</h3></p> <div class="jb51code"> <pre class="b<a href="https://www.jb51.cc/tag/rush/" target="_blank" class="keywords">rush</a>:<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>;"> $url = "http://www.baidu.com"; $ch = curl_init(); $timeout = 5; curl_s<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>pt($ch,CURLOPT_URL,$url); curl_s<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>pt($ch,CURLOPT_RETURNTRANSFER,1); curl_s<a href="https://www.jb51.cc/tag/eto/" target="_blank" class="keywords">eto</a>pt($ch,CURLOPT_CONNECTTIMEOUT,$timeout); $contents = curl_exec($ch); curl_close($ch);</pre> </div> <p><h3>五、exec() 执行命令行命令</h3></p> <div class="jb51code"> <pre class="b<a href="https://www.jb51.cc/tag/rush/" target="_blank" class="keywords">rush</a>:<a href="https://www.jb51.cc/tag/PHP/" target="_blank" class="keywords">PHP</a>;"> //exec("wget 220.181.111.188"); shell_exec("wget 220.181.111.188");</pre> </div> <p><h3>总结</h3></p> <p>以上就是这篇<a href="https://www.jb51.cc/tag/wenzhang/" target="_blank" class="keywords">文章</a>的全部<a href="https://www.jb51.cc/tag/neirong/" target="_blank" class="keywords">内容</a>了,希望本文的<a href="https://www.jb51.cc/tag/neirong/" target="_blank" class="keywords">内容</a>对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对编程之家的<a href="https://www.jb51.cc/tag/zhichi/" target="_blank" class="keywords">支持</a>。</p><div class="topcard-tags"><a href="https://m.jb51.cc/tag/phpfasongurlqingqiu/" class="tag_link" target="_blank">php发送url请求</a><a href="https://m.jb51.cc/tag/huoquqingqiuurl/" class="tag_link" target="_blank">获取请求url</a><a href="https://m.jb51.cc/tag/qingqiuurl/" class="tag_link" target="_blank">请求url</a></div> </div> </div> </div> <!-- row end--> <div class="clearfix"></div> <!-- row --> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <div class="title"><h1>相关文章</h1></div><div class="list_con"> <div class="title"> <a href="https://m.jb51.cc/php/4739043.html" title="vue+thinkphp5实现微信扫码支付(NATIVE支付)">vue+thinkphp5实现微信扫码支付(NATIVE支付)</a> </div> <div class="summary">统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...</div> </div><div class="list_con"> <img src="https://m.jb51.cc/res/2024/07-30/10/32d06eca1830b39e35547ff639b7b95b.png" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/php/4739042.html" title="uniapp+thinkphp5实现微信扫码支付(APP支付)">uniapp+thinkphp5实现微信扫码支付(APP支付)</a> </div> <div class="summary">统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...</div> </div><div class="list_con"> <img src="https://m.jb51.cc/res/2024/07-30/10/3d907355e295604aef536bba5b45dd7d.png" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/php/4739041.html" title="uniapp+thinkphp5实现微信登录">uniapp+thinkphp5实现微信登录</a> </div> <div class="summary">前言 之前做了微信登录,所以总结一下微信授权登录并获取用户...</div> </div><div class="list_con"> <img src="https://m.jb51.cc/res/2024/07-30/10/fc29e4e4eba385bc042444571f518ef6.png" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/php/4739040.html" title="【PHP】关于fastadmin框架中使用with进行连表查询时setEagerlyType字段的理解">【PHP】关于fastadmin框架中使用with进行连表查询时setEagerlyType字段的理解</a> </div> <div class="summary">FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一...</div> </div><div class="list_con"> <img src="https://m.jb51.cc/res/2024/07-30/10/e6971fb86d3ee30a4466e87826da43d8.png" width="100" height="64" style="float:right;margin-left:20px;" /> <div class="title"> <a href="https://m.jb51.cc/php/4739039.html" title="【workerman】uniapp+thinkPHP5使用GatewayWorker实现实时通讯">【workerman】uniapp+thinkPHP5使用GatewayWorker实现实时通讯</a> </div> <div class="summary">之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛...</div> </div><div class="list_con"> <div class="title"> <a href="https://m.jb51.cc/php/4739038.html" title="uniapp+thinkphp5实现微信支付(JSAPI支付)">uniapp+thinkphp5实现微信支付(JSAPI支付)</a> </div> <div class="summary">统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返...</div> </div></div> </div> <!-- row end--> </div> </div> <footer id="footer"> <div class="container"> <div class="copyright"> Copyright © 2018 编程之家. 当前版本 V7.0.16<br> <span class="ml5">编程之家 版权所有 <a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">闽ICP备13020303号-8</a> </div> </div> </footer> <script> (function () { var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(bp, s); })(); </script> <script src="https://m.jb51.cc/js/count.js"></script> </body> </html>