<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></title> 
<link>https://jackxiang.com/index.php</link> 
<description><![CDATA[赢在IT，Playin' with IT,Focus on Killer Application,Marketing Meets Technology.]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[向东博客 专注WEB应用 构架之美 --- 构架之美，在于尽态极妍 | 应用之美，在于药到病除]]></copyright>
<item>
<link>https://jackxiang.com/post//</link>
<title><![CDATA[php调用远程url的六种方法]]></title> 
<author>jack &lt;xdy108@126.com&gt;</author>
<category><![CDATA[WEB2.0]]></category>
<pubDate>Thu, 14 Jan 2010 05:24:41 +0000</pubDate> 
<guid>https://jackxiang.com/post//</guid> 
<description>
<![CDATA[ 
	<br/><div class="code">示例代码1: 用file_get_contents 以get方式获取内容<br/>&nbsp;&nbsp; &lt;?php<br/>&nbsp;&nbsp; $url=&#039;http://www.baidu.com/&#039;;<br/>&nbsp;&nbsp; $html=file_get_contents($url);<br/>&nbsp;&nbsp;//print_r($http_response_header);<br/>&nbsp;&nbsp;ec($html);<br/>&nbsp;&nbsp;printhr();<br/>&nbsp;&nbsp;printarr($http_response_header);<br/>&nbsp;&nbsp; printhr();<br/>&nbsp;&nbsp;?&gt;<br/>&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;示例代码2: 用fopen打开url, 以get方式获取内容<br/>&nbsp;&nbsp;&lt;?<br/>&nbsp;&nbsp;$fp=fopen($url,&#039;r&#039;);<br/>&nbsp;&nbsp;printarr(stream_get_meta_data($fp));<br/>&nbsp;&nbsp; printhr();<br/>&nbsp;&nbsp;while(!feof($fp))&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result.=fgets($fp,1024);<br/>&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;echo&quot;url body:&nbsp;&nbsp; $result&quot;;<br/>&nbsp;&nbsp;printhr();<br/>&nbsp;&nbsp;fclose($fp);<br/>&nbsp;&nbsp;?&gt;<br/>示例代码3：用file_get_contents函数,以post方式获取url<br/>&nbsp;&nbsp;&lt;?php<br/>&nbsp;&nbsp;$data=array(&#039;foo&#039;=&gt;&#039;bar&#039;);<br/>&nbsp;&nbsp;$data=http_build_query($data);<br/><br/>&nbsp;&nbsp;$opts=array(<br/>&nbsp;&nbsp;&nbsp;&nbsp;&#039;http&#039;=&gt;array(<br/>&nbsp;&nbsp; &#039;method&#039;=&gt;&#039;POST&#039;,<br/>&nbsp;&nbsp; &#039;header&#039;=&gt;&quot;Content-type: application/x-www-form-urlencoded&#92;r&#92;n&quot;.<br/>&nbsp;&nbsp;&nbsp;&nbsp;&quot;Content-Length: &quot;.strlen($data).&quot;&#92;r&#92;n&quot;,<br/>&nbsp;&nbsp;&#039;content&#039;=&gt;$data<br/>&nbsp;&nbsp;),<br/>&nbsp;&nbsp;);<br/>$context=stream_context_create($opts);<br/>$html=file_get_contents(&#039;http://localhost/e/admin/test.html&#039;,false,$context);<br/>echo$html;<br/>?&gt;<br/>示例代码4：用fsockopen函数打开url，以get方式获取完整的数据，包括header和body<br/>&lt;?<br/>functionget_url($url,$cookie=false)&#123;<br/>$url=parse_url($url);<br/>$query=$url&#91;path&#93;.&quot;?&quot;.$url&#91;query&#93;;<br/>ec(&quot;Query:&quot;.$query);<br/>$fp=fsockopen($url&#91;host&#93;,$url&#91;port&#93;?$url&#91;port&#93;:80,$errno,$errstr,30);<br/>if(!$fp)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;returnfalse;<br/>&#125;else&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$request=&quot;GET$queryHTTP/1.1&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$request.=&quot;Host:$url&#91;host&#93;&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$request.=&quot;Connection: Close&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if($cookie)$request.=&quot;Cookie:&nbsp;&nbsp;$cookie&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$request.=&quot;&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fwrite($fp,$request);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while(!@feof($fp))&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result.=@fgets($fp,1024);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fclose($fp);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return$result;<br/>&#125;<br/>&#125;<br/>//获取url的html部分，去掉header<br/>functionGetUrlHTML($url,$cookie=false)&#123;<br/>&nbsp;&nbsp; $rowdata=get_url($url,$cookie);<br/>&nbsp;&nbsp; if($rowdata)<br/>&nbsp;&nbsp; &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $body=stristr($rowdata,&quot;&#92;r&#92;n&#92;r&#92;n&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $body=substr($body,4,strlen($body));<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return$body;<br/>&nbsp;&nbsp; &#125;<br/>&nbsp;&nbsp; returnfalse;<br/>&#125;<br/>?&gt;<br/>示例代码5：用fsockopen函数打开url，以POST方式获取完整的数据，包括header和body<br/>&lt;?<br/>functionHTTP_Post($URL,$data,$cookie,$referrer=&quot;&quot;)&#123;<br/>// parsing the given URL<br/>$URL_Info=parse_url($URL);<br/><br/>// Building referrer<br/>if($referrer==&quot;&quot;)// if not given use this script. as referrer<br/>$referrer=&quot;111&quot;;<br/><br/>// making string from $data<br/>foreach($dataas$key=&gt;$value)<br/>$values&#91;&#93;=&quot;$key=&quot;.urlencode($value);<br/>$data_string=implode(&quot;&amp;&quot;,$values);<br/><br/>// Find out which port is needed - if not given use standard (=80)<br/>if(!isset($URL_Info&#91;&quot;port&quot;&#93;))<br/>$URL_Info&#91;&quot;port&quot;&#93;=80;<br/><br/>// building POST-request:<br/>$request.=&quot;POST &quot;.$URL_Info&#91;&quot;path&quot;&#93;.&quot; HTTP/1.1&#92;n&quot;;<br/>$request.=&quot;Host: &quot;.$URL_Info&#91;&quot;host&quot;&#93;.&quot;&#92;n&quot;;<br/>$request.=&quot;Referer:$referer&#92;n&quot;;<br/>$request.=&quot;Content-type: application/x-www-form-urlencoded&#92;n&quot;;<br/>$request.=&quot;Content-length: &quot;.strlen($data_string).&quot;&#92;n&quot;;<br/>$request.=&quot;Connection: close&#92;n&quot;;<br/>$request.=&quot;Cookie:&nbsp;&nbsp;$cookie&#92;n&quot;;<br/>$request.=&quot;&#92;n&quot;;<br/>$request.=$data_string.&quot;&#92;n&quot;;<br/><br/>$fp=fsockopen($URL_Info&#91;&quot;host&quot;&#93;,$URL_Info&#91;&quot;port&quot;&#93;);<br/>fputs($fp,$request);<br/>while(!feof($fp))&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$result.=fgets($fp,1024);<br/>&#125;<br/>fclose($fp);<br/>return$result;<br/>&#125;<br/>printhr();<br/>?&gt;<br/>示例代码6:使用curl库，使用curl库之前，你可能需要查看一下php.ini，查看是否已经打开了curl扩展<br/>&lt;?<br/>$ch = curl_init();<br/>$timeout = 5;<br/>curl_setopt ($ch, CURLOPT_URL, &#039;http://www.baidu.com/&#039;);<br/>curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);<br/>curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);<br/>$file_contents = curl_exec($ch);<br/>curl_close($ch);<br/>echo $file_contents;<br/>?&gt;<br/>关于curl库：<br/>curl官方网站http://curl.haxx.se/<br/>curl 是使用URL语法的传送文件工具，支持FTP、FTPS、HTTP HTPPS SCP SFTP TFTP TELNET DICT FILE和LDAP。curl 支持SSL证书、HTTP POST、HTTP PUT 、FTP 上传，kerberos、基于HTT格式的上传、代理、cookie、用户＋口令证明、文件传送恢复、http代理通道和大量其他有用的技巧<br/>&lt;?<br/>functionprintarr(array$arr)<br/>&#123;<br/>&nbsp;&nbsp; echo&quot;&lt;br&gt; Row field count: &quot;.count($arr).&quot;&lt;br&gt;&quot;;<br/>&nbsp;&nbsp; foreach($arras$key=&gt;$value)<br/>&nbsp;&nbsp; &#123;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo&quot;$key=$value&nbsp;&nbsp;&nbsp;&nbsp;&lt;br&gt;&quot;;<br/>&nbsp;&nbsp; &#125;<br/>&#125;<br/>?&gt;<br/>======================================================<br/>PHP抓取远程网站数据的代码<br/>现在可能还有很多程序爱好者都会遇到同样的疑问,就是要如何像搜索引擎那样去抓取别人网站的HTML代码,然后把代码收集整理成为自己有用的数据!今天就等我介绍一些简单例子吧.<br/><br/>Ⅰ.抓取远程网页标题的例子:<br/>以下是代码片段：<br/>&lt;?php<br/>/*<br/>+-------------------------------------------------------------<br/>+抓取网页标题的代码,直接拷贝本代码片段,另存为.php文件执行即可.<br/>+-------------------------------------------------------------<br/>*/<br/><br/>error_reporting(7);<br/>$file = fopen (&quot;http://www.dnsing.com/&quot;, &quot;r&quot;);<br/>if (!$file) &#123;<br/>echo &quot;&lt;font color=red&gt;Unable to open remote file.&lt;/font&gt;&#92;n&quot;;<br/>exit;<br/>&#125;<br/>while (!feof ($file)) &#123;<br/>$line = fgets ($file, 1024);<br/>if (eregi (&quot;&lt;title&gt;(.*)&lt;/title&gt;&quot;, $line, $out)) &#123;<br/>$title = $out&#91;1&#93;;<br/>echo &quot;&quot;.$title.&quot;&quot;;<br/>break;<br/>&#125;<br/>&#125;<br/>fclose($file);<br/><br/>//End<br/>?&gt;<br/>Ⅱ.抓取远程网页HTML代码的例子:<br/><br/>以下是代码片段：<br/>&lt;? php<br/>/*<br/>+----------------<br/>+DNSing Sprider<br/>+----------------<br/>*/<br/><br/>$fp = fsockopen(&quot;www.dnsing.com&quot;, 80, $errno, $errstr, 30);<br/>if (!$fp) &#123;<br/>&nbsp;&nbsp; echo &quot;$errstr ($errno)&lt;br/&gt;&#92;n&quot;;<br/>&#125; else &#123;<br/>&nbsp;&nbsp; $out = &quot;GET / HTTP/1.1&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp; $out .= &quot;Host:www.dnsing.com&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp; $out .= &quot;Connection: Close &#92;r&#92;n&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp; fputs($fp, $out);<br/>&nbsp;&nbsp; while (!feof($fp)) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp; echo fgets($fp, 128);<br/>&nbsp;&nbsp; &#125;<br/>&nbsp;&nbsp; fclose($fp);<br/>&#125;<br/>//End<br/>?&gt;<br/>以上两个代码片段都直接Copy回去运行就知道效果了,上面的例子只是抓取网页数据的雏形,要使其更适合自己的使用,情况有各异.所以,在此各位程序爱好者自己好好研究一下吧.<br/><br/>===============================<br/><br/>稍微有点意义的函数是：get_content_by_socket(), get_url(), get_content_url(), get_content_object 几个函数，也许能够给你点什么想法。<br/>&lt;?php<br/><br/>//获取所有内容url保存到文件<br/>function get_index($save_file, $prefix=&quot;index_&quot;)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $count = 68;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $i = 1;<br/>&nbsp;&nbsp;&nbsp;&nbsp; if (file_exists($save_file)) @unlink($save_file);<br/>&nbsp;&nbsp;&nbsp;&nbsp; $fp = fopen($save_file, &quot;a+&quot;) or die(&quot;Open &quot;. $save_file .&quot; failed&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp; while($i&lt;$count)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $url = $prefix . $i .&quot;.htm&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &quot;Get &quot;. $url .&quot;...&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $url_str = get_content_url(get_url($url));<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &quot; OK&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fwrite($fp, $url_str);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ++$i;<br/>&nbsp;&nbsp;&nbsp;&nbsp; &#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp; fclose($fp);<br/>&#125;<br/><br/>//获取目标多媒体对象<br/>function get_object($url_file, $save_file, $split=&quot;&#124;--:**:--&#124;&quot;)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp; if (!file_exists($url_file)) die($url_file .&quot; not exist&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp; $file_arr = file($url_file);<br/>&nbsp;&nbsp;&nbsp;&nbsp; if (!is_array($file_arr) &#124;&#124; empty($file_arr)) die($url_file .&quot; not content&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp; $url_arr = array_unique($file_arr);<br/>&nbsp;&nbsp;&nbsp;&nbsp; if (file_exists($save_file)) @unlink($save_file);<br/>&nbsp;&nbsp;&nbsp;&nbsp; $fp = fopen($save_file, &quot;a+&quot;) or die(&quot;Open save file &quot;. $save_file .&quot; failed&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp; foreach($url_arr as $url)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (empty($url)) continue;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &quot;Get &quot;. $url .&quot;...&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $html_str = get_url($url);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo $html_str;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo $url;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; exit;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $obj_str = get_content_object($html_str);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &quot; OK&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fwrite($fp, $obj_str);<br/>&nbsp;&nbsp;&nbsp;&nbsp; &#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp; fclose($fp);<br/>&#125;<br/><br/>//遍历目录获取文件内容<br/>function get_dir($save_file, $dir)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $dp = opendir($dir);<br/>&nbsp;&nbsp;&nbsp;&nbsp; if (file_exists($save_file)) @unlink($save_file);<br/>&nbsp;&nbsp;&nbsp;&nbsp; $fp = fopen($save_file, &quot;a+&quot;) or die(&quot;Open save file &quot;. $save_file .&quot; failed&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp; while(($file = readdir($dp)) != false)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ($file!=&quot;.&quot; &amp;&amp; $file!=&quot;..&quot;)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &quot;Read file &quot;. $file .&quot;...&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $file_content = file_get_contents($dir . $file);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $obj_str = get_content_object($file_content);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; echo &quot; OK&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fwrite($fp, $obj_str);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp; &#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp; fclose($fp);<br/>&#125;<br/><br/><br/>//获取指定url内容<br/>function get_url($url)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $reg = &#039;/^http:&#92;/&#92;/&#91;^&#92;/&#93;.+$/&#039;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; if (!preg_match($reg, $url)) die($url .&quot; invalid&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp; $fp = fopen($url, &quot;r&quot;) or die(&quot;Open url: &quot;. $url .&quot; failed.&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp; while($fc = fread($fp, 8192))&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $content .= $fc;<br/>&nbsp;&nbsp;&nbsp;&nbsp; &#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp; fclose($fp);<br/>&nbsp;&nbsp;&nbsp;&nbsp; if (empty($content))&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; die(&quot;Get url: &quot;. $url .&quot; content failed.&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp; &#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp; return $content;<br/>&#125;<br/><br/>//使用socket获取指定网页<br/>function get_content_by_socket($url, $host)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $fp = fsockopen($host, 80) or die(&quot;Open &quot;. $url .&quot; failed&quot;);<br/>&nbsp;&nbsp;&nbsp;&nbsp; $header = &quot;GET /&quot;.$url .&quot; HTTP/1.1&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $header .= &quot;Accept: */*&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $header .= &quot;Accept-Language: zh-cn&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $header .= &quot;Accept-Encoding: gzip, deflate&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $header .= &quot;User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; Maxthon; InfoPath.1; .NET CLR 2.0.50727)&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $header .= &quot;Host: &quot;. $host .&quot;&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $header .= &quot;Connection: Keep-Alive&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; //$header .= &quot;Cookie: cnzz02=2; rtime=1; ltime=1148456424859; cnzz_eid=56601755-&#92;r&#92;n&#92;r&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $header .= &quot;Connection: Close&#92;r&#92;n&#92;r&#92;n&quot;;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; fwrite($fp, $header);<br/>&nbsp;&nbsp;&nbsp;&nbsp; while (!feof($fp)) &#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $contents .= fgets($fp, 8192);<br/>&nbsp;&nbsp;&nbsp;&nbsp; &#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp; fclose($fp);<br/>&nbsp;&nbsp;&nbsp;&nbsp; return $contents;<br/>&#125;<br/><br/><br/>//获取指定内容里的url<br/>function get_content_url($host_url, $file_contents)&#123;<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; //$reg = &#039;/^(#&#124;javascript.*?&#124;ftp:&#92;/&#92;/.+&#124;http:&#92;/&#92;/.+&#124;.*?href.*?&#124;play.*?&#124;index.*?&#124;.*?asp)+$/i&#039;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; //$reg = &#039;/^(down.*?&#92;.html&#124;&#92;d+_&#92;d+&#92;.htm.*?)$/i&#039;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $rex = &quot;/(&#91;hH&#93;&#91;rR&#93;&#91;eE&#93;&#91;Ff&#93;)&#92;s*=&#92;s*&#91;&#039;&#92;&quot;&#93;*(&#91;^&gt;&#039;&#92;&quot;&#92;s&#93;+)&#91;&#92;&quot;&#039;&gt;&#93;*&#92;s*/i&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $reg = &#039;/^(down.*?&#92;.html)$/i&#039;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; preg_match_all ($rex, $file_contents, $r);<br/>&nbsp;&nbsp;&nbsp;&nbsp; $result = &quot;&quot;; //array();<br/>&nbsp;&nbsp;&nbsp;&nbsp; foreach($r as $c)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (is_array($c))&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach($c as $d)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (preg_match($reg, $d))&#123; $result .= $host_url . $d.&quot;&#92;n&quot;; &#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp; &#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp; return $result;<br/>&#125;<br/><br/>//获取指定内容中的多媒体文件<br/>function get_content_object($str, $split=&quot;&#124;--:**:--&#124;&quot;)&#123;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $regx = &quot;/href&#92;s*=&#92;s*&#91;&#039;&#92;&quot;&#93;*(&#91;^&gt;&#039;&#92;&quot;&#92;s&#93;+)&#91;&#92;&quot;&#039;&gt;&#93;*&#92;s*(&lt;b&gt;.*?&lt;&#92;/b&gt;)/i&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; preg_match_all($regx, $str, $result);<br/><br/>&nbsp;&nbsp;&nbsp;&nbsp; if (count($result) == 3)&#123;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result&#91;2&#93; = str_replace(&quot;&lt;b&gt;多媒体： &quot;, &quot;&quot;, $result&#91;2&#93;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result&#91;2&#93; = str_replace(&quot;&lt;/b&gt;&quot;, &quot;&quot;, $result&#91;2&#93;);<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $result = $result&#91;1&#93;&#91;0&#93; . $split .$result&#91;2&#93;&#91;0&#93; . &quot;&#92;n&quot;;<br/>&nbsp;&nbsp;&nbsp;&nbsp; &#125;<br/>&nbsp;&nbsp;&nbsp;&nbsp; return $result;<br/>&#125;<br/><br/>?&gt;<br/><br/>======================================================<br/><br/>同一域名对应多个IP时，PHP获取远程网页内容的函数<br/><br/>fgc就是简单的读取过来，把一切操作封装了<br/>fopen也进行了一些封装，但是需要你循环读取得到所有数据。<br/>fsockopen这是直板板的socket操作。<br/>如果仅仅是读取一个html页面，fgc更好。<br/>如果公司是通过防火墙上网，一 般的file_get_content函数就不行了。当然，通过一些socket操作，直接向proxy写http请求也是可以的，但是比较麻烦。<br/>如果你能确认文件很小，可以任选以上两种方式fopen ,join(&#039;&#039;,file($file));。比如，你只操作小于1k的文件，那最好还是用file_get_contents吧。<br/><br/>如果确定文件很大，或者不能确定文件的大小，那就最好使用文件流了。fopen一个1K的文件和fopen一个1G的文件没什么明显的区别。内容长，就可以花更长的时间去读，而不是让脚本死掉。<br/><br/>----------------------------------------------------<br/>http://www.phpcake.cn/archives/tag/fsockopen<br/>PHP获取远程网页内容有多种方式，例如用自带的file_get_contents、fopen等函数。<br/> <br/>&lt;?php&nbsp;&nbsp;<br/><br/>echo file_get_contents(&quot;http://blog.s135.com/abc.php&quot;);<br/>?&gt;　但是，在DNS轮询等负载均衡中，同一域名，可能对应多台服务器，多个IP。假设blog.s135.com被DNS解析到 72.249.146.213、72.249.146.214、72.249.146.215三个IP，用户每次访问blog.s135.com，系统会根据负载均衡的相应算法访问其中的一台服务器。 <br/>　　上周做一个视频项目时，就碰到这样一类需求：需要依次访问每台服务器上的一个PHP接口程序（假设为abc.php），查询这台服务器的传输状态。<br/><br/>　　这时就不能直接用file_get_contents访问http://blog.s135.com/abc.php了，因为它可能一直重复访问某一台服务器。<br/><br/>　　而采用依次访问http://72.249.146.213/abc.php、http://72.249.146.214/abc.php、http://72.249.146.215/abc.php的方法，在这三台服务器上的Web Server配有多个虚拟主机时，也是不行的。<br/><br/>　　通过设置本地hosts也不行，因为hosts不能设置多个IP对应同一个域名。<br/><br/>　　那就只有通过PHP和HTTP协议来实现：访问abc.php时，在header头中加上blog.s135.com域名。于是，我写了下面这个PHP函数：<br/><br/>&lt;?php&nbsp;&nbsp; <br/><br/> /************************&nbsp;&nbsp;<br/> * 函数用途：同一域名对应多个IP时，获取指定服务器的远程网页内容&nbsp;&nbsp;<br/> * 创建时间：2008-12-09&nbsp;&nbsp;<br/> * 创建人：张宴（blog.s135.com）&nbsp;&nbsp;<br/> * 参数说明：&nbsp;&nbsp;<br/> *&nbsp;&nbsp;&nbsp;&nbsp;$ip&nbsp;&nbsp; 服务器的IP地址&nbsp;&nbsp;<br/> *&nbsp;&nbsp;&nbsp;&nbsp;$host&nbsp;&nbsp; 服务器的host名称&nbsp;&nbsp;<br/> *&nbsp;&nbsp;&nbsp;&nbsp;$url&nbsp;&nbsp; 服务器的URL地址（不含域名）&nbsp;&nbsp;<br/> * 返回值：&nbsp;&nbsp;<br/> *&nbsp;&nbsp;&nbsp;&nbsp;获取到的远程网页内容&nbsp;&nbsp;<br/> *&nbsp;&nbsp;&nbsp;&nbsp;false&nbsp;&nbsp; 访问远程网页失败&nbsp;&nbsp;<br/> ************************/&nbsp;&nbsp; <br/> function HttpVisit($ip, $host, $url)&nbsp;&nbsp;&nbsp;&nbsp;<br/> &#123;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $errstr = &#039;&#039;;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $errno = &#039;&#039;;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; $fp = fsockopen ($ip, 80, $errno, $errstr, 90);&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; if (!$fp)&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; &#123;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return false;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; &#125;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; else&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp; &#123;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $out = &quot;GET &#123;$url&#125; HTTP/1.1&#92;r&#92;n&quot;;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $out .= &quot;Host:&#123;$host&#125;&#92;r&#92;n&quot;;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $out .= &quot;Connection: close&#92;r&#92;n&#92;r&#92;n&quot;;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fputs ($fp, $out);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br/>&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while($line = fread($fp, 4096))&#123;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$response .= $line;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &#125;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; fclose( $fp );&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; //去掉Header头信息&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $pos = strpos($response, &quot;&#92;r&#92;n&#92;r&#92;n&quot;);&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; $response = substr($response, $pos + 4);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp; <br/>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return $response;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp;&nbsp;&nbsp; &#125;&nbsp;&nbsp;&nbsp;&nbsp;<br/> &#125;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br/>&nbsp;&nbsp; <br/> //调用方法：&nbsp;&nbsp; <br/> $server_info1 = HttpVisit(&quot;72.249.146.213&quot;, &quot;blog.s135.com&quot;, &quot;/abc.php&quot;);&nbsp;&nbsp;&nbsp;&nbsp;<br/> $server_info2 = HttpVisit(&quot;72.249.146.214&quot;, &quot;blog.s135.com&quot;, &quot;/abc.php&quot;);&nbsp;&nbsp;&nbsp;&nbsp;<br/> $server_info3 = HttpVisit(&quot;72.249.146.215&quot;, &quot;blog.s135.com&quot;, &quot;/abc.php&quot;);&nbsp;&nbsp;&nbsp;&nbsp;<br/> ?&gt;&nbsp;&nbsp;</div>
]]>
</description>
</item><item>
<link>https://jackxiang.com/post//#blogcomment</link>
<title><![CDATA[[评论] php调用远程url的六种方法]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>https://jackxiang.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>