<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>潘锦的空间 &#187; HTTP</title>
	<atom:link href="https://www.phppan.com/tag/http/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.phppan.com</link>
	<description>SaaS SaaS架构 团队管理 技术管理 技术架构 PHP 内核 扩展 项目管理</description>
	<lastBuildDate>Sat, 25 Apr 2026 00:56:17 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>HTTP缓存算法</title>
		<link>https://www.phppan.com/2012/12/http-cache-algorithm/</link>
		<comments>https://www.phppan.com/2012/12/http-cache-algorithm/#comments</comments>
		<pubDate>Mon, 10 Dec 2012 00:26:46 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[程序相关]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[HTTP协议]]></category>
		<category><![CDATA[HTTP权威指南]]></category>
		<category><![CDATA[HTTP缓存]]></category>
		<category><![CDATA[缓存]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=1768</guid>
		<description><![CDATA[HTTP协议缓存的目标是去除许多情况下对于发送请求的需求和去除许多情况下发送完整请求的需求。以不发送请求或减少 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>HTTP协议缓存的目标是去除许多情况下对于发送请求的需求和去除许多情况下发送完整请求的需求。以不发送请求或减少请求传输的数据量来优化整个HTTP架构，此目标的实现可以产生如下好处：</p>
<ul>
<li>减少网络传输的冗余信息量</li>
<li>缓解网络瓶颈的问题</li>
<li>降低对原始服务器的请求量</li>
<li>减少了传送距离，降低了因为距离而产生的时延</li>
</ul>
<p>缓存基本处理过程包括七个步骤。</p>
<ol>
<li>接收 &#8211; 缓存从网络中读取抵达的请求报文</li>
<li>解析 &#8211; 缓存对报文进行解析，提取出URL和各种首部</li>
<li>查询 &#8211; 缓存查看是否有本地副本可用，如果没有，就获取一份副本，并保存在本地</li>
<li>新鲜度检测 &#8211; 缓存查看已缓存副本是否足够新鲜，如果不是，就询问服务器是否有任何更新</li>
<li>创建响应 &#8211; 缓存会用新的首部和已缓存主体来构建一条响应报文</li>
<li>发送 &#8211; 缓存通过网络将响应发回给客户端</li>
<li>日志 &#8211; 缓存可选地创建一个日志文件条目来描述这个事务</li>
</ol>
<p>这里的缓存可以是本地客户端缓存，也可以是代理缓存之类的公共缓存。</p>
<h2>HTTP缓存模型</h2>
<p>HTTP缓存可以在不依赖服务器记住有哪些缓存拥有文档副本，而实现文档的一致。这些机制称为文档过期（document  expiration）和服务器再验证（server revalidation），也可以称它们为截止模型和证实模型。</p>
<p>截止模型是HTTP请求中带上标记文档的过期时间，HTTP协议中使用如下两个字段标记过期时间：</p>
<ul>
<li>Expires字段 &#8211; 指定一个绝对的过期日期。</li>
<li>Cache-control:max-age &#8211; 定义文档的最大使用期，从第一次生成文档到文档不再新鲜，无法使用为止，最大的合法生存时间（单位为s）</li>
</ul>
<p>仅仅使用截止模型还不够，即使文档过期了，也并不意味着当前文档和原始服务器的文档不一致了。此时就到证实模型大显身手的时候了。证实模型需要询问原始服务器文档是否发生了变化。其依赖于HTTP协议的如下字段：</p>
<ul>
<li>If-Modified-Since字段 &#8211;  如果从指定日期之后文档被修改了，就执行请求的方法。可以与Last-modified服务器响应首部配合使用。它告诉服务器只有在客户端缓存了对象的副本后，又服务器对其进行了修改的情况下，才在回复中发送此对象。如果服务器对象没有修改，返回304  Not Modified。如果服务器修改了此对象，发送此对象，返回200 OK。如果服务器删除了些对象，返回404 Not Found。</li>
<li>If-None-Match字段 &#8211; 服务器可以为文档提供特殊的标签（ETag），如果此标签与服务器的标签不一样，就会执行请求的方法。</li>
</ul>
<p>如果服务器应答中包括一个ETag，又包括一个Last-Mofidied值，则客户端在发送请求时使用两种证实机制，并且只有当两种证实机制都满足时才会返回304  Not Modified。</p>
<p>缓存在新鲜度检测时，只需要计算两个值：已缓存副本的使用期和已缓存副本的新鲜生存期。</p>
<h2>HTTP缓存使用期算法</h2>
<p>响应的使用期是服务器发布响应（或通过证实模型再验证）之后经过的总时间。使用期包括了因特网中传输的时间，在中间节点缓存的时间，以及在本地缓存中的停留时间。</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;">       <span style="color: #666666; font-style: italic;">/*
       * age_value 当代理服务器用自己的头部去响应请求时，Age标明实体产生到现在多长时间了。
       * date_value HTTP 服务器应答中的Date字段 原始服务器
       * request_time 缓存的请求时间
       * response_time 缓存获取应答的时间
       * now 当前时间
       */</span>
&nbsp;
      apparent_age <span style="color: #339933;">=</span> <span style="color: #990000;">max</span>（<span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> response_time <span style="color: #339933;">-</span> date_value）<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//缓存收到响应时响应的年龄 处理时钟偏差存在时，可能为负的情况</span>
&nbsp;
      corrected_received_age <span style="color: #339933;">=</span> <span style="color: #990000;">max</span>（apparent_age<span style="color: #339933;">,</span> age_value）<span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  容忍Age首部的错误</span>
&nbsp;
      response_delay <span style="color: #339933;">=</span> response_time <span style="color: #339933;">-</span> request_time<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 处理网络时延，导致结果保守</span>
&nbsp;
      corrected_initial_age <span style="color: #339933;">=</span> corrected_received_age <span style="color: #339933;">+</span> response_delay<span style="color: #339933;">;</span>
&nbsp;
      resident_time <span style="color: #339933;">=</span> now <span style="color: #339933;">-</span> response_time<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// 本地的停留时间，即收到响应到现在的时间间隔</span>
&nbsp;
      current_age   <span style="color: #339933;">=</span> corrected_initial_age <span style="color: #339933;">+</span> resident_time<span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>因此，完整的使用期计算算法是通过查看Date首部和Age首部来判断响应已使用的时间，再记录其在本地缓存中的停留时间就是总的使用期。除此之外，HTTP协议对时钟偏差和网络时延进行了一补偿，特别是其对网络时延的补偿，可能会重复计算已使用的时间，从而使整个算法产生保守的结果。这种保守的效果时，如果出错了，算法只会使文档看起来比实际使用期要老，并引发再验证。</p>
<h2>HTTP缓存新鲜度算法</h2>
<p>通过已缓存文档的使用期，根据服务器和客户端限制来计算新鲜生存期，就可以确定已缓存的文档是否新鲜。已缓存文档的使用期在前面已经介绍过了，这小节我们来看看新鲜生存期的计算。</p>
<p>为了确定一条响应是保鲜的（fresh）还是陈旧的（stale），我们需要将其保鲜寿命（freshness  lifetime)和年龄(age)进行比较。年龄的计算见13.2.3节，本节讲解怎样计算保鲜寿命，以及判定一个响应是否已经过期。在下面的讨论中，数值可以用任何适于算术操作的形式表示。</p>
<p>与此相关的首部字段包括（按优先级从高到低）：  Cache-Control字段中“max-age”控制指令的值、Expires、Last-Modified、默认最小的生存期。用PHP代码体现如下：</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #009933; font-style: italic;">/**
     * $heuristic 启发式过期值应不大于从那个时间开始到现在这段时间间隔的某个分数
     * $Max_Age_value_set  是否存在Max_Age值  Cache-Control字段中“max-age”控制指令的值
     * $Max_Age_value  Max_Age值
     * $Expires_value_set 是否存在Expires值
     * $Expires_value Expires值
     * $Date_value Date头部
     * $default_cache_min_lifetime 
     * $default_cache_max_lifetime
     */</span>
    <span style="color: #000000; font-weight: bold;">function</span> server_freshness_limit<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$Max_Age_value_set</span><span style="color: #339933;">,</span> <span style="color: #000088;">$Max_Age_value</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$Expires_value_set</span><span style="color: #339933;">,</span> <span style="color: #000088;">$Expires_value</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$Date_value</span><span style="color: #339933;">,</span> <span style="color: #000088;">$default_cache_min_lifetime</span><span style="color: #339933;">,</span> <span style="color: #000088;">$default_cache_max_lifetime</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000088;">$factor</span> <span style="color: #339933;">=</span> <span style="color:#800080;">0.1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//典型设置为10%</span>
&nbsp;
        <span style="color: #000088;">$heuristic</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//  启发式 默认为0</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$Max_Age_value_set</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>   <span style="color: #666666; font-style: italic;">// 优先级一为 Max_Age</span>
            <span style="color: #000088;">$freshness_lifetime</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$Max_Age_value</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Expires_value_set</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>  <span style="color: #666666; font-style: italic;">//   优先级二为Expires</span>
            <span style="color: #000088;">$freshness_lifetime</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$Expires_value</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$Date_value</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">elseif</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$Last_Modified_value_set</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">//  优先级三为Last_Modified</span>
            <span style="color: #000088;">$freshness_lifetime</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>int<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$factor</span> <span style="color: #339933;">*</span> <span style="color: #990000;">max</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$Last_Modified_value</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$Date_value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$heuristic</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//  启发式</span>
        <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>  
            <span style="color: #000088;">$freshness_lifetime</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$default_cache_min_lifetime</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$heuristic</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//  启发式</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$heuristic</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$freshness_lifetime</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$freshness_lifetime</span> <span style="color: #339933;">&gt;</span> <span style="color: #000088;">$default_cache_max_lifetime</span> ? <span style="color: #000088;">$default_cache_max_lifetime</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$freshness_lifetime</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$freshness_lifetime</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$freshness_lifetime</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$default_cache_min_lifetime</span> ? <span style="color: #000088;">$default_cache_min_lifetime</span> <span style="color: #339933;">:</span> <span style="color: #000088;">$freshness_lifetime</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$freshness_lifetime</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>计算响应是否过期非常简单： response_is_fresh = (server_freshness_limit() &gt;  current_age)</p>
<p>以此为《HTTP权威指南》第七章读书笔记。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2012/12/http-cache-algorithm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>获取远程大文件部分内容的方法</title>
		<link>https://www.phppan.com/2009/10/get-a-part-of-a-remote-large-files/</link>
		<comments>https://www.phppan.com/2009/10/get-a-part-of-a-remote-large-files/#comments</comments>
		<pubDate>Wed, 21 Oct 2009 12:12:46 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[PHP应用]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=338</guid>
		<description><![CDATA[获取远程大文件部分内容的方法 【需求】 取远程文件的一部分，文件是以换行隔开，并且这个文件可能比较大 【第一次 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>获取远程大文件部分内容的方法</p>
<p>【需求】<br />
取远程文件的一部分，文件是以换行隔开，并且这个文件可能比较大<br />
<br />
【第一次的方案】<br />
这是我的第一个方案，也是很傻很天真却正确的方案，<br />
这个在当时没有考虑到文件会比较大，而且也没有考虑下载时间过长，导致程序出错！<br />
解决方案是使用PHP中的file()函数，这样会得到一个以行为单位的数组，然后是对这个数组的处理<br />
file函数也可以使用file_get_contents代替，只是需要使用explode函数处理一下。<br />
<br />
【优化后的方案】<br />
由于我们所要取的数据不是全部，只是其中的一部分，我们可以只取其中的一部分，<br />
开始使用fseek函数定位文件指针，只是它不支持URL地址<br />
后来想到文件下载中的断点续传，在HTTP协议中存在Range字段，<br />
Range是在 HTTP/1.1（http://www.w3.org/Protocols/rfc2616/rfc2616.html）里新增的一个 header field，也是现在众多号称多线程下载工具（如 FlashGet、迅雷等）实现多线程下载的核心所在。<br />
Range 的规范定义如下：<br />
ranges-specifier = byte-ranges-specifier<br />
byte-ranges-specifier = bytes-unit “=” byte-range-set<br />
byte-range-set = 1#( byte-range-spec | suffix-byte-range-spec )<br />
byte-range-spec = first-byte-pos “-” [last-byte-pos]<br />
first-byte-pos = 1*DIGIT<br />
last-byte-pos = 1*DIGIT</p>
<p>一些其它介绍可以移步：http://minms.blogbus.com/logs/39569593.html<br />
或者直接查看RFC</p>
<p>我们使用文件记录上次访问的位置，下次直接从这个位置访问<br />
使用PHP的fsockopen函数实现获取大文件部分内容的代码如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?PHP</span>
<span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Content-type:text/html;charset=UTF-8&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;downloads.php.net&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$port</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$path</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/johannes/php-5.3.1RC1.tar.bz2&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$length</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">100</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> get_remote_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$port</span><span style="color: #339933;">,</span> <span style="color: #000088;">$path</span><span style="color: #339933;">,</span> <span style="color: #000088;">$start</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$content_length</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$file</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/Content-Length:.?(\d+)/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$header_length</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content_length</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$header_length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$header_length</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>   <span style="color: #666666; font-style: italic;">//  更新start，可能需要记录当前位置</span>
            <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$start</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$header_length</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$content</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//echo xdebug_time_index();</span>
<span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #009933; font-style: italic;">/**
 * 获取远程文件内容
 * @param &lt;type&gt; $host 主机
 * @param &lt;type&gt; $port 端口
 * @param &lt;type&gt; $path 路径
 * @param &lt;type&gt; $start 开始位置
 * @return &lt;type&gt; 远程部分内容
 */</span>
<span style="color: #000000; font-weight: bold;">function</span> get_remote_content<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$port</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">80</span><span style="color: #339933;">,</span> <span style="color: #000088;">$path</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$start</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #000088;">$length</span> <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$fp</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fsockopen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$port</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errno</span><span style="color: #339933;">,</span> <span style="color: #000088;">$errstr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$range</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$start</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;-&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$length</span> <span style="color: #339933;">!=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$range</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$start</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$length</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000088;">$out</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;GET &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$path</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot; HTTP/1.1<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Host: &quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$host</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Range:bytes=&quot;</span> <span style="color: #339933;">.</span><span style="color: #000088;">$range</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//  取start之后的内容</span>
    <span style="color: #000088;">$out</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;Connection: Close<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #990000;">fwrite</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #000088;">$out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$buffer</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">feof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$buffer</span> <span style="color: #339933;">.=</span> <span style="color: #990000;">fgets</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$fp</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4096</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$buffer</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2009/10/get-a-part-of-a-remote-large-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>去掉fsockopen返回结果中的HTTP头信息的2种方法</title>
		<link>https://www.phppan.com/2009/10/two-methods-of-getting-content-by-fsockopen/</link>
		<comments>https://www.phppan.com/2009/10/two-methods-of-getting-content-by-fsockopen/#comments</comments>
		<pubDate>Mon, 19 Oct 2009 03:10:11 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[HTTP]]></category>
		<category><![CDATA[PHP应用]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=330</guid>
		<description><![CDATA[去掉fsockopen返回结果中的HTTP头信息的两种方法 1、【使用split或substr,strpos截 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>去掉fsockopen返回结果中的HTTP头信息的两种方法</p>
<p>1、【使用split或substr,strpos截断】<br />
在返回的内容中HTTP头信息与正文内容是以两个“换行回车”隔开的所以我们可以在此截断，取之后的内容。</p>
<p>2、【先取Content-Length,然后截取】<br />
在HTTP协议中，Content-Length字段是一个比较重要的字段，它标明了服务器返回数据的长度，这个长度是不包含HTTP头长度的，也就是说我们可以从 总长度－Content-Length  开始截取<br />
PHP代码如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">   <span style="color: #990000;">preg_match</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/Content-Length:.?(\d+)/&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$content</span><span style="color: #339933;">,</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$length</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$content</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$content</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  <span style="color: #666666; font-style: italic;">//感谢@fw</span>
    <span style="color: #666666; font-style: italic;">//$content = substr($content, strlen($content) - $length);</span></pre></td></tr></table></div>

<p>这段段代码在性能上还有优化的空间</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2009/10/two-methods-of-getting-content-by-fsockopen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
