微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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><ins class="adsbygoogle" style="display:inline-block;width:780px;height:90px" data-ad-client="ca-pub-4605373693034661" data-ad-slot="4286417769"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script><p class="small text-muted">版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。</p> </div><div class="topcard-tags"><a href="https://www.jb51.cc/tag/phpfasongurlqingqiu/" class="tag_link" target="_blank">php发送url请求</a><a href="https://www.jb51.cc/tag/huoquqingqiuurl/" class="tag_link" target="_blank">获取请求url</a><a href="https://www.jb51.cc/tag/qingqiuurl/" class="tag_link" target="_blank">请求url</a></div><ul class="list-group"> <li class="list-group-item"><a href="https://www.jb51.cc/php/17155.html" title="php获取'/'传参的值简单方法">上一篇:php获取'/'传参的值简单方</a><a href="https://www.jb51.cc/php/17152.html" title="PHP基于socket实现客户端和服务端通讯功能" class="text-muted pull-right">下一篇:PHP基于socket实现客户端和服务端通</a> </li> </ul> </div> </div> </div> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <h4>相关推荐</h4> <hr /> <div class="list_con"> <a href="https://www.jb51.cc/php/4739043.html" title="vue+thinkphp5实现微信扫码支付(NATIVE支付)"><div class="title">vue+thinkphp5实现微信扫码支付(NATIVE支付)</div> <div class="summary">统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返回预支付订单号的接口,目前微信支付所有场景均使用这一接口。下面介绍的是其中NATIVE的支付实现流程与PC端实现扫码支付流程</div> <time class="summary">作者:我恨bug 时间:2024-07-30</time> </a> </div><div class="list_con"> <a href="https://www.jb51.cc/php/4739042.html" title="uniapp+thinkphp5实现微信扫码支付(APP支付)"><img class="lazy" src="https://www.jb51.cc/images/np.jpg" data-original="https://www.jb51.cc/res/2024/07-30/10/32d06eca1830b39e35547ff639b7b95b.png" title="" width="160" height="90" style="float:right;margin-left:30px;" /><div class="title">uniapp+thinkphp5实现微信扫码支付(APP支付)</div> <div class="summary">统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返回预支付订单号的接口,目前微信支付所有场景均使用这一接口。下面介绍的是其中APP的支付的配置与实现流程</div> <time class="summary">作者:我恨bug 时间:2024-07-30</time> </a> </div><div class="list_con"> <a href="https://www.jb51.cc/php/4739041.html" title="uniapp+thinkphp5实现微信登录"><img class="lazy" src="https://www.jb51.cc/images/np.jpg" data-original="https://www.jb51.cc/res/2024/07-30/10/3d907355e295604aef536bba5b45dd7d.png" title="" width="160" height="90" style="float:right;margin-left:30px;" /><div class="title">uniapp+thinkphp5实现微信登录</div> <div class="summary">前言 之前做了微信登录,所以总结一下微信授权登录并获取用户信息这个功能的开发流程。 配置 1.首先得在微信公众平台申请一下微信小程序账号并获取到小程序的AppID和AppSecret https://mp.weixin.qq.com/cgi-bin/loginpage?url=%2Fwxamp%2F</div> <time class="summary">作者:我恨bug 时间:2024-07-30</time> </a> </div><div class="list_con"> <a href="https://www.jb51.cc/php/4739040.html" title="【PHP】关于fastadmin框架中使用with进行连表查询时setEagerlyType字段的理解"><img class="lazy" src="https://www.jb51.cc/images/np.jpg" data-original="https://www.jb51.cc/res/2024/07-30/10/fc29e4e4eba385bc042444571f518ef6.png" title="" width="160" height="90" style="float:right;margin-left:30px;" /><div class="title">【PHP】关于fastadmin框架中使用with进行连表查询时setEagerlyType字段的理解</div> <div class="summary">FastAdmin是我第一个接触的后台管理系统框架。FastAdmin是一款开源且免费商用的后台开发框架,它基于ThinkPHP和Bootstrap两大主流技术构建的极速后台开发框架,它有着非常完善且强大的功能和便捷的开发体验,使我逐渐喜欢上了它。</div> <time class="summary">作者:我恨bug 时间:2024-07-30</time> </a> </div><div class="list_con"> <a href="https://www.jb51.cc/php/4739039.html" title="【workerman】uniapp+thinkPHP5使用GatewayWorker实现实时通讯"><img class="lazy" src="https://www.jb51.cc/images/np.jpg" data-original="https://www.jb51.cc/res/2024/07-30/10/e6971fb86d3ee30a4466e87826da43d8.png" title="" width="160" height="90" style="float:right;margin-left:30px;" /><div class="title">【workerman】uniapp+thinkPHP5使用GatewayWorker实现实时通讯</div> <div class="summary">之前公司需要一个内部的通讯软件,就叫我做一个。通讯软件嘛,就离不开通讯了,然后我就想到了长连接。这里本人用的是GatewayWorker框架。</div> <time class="summary">作者:我恨bug 时间:2024-07-30</time> </a> </div><div class="list_con"> <a href="https://www.jb51.cc/php/4739038.html" title="uniapp+thinkphp5实现微信支付(JSAPI支付)"><div class="title">uniapp+thinkphp5实现微信支付(JSAPI支付)</div> <div class="summary">统一支付是JSAPI/NATIVE/APP各种支付场景下生成支付订单,返回预支付订单号的接口,目前微信支付所有场景均使用这一接口。下面介绍的是其中JSAPI的支付实现流程</div> <time class="summary">作者:我恨bug 时间:2024-07-30</time> </a> </div><div class="list_con"> <a href="https://www.jb51.cc/php/4676831.html" title="服务器优化必备:深入了解PHP8底层开发原理"><img class="lazy" src="https://www.jb51.cc/images/np.jpg" data-original="https://www.jb51.cc/res/2023/09-11/01/a3322fb8f7853a71e573cffcd7960431.jpg" title="" width="160" height="90" style="float:right;margin-left:30px;" /><div class="title">服务器优化必备:深入了解PHP8底层开发原理</div> <div class="summary">服务器优化必备:深入了解PHP8底层开发原理</div> <time class="summary">作者:编程之家 时间:2023-09-11</time> </a> </div><div class="list_con"> <a href="https://www.jb51.cc/php/4676830.html" title="Golang的网络编程:如何快速构建高性能的网络应用?"><img class="lazy" src="https://www.jb51.cc/images/np.jpg" data-original="https://www.jb51.cc/res/2023/09-11/01/891786da89034c5ca51a30e1cd0b54ec.jpg" title="" width="160" height="90" style="float:right;margin-left:30px;" /><div class="title">Golang的网络编程:如何快速构建高性能的网络应用?</div> <div class="summary">Golang的网络编程:如何快速构建高性能的网络应用?</div> <time class="summary">作者:编程之家 时间:2023-09-11</time> </a> </div><div class="list_con"> <a href="https://www.jb51.cc/php/4676829.html" title="Golang和其他编程语言的对比:为什么它的开发效率更高?"><img class="lazy" src="https://www.jb51.cc/images/np.jpg" data-original="https://www.jb51.cc/res/2023/09-11/01/b4ba3ff14a96ec8962f292ee854b63d9.jpg" title="" width="160" height="90" style="float:right;margin-left:30px;" /><div class="title">Golang和其他编程语言的对比:为什么它的开发效率更高?</div> <div class="summary">Golang和其他编程语言的对比:为什么它的开发效率更高?</div> <time class="summary">作者:编程之家 时间:2023-09-11</time> </a> </div><div class="list_con"> <a href="https://www.jb51.cc/php/4676828.html" title="PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用"><img class="lazy" src="https://www.jb51.cc/images/np.jpg" data-original="https://www.jb51.cc/res/2023/09-11/01/6932d37a1c1dfaec001b928e40ac7216.jpg" title="" width="160" height="90" style="float:right;margin-left:30px;" /><div class="title">PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用</div> <div class="summary">PHP8底层开发原理揭秘:如何利用新特性创建出色的Web应用</div> <time class="summary">作者:编程之家 时间:2023-09-11</time> </a> </div></div> </div> </div> </div> <div class="col-sm-12 col-md-12 col-lg-3"> <!-- row --> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <!-- jb51-article-300x600 --> <ins class="adsbygoogle" style="display:inline-block;width:300px;height:600px" data-ad-client="ca-pub-4605373693034661" data-ad-slot="7541177540"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script> </div> </div> </div> <!-- row end --> <!-- row --> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card" style="padding:20px;"> <label class="main-content-label ">小编推荐</label><div class="entry-img"><img src="https://www.jb51.cc/res/2023/06-29/12/fa513c17761076a6649593e2bd17f268.jpg" height="150" width="100%"><div class="entry-wrap"><a href="https://www.jb51.cc/mp/4606010.html" title="苹果市值2025年有望达4万亿美元" >苹果市值2025年有望达4万亿美元</a> </div></div><ul class="n-list"><li><a href="https://www.jb51.cc/mp/4742234.html" title="微信青少年模式全新升级:视频号内容分级与家长控制功能上线!" >• 微信青少年模式全新升级:视频号内容分</a></li><li><a href="https://www.jb51.cc/mp/4742229.html" title="《地下城与勇士DNF》永久交通卡卡面上线华为钱包!" >• 《地下城与勇士DNF》永久交通卡卡面上线</a></li><li><a href="https://www.jb51.cc/mp/4742227.html" title="优酷为方便长辈看视频,上线银发剧场!" >• 优酷为方便长辈看视频,上线银发剧场!</a></li><li><a href="https://www.jb51.cc/mp/4742224.html" title="微信读书集成腾讯混元AI技术,可以一日阅读千页?" >• 微信读书集成腾讯混元AI技术,可以一日</a></li><li><a href="https://www.jb51.cc/mp/4742221.html" title="京东快递上线11项会员权益" >• 京东快递上线11项会员权益</a></li><li><a href="https://www.jb51.cc/mp/4742219.html" title="高德地图功能升级,新增安全预警" >• 高德地图功能升级,新增安全预警</a></li><li><a href="https://www.jb51.cc/mp/4742214.html" title="小爱同学多模态能力升级,引领智能生活新潮流" >• 小爱同学多模态能力升级,引领智能生活</a></li></ul> </div> </div> </div> <!-- row end --> <div class="row row-sm"> <div class="col-sm-12 col-md-12 col-lg-12"> <div class="card"> <label class="main-content-label ">热门标签<a href="https://www.jb51.cc/all" class="pull-right">更多</a> </label> <div class="topcard-tags"><a href="https://www.jb51.cc/tag/python/" title="python">python</a><a href="https://www.jb51.cc/tag/JavaScript/" title="JavaScript">JavaScript</a><a href="https://www.jb51.cc/tag/java/" title="java">java</a><a href="https://www.jb51.cc/tag/HTML/" title="HTML">HTML</a><a href="https://www.jb51.cc/tag/PHP/" title="PHP">PHP</a><a href="https://www.jb51.cc/tag/reactjs/" title="reactjs">reactjs</a><a href="https://www.jb51.cc/tag/C/" title="C#">C#</a><a href="https://www.jb51.cc/tag/Android/" title="Android">Android</a><a href="https://www.jb51.cc/tag/CSS/" title="CSS">CSS</a><a href="https://www.jb51.cc/tag/Nodejs/" title="Node.js">Node.js</a><a href="https://www.jb51.cc/tag/sql/" title="sql">sql</a><a href="https://www.jb51.cc/tag/rp/" title="r">r</a><a href="https://www.jb51.cc/tag/python3x/" title="python-3.x">python-3.x</a><a href="https://www.jb51.cc/tag/MysqL/" title="MysqL">MysqL</a><a href="https://www.jb51.cc/tag/jQuery/" title="jQuery">jQuery</a><a href="https://www.jb51.cc/tag/c4343/" title="c++">c++</a><a href="https://www.jb51.cc/tag/pandas/" title="pandas">pandas</a><a href="https://www.jb51.cc/tag/flutter/" title="Flutter">Flutter</a><a href="https://www.jb51.cc/tag/angular/" title="angular">angular</a><a href="https://www.jb51.cc/tag/IOS/" title="IOS">IOS</a><a href="https://www.jb51.cc/tag/django/" title="django">django</a><a href="https://www.jb51.cc/tag/linux/" title="linux">linux</a><a href="https://www.jb51.cc/tag/swift/" title="swift">swift</a><a href="https://www.jb51.cc/tag/typescript/" title="typescript">typescript</a><a href="https://www.jb51.cc/tag/luyouqi/" title="路由器">路由器</a><a href="https://www.jb51.cc/tag/JSON/" title="JSON">JSON</a><a href="https://www.jb51.cc/tag/luyouqishezhi/" title="路由器设置">路由器设置</a><a href="https://www.jb51.cc/tag/wuxianluyouqi/" title="无线路由器">无线路由器</a><a href="https://www.jb51.cc/tag/h3c/" title="h3c">h3c</a><a href="https://www.jb51.cc/tag/huasan/" title="华三">华三</a><a href="https://www.jb51.cc/tag/huasanluyouqishezhi/" title="华三路由器设置">华三路由器设置</a><a href="https://www.jb51.cc/tag/huasanluyouqi/" title="华三路由器">华三路由器</a><a href="https://www.jb51.cc/tag/diannaoruanjianjiaocheng/" title="电脑软件教程">电脑软件教程</a><a href="https://www.jb51.cc/tag/arrays/" title="arrays">arrays</a><a href="https://www.jb51.cc/tag/docker/" title="docker">docker</a><a href="https://www.jb51.cc/tag/ruanjiantuwenjiaocheng/" title="软件图文教程">软件图文教程</a><a href="https://www.jb51.cc/tag/C/" title="C">C</a><a href="https://www.jb51.cc/tag/vuejs/" title="vue.js">vue.js</a><a href="https://www.jb51.cc/tag/laravel/" title="laravel">laravel</a><a href="https://www.jb51.cc/tag/springboot/" title="spring-boot">spring-boot</a></div> </div> </div> </div> </div> </div> </div> <footer id="footer"> <div class="container" style="width:1440px;"> <div class="row hidden-xs"> <div class="col-sm-12 col-md-9 col-lg-9 site-link"> <ul class="list-inline"> <li>友情链接:</li><li><a href="https://ai.jb51.cc/" title="ai导航是编程之家旗下ai方向的ai资讯、ai工具类集合导航站。" target="_blank" rel="nofollow">ai导航</a></li></ul> <ul class="list-inline"> <li><a href="https://www.jb51.cc" title="编程之家">编程之家</a></li>-<li><a href="https://t5m44pq3f7.jiandaoyun.com/f/638ca61b7b079a000a5d2dd6" rel="nofollow" title="我要投稿" target="_blank">我要投稿</a></li>-<li><a target="_blank" rel="nofollow" href="https://t5m44pq3f7.jiandaoyun.com/f/638ca8c69ad234000a79561f" title="广告合作">广告合作</a></li>-<li><a target="_blank" href="http://wpa.qq.com/msgrd?v=3&uin=76874919&site=qq&menu=yes">联系我们</a></li>-<li><a href="https://www.jb51.cc/disclaimers.html" title="免责声明">免责声明</a></li>-<li><a href="https://www.jb51.cc/sitemap/all/index.xml" title="网站地图" target="_blank">网站地图</a></li> </ul> <div>版权所有 © 2018编程之家<a href="https://beian.miit.gov.cn/" target="_blank" rel="nofollow">闽ICP备13020303号-8</a> </div> </div> <div class="col-sm-12 col-md-3 col-lg-3"><img src="https://www.jb51.cc/qrcode.jpg" width="90" alt="微信公众号搜索 “ 程序精选 ” ,选择关注!"> <div class="pull-right">微信公众号搜<span class="text-danger">"智元新知"</span>关注<br />微信扫一扫可直接关注哦!</div> </div> </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://www.jb51.cc/js/count.js"></script> </body> </html>