<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="zh-cn">
		<id>http://wiki.tomtalk.net/index.php?action=history&amp;feed=atom&amp;title=File_get_contents%E7%9A%84%E8%B6%85%E6%97%B6%E5%A4%84%E7%90%86</id>
		<title>File get contents的超时处理 - 版本历史</title>
		<link rel="self" type="application/atom+xml" href="http://wiki.tomtalk.net/index.php?action=history&amp;feed=atom&amp;title=File_get_contents%E7%9A%84%E8%B6%85%E6%97%B6%E5%A4%84%E7%90%86"/>
		<link rel="alternate" type="text/html" href="http://wiki.tomtalk.net/index.php?title=File_get_contents%E7%9A%84%E8%B6%85%E6%97%B6%E5%A4%84%E7%90%86&amp;action=history"/>
		<updated>2026-04-24T10:04:06Z</updated>
		<subtitle>本wiki的该页面的版本历史</subtitle>
		<generator>MediaWiki 1.24.2</generator>

	<entry>
		<id>http://wiki.tomtalk.net/index.php?title=File_get_contents%E7%9A%84%E8%B6%85%E6%97%B6%E5%A4%84%E7%90%86&amp;diff=4519&amp;oldid=prev</id>
		<title>Tom：/* file_get_contents的超时处理对连接超时无效 */</title>
		<link rel="alternate" type="text/html" href="http://wiki.tomtalk.net/index.php?title=File_get_contents%E7%9A%84%E8%B6%85%E6%97%B6%E5%A4%84%E7%90%86&amp;diff=4519&amp;oldid=prev"/>
				<updated>2016-08-27T02:40:51Z</updated>
		
		<summary type="html">&lt;p&gt;‎&lt;span dir=&quot;auto&quot;&gt;&lt;span class=&quot;autocomment&quot;&gt;file_get_contents的超时处理对连接超时无效&lt;/span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;新页面&lt;/b&gt;&lt;/p&gt;&lt;div&gt;写PHP的朋友可能经常会用到file_get_contents，对于复杂一点的应用可能还会用下面的方法给file_get_contents加上超时处理&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$opts = array(&lt;br /&gt;
        'http'=&amp;gt;array(&lt;br /&gt;
            'method'=&amp;gt;&amp;quot;GET&amp;quot;,&lt;br /&gt;
            'timeout'=&amp;gt;10,&lt;br /&gt;
            )&lt;br /&gt;
        );&lt;br /&gt;
 &lt;br /&gt;
$context = stream_context_create($opts);&lt;br /&gt;
 &lt;br /&gt;
$html = file_get_contents('http://www.aipai.com', false, $context);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
经过测试，这样的方法对于连接成功，但是后端输出/传输超时是没问题的。对于连接超时无效，file_get_contents函数会一直等到连接超时。&lt;br /&gt;
 &lt;br /&gt;
之前发现服务器上有一些CPU占用100%的PHP进程，是由于file_get_contents引起的，建议大家还是封装一个curl的函数或者类来用，类似这样&lt;br /&gt;
 &lt;br /&gt;
&amp;lt;source lang=&amp;quot;php&amp;quot;&amp;gt;&lt;br /&gt;
$ch = curl_init();&lt;br /&gt;
curl_setopt($ch, CURLOPT_URL, $url);&lt;br /&gt;
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));&lt;br /&gt;
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);&lt;br /&gt;
curl_setopt($ch, CURLOPT_TIMEOUT, 10);&lt;br /&gt;
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);&lt;br /&gt;
$reval = curl_exec($ch);&lt;br /&gt;
curl_close($ch);&lt;br /&gt;
&amp;lt;/source&amp;gt;&lt;/div&gt;</summary>
		<author><name>Tom</name></author>	</entry>

	</feed>