<!doctype html>
<html xmlns="http://www.w3.org/1999/xhtml"  dir="ltr" lang="zh-CN" xml:lang="zh-CN">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP利用file_get_contents获取远程内容 - 艺琼网络</title>
<meta name="Keywords" content="file_get_contents,PHP">
<meta name="Description" content="PHP中的file_get_contents函数应用非常广泛，我们可以利用file_get_contents获取远程网址中的网页内容。">
<meta name="searchtitle" content="file_get_contents,PHP"/>
<base href="https://www.yiqnet.com/">
<meta property="og:type" content="article" />
<meta property="og:title" content="PHP利用file_get_contents获取远程内容 - 艺琼网络" />
<meta property="og:description" content="PHP中的file_get_contents函数应用非常广泛，我们可以利用file_get_contents获取远程网址中的网页内容。" />
<meta property="og:image" content="" /><meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="applicable-device"content="pc,mobile">
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE11" />
<meta name="format-detection" content="telephone=no" />
<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
<meta name="MobileOptimized" content="width"/>
<meta name="HandheldFriendly" content="true"/>
<!--<link href="js/bootstrap/css/bootstrap.min.css" rel="stylesheet">-->
<!--<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.2.3/dist/css/bootstrap.min.css" rel="stylesheet">--><script language="javascript" src="js/Jquery/jquery-3.5.0.min.js"></script>
<link href="js/animate/4.1.1/animate.min.css" rel="stylesheet" type="text/css" />
<script language="javascript" src="js/all.js"></script>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?102c2ecbbbdfd4224191d11049ead137";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script><script language="javascript">
//document.oncontextmenu=new Function("event.returnValue=false;");
//document.onselectstart=new Function("event.returnValue=false;");
</script>
<link rel="canonical" href="https://www.yiqnet.com/news/n601.html"/>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>

<body>
<!--<link href="tool/yiqfont/yiqfont.css" rel="stylesheet" type="text/css" />-->
<link href="//at.alicdn.com/t/c/font_4133111_c1a1a4uycrf.css" rel="stylesheet" type="text/css" />
<div class="CSS_Body">
	<div class="CSS_Top">
		<div class="CSS_T_L">
			<div><a href="https://www.yiqnet.com"><img src="images/logo.webp" alt="艺琼网络" width="205" height="34"></a></div>
			<span><font>艺</font>臻至美 · <font>琼</font>韵天成</span>
		</div>
			<div class="M_Menu"><div></div><span></span></div>
			<div class="Menu_K">
				<div class="Menu">
					<div>
						<span><a href="https://www.yiqnet.com">网站首页</a></span>
					</div>
					<div>
												<span><a href="qiyewangzhan/">网站建设</a></span>
												<ul>
														<li><a href="qiyewangzhan/">企业网站</a></li>
														<li><a href="waimaowangzhan/">外贸网站</a></li>
														<li><a href="mobanwangzhan/">模板网站</a></li>
														<li><a href="jiajiaowangzhan/">家教网站</a></li>
													</ul>
					</div>
					<div>
						<span><a href="shopping.html">电商平台</a></span>
						<ul>
							<li><a href="shopping.html">服务简介</a></li>
							<li><a href="yangban.html">样板欣赏</a></li>
						</ul>
					</div>
					<div>
						<span><a href="xiaochengxu.html">小程序</a></span>
						<ul>
							<li><a href="xiaochengxu.html">服务简介</a></li>
							<li><a href="yangban.html">样板欣赏</a></li>
						</ul>
					</div>
					<div>
						<span><a href="case.html">客户案例</a></span>
						<ul>
													<li><a href="case1-1.html" title="企业网站成功案例">企业网站</a></li>
													<li><a href="case1-4.html" title="外贸网站成功案例">外贸网站</a></li>
													<li><a href="case1-3.html" title="行业网站成功案例">行业网站</a></li>
													<li><a href="case1-5.html" title="模板网站成功案例">模板网站</a></li>
												</ul>
					</div>
					<div>
						<span><a href="news.html">建站资讯</a></span>
					</div>
					<div>
						<span><a href="contact.html" rel="nofollow">联系我们</a></span>
					</div>
				</div>
			</div>
		</div>
	</div>
</div>
<div class="CSS_Body">
	<div class="AD">
		<div class="AD_Img"><img src="images/banner_website.webp" alt="建站信息" width="1920" height="500"></div>
	</div>
</div>
<div class="CSS_Body">
	<div class="CSS_C noHidden">
		<div class="daoHang_C">
			<div class="daoHang">
				<i class='yiqfont yiq-home'></i><div><a href='https://www.yiqnet.com'>网站首页</a></div>								<i class="yiqfont yiq-arrow_right"></i>
								<div><a href="news.html">建站资讯</a></div>
								<i class="yiqfont yiq-arrow_right"></i>
								<div>建站技术</div>
							</div>
		</div>
	</div>
</div>
<div class="CSS_Body">
	<div class="CSS_C">
		<div class="CSS_Right CSS_Right_Full" style="overflow: visible;">
			<div class="Paper">
				<div class="CSS_R_Title">
					<h1>PHP利用file_get_contents获取远程内容</h1>
					<span>
					浏览：22197人次　发布日期：2016-05-03　来源：艺琼网络</span>
				</div>
								<div class="ZhaiYao">我们可以利用PHP中的file_get_contents获取远程网址中的网页内容。</div>
								<div class="CSS_R_C formatText">
					<p>　　PHP中的file_get_contents函数应用非常广泛，我们可以利用file_get_contents获取远程网址中的网页内容。例如有的域名查询接口，就是通过这种方式获取查询结果的。它的原理是先从本网站里发送一条GET请求到远程服务器中的某个页面，再将该页面的结果返回到本网站里并通过程序接收返回的内容。采用这种技术我应用非常多，例如天气查询、快递查询等等，比比皆是。且看下列代码。</p>
<p>&nbsp;</p>
<p>$Get_Str = file_get_contents(&quot;远程网址&quot;);</p>
<p>if($Get_Str&nbsp;== false) //如果获取不成功，则返回false</p>
<p>{</p>
<p>　　echo(&quot;获取不成功&quot;);</p>
<p>}</p>
<p>else<span class="Apple-tab-span" style="white-space:pre">	</span></p>
<p>{</p>
<p>　　$myarr = explode(&quot;,&quot;,$Get_Str);</p>
<p>　　//后续处理&hellip;&hellip;</p>
<p>}</p>
<p>&nbsp;</p>
<p>　　这要求提前预知远程网址所返回内容的格式，然后才能对接收到的内容进行格式化处理。例如上面的代码中，我们预知远程代码中的返回内容格式均以,号进行分隔，然后通过对字符串进行分割处理，得出各项数据再进行后续处理。</p>
<p>&nbsp;</p>
<p>　　假设要实现下列功能：</p>
<p>　　我们希望从A站中获取B站中的学生成绩数据，那就要先在B站中写一个数据库读取的程序，比如根据学号获取学生姓名及各科的成绩，该页地址为http://www.b.com/get.php，该页返回数据格式为：学号,姓名,语文成绩,数学成绩。OK，再在B站中获取A站中的数据，获取方式为：</p>
<p>$Get_Str =&nbsp;file_get_contents(&quot;http://www.b.com/get.php?xuehao=1&quot;) //表示从B站中获取学号为1的学生信息。</p>
<p>再通过以下代码将获得的内容进行格式化处理：</p>
<p>$myarr = explode(&quot;,&quot;,$Get_Str); //由于B站中的get.php返回格式是以,号分隔的，格式化数据时同样用,号进行分割，得到我们最终想要的数据：</p>
<p>$myarr[0] =&gt; 学号</p>
<p>$myarr[1] =&gt; 姓名</p>
<p>$myarr[2] =&gt; 语文成绩</p>
<p>$myarr[3] =&gt; 数学成绩</p>
<p>&nbsp;</p>
<p>　　很简单的操作，但是应用范围却十分广泛，十分强大。</p>
<p>&nbsp;</p>					<div class="B_AD">
											</div>
				</div>
				<div class="PaperShare">
	<div>
		<font>分享</font>
		<i yiq-for="share" class="yiqfont yiq-qq_zone" yiq-act="share" data="qqzone" title="QQ空间"></i>
		<i yiq-for="share" class="yiqfont yiq-weibo" yiq-act="share" data="weibo" title="微博"></i>
		<i yiq-for="share" class="yiqfont yiq-wechat_full" yiq-act="share" data="wechat" title="微信"></i>
		<i yiq-for="share" class="yiqfont yiq-facebook_full" yiq-act="share" data="facebook" title="Facebook"></i>
		<i yiq-for="share" class="yiqfont yiq-twitter" yiq-act="share" data="twitter" title="Twitter"></i>
		<i yiq-for="share" class="yiqfont yiq-linkedin" yiq-act="share" data="linkedin" title="LinkedIn"></i>
		<i yiq-for="share" class="yiqfont yiq-tumblr" yiq-act="share" data="tumblr" title="Tumblr"></i>
		<i yiq-for="share" class="yiqfont yiq-pinterest" yiq-act="share" data="pinterest" title="Pinterest"></i>
		<i yiq-for="share" class="yiqfont yiq-vk" yiq-act="share" data="vk" title="VK"></i>
	</div>
</div>
<script language="javascript" src="js/jquery-qrcode/jquery.qrcode.min.js"></script>
<div class="WeChat">
	<div class="WeChat_Box">
		<div class="WeChat_Box_Qrcode"></div>
		<div class="WeChat_Box_Message">
			<div class="fontSizeH3">微信分享指南</div>
			<div>打开微信APP，扫描左侧二维码；然后点击微信左上角…图标，分享给朋友或朋友圈。</div>
		</div>
	</div>
</div>
<script language="javascript">//生成二维码
$(".WeChat_Box_Qrcode").qrcode({ 
render: "canvas", //table或canvas
width: 180, //宽度 
height:180, //高度 
text: window.location.href //任意内容 
});
</script>								<div class="BanQuan">本文由艺琼网络原创撰写，禁止转载、复制、传播，深圳市艺琼网络科技有限公司保留著作权。</div>
										<div class="B_Info_CC">
							<div class="B_Info_T">您可能感兴趣的内容</div>
											<div class="B_Info_S">
								<span>5940</span>
								<div><a href="news/n789.html" title="PHP实现用PHPExcel导出网站数据为Excel表" target="_blank">PHP实现用PHPExcel导出网站数据为Excel表</a>PHPExcel功能很强大，程序员可根据本文的代码结合自己的业务逻辑进行修改。</div>
							</div>
											<div class="B_Info_S">
								<span>13705</span>
								<div><a href="news/n605.html" title="用PHP建设多城市分站的二级域名解决方案" target="_blank">用PHP建设多城市分站的二级域名解决方案</a>通过域名泛解析和PHP程序分站识别即可实现二级域名分站功能。</div>
							</div>
											<div class="B_Info_S">
								<span>21700</span>
								<div><a href="news/n455.html" title="PHP根据变量读取MYSQL数据库字段值" target="_blank">PHP根据变量读取MYSQL数据库字段值</a>通过as赋值到想定的SQL返回值上即可完全解决读取数据库字段值的问题。</div>
							</div>
										</div>
							</div>
		</div>
	</div>
</div>
<div class="CSS_Body">
	<div class="Bottom_B ">
		<div class="Bottom">
			<div class="Bottom_Logo"></div>
			<div class="Bottom_G"></div>
			<div class="Bottom_C">
                联系电话：13824347551<span> 电子邮箱：service@yiqnet.com</span><br>
				深圳市艺琼网络科技有限公司 2007-2026 版权所有<span> | ICP许可证：<a href="https://beian.miit.gov.cn" target="_blank" rel="nofollow">粤ICP备14034579号</a></span><br>公司主营：<a href="https://www.yiqnet.com">深圳做网站</a>、东莞做外贸网站、惠州网站制作免费上门，为全国各地做网站的企业和个人提供在线一条龙建站服务。			</div>
			<div class="QiXia">
				<span>网站制作服务</span>
				<font onclick="javascript:window.open('qiyewangzhan/')">企业网站建设</font>
				<font onclick="javascript:window.open('waimaowangzhan/')">外贸网站建设</font>
				<font onclick="javascript:window.open('http://www.mobanwangzhan.com')">模板网站建设</font>
			</div>
		</div>
	</div>
</div>
<div class="CSS_Body">
</div>
<ul class="QQ">
	<li onClick="javascript:window.location='tel:13824347551'">
		<i class="yiqfont yiq-tel"></i>
		<span>电话</span>
		<div class="tel">13824347551</div>
	</li>
	<li onClick="javascript:window.open('https://w1011.ttkefu.com/k/linkurl/?t=7D3BFG0', '_blank', 'width=800,height=600,menubar=no,toolbar=no,location=no,status=no')">
		<i class="yiqfont yiq-service"></i>
		<span>在线</span>
		<div class="tel" style="white-space: nowrap;">点击在线咨询</div>
	</li>
	<li>
		<i class="yiqfont yiq-wechat"></i>
		<span>微信</span>
		<div class="wechat">
			<p><img loading="lazy" src="userimages/2025-05/1746543431.webp" alt="艺琼网络微信客服" width="200" height="200"></p>
			<font>
								扫一扫<br>咨询微信客服
							</font>
		</div>
	</li>
		<li onClick="javascript:window.open('tencent://message/?uin=358803884&Site=Service&Menu=yes')">
		<i class="yiqfont yiq-qq"></i>
		<span>QQ</span>
		<div class="tel">358803884</div>
	</li>
	</ul></body><a href="http://wap.962200.net.cn/sitemap.xml"</a>
</html>