如何通过php获取别的服务器上的图片

时间:2007-09-08 09:59:53      类别:PHP, Web技术|WEB Tech      本文链接:生活点滴Enjoy Life

目前本站FB订阅图标的显示服务申请已经停止,但是作为一个负责的男人,我会一直保留以前的申请的图标继续显示的,除非不可抗拒的因素产生,俗话说,授人鱼不如授人以渔,所以对于虚拟主机abroad的兄弟,下面的这段代码或许对你有用。当然啊,你也别担心麻烦,后面会提供源代码的下载和使用方法

  1. < ?php
  2. $feedburner_image ="http://feeds.feedburner.com/~fc/storydaycom?bg=99CCFF&fg=444444&anim=0";//图片的路径
  3. header("Content-type: image/gif");
  4. //
  5. //下面的两个函数来自php.net
  6. //函数的作用是利用socks获取http内容
  7. //
  8.  function httpSocketConnection($host, $method, $path, $data)
  9.    {
  10.        $method = strtoupper($method);       
  11.      
  12.        if ($method == "GET")
  13.        {
  14.            $path.= '?'.$data;
  15.        }   
  16.      
  17.        $filePointer = fsockopen($host, 80, $errorNumber, $errorString);
  18.      
  19.        if (!$filePointer)
  20.        {
  21.               return false;
  22.        }
  23.  
  24.        $requestHeader = $method." ".$path."  HTTP/1.1\r\n";
  25.        $requestHeader.= "Host: ".$host."\r\n";
  26.        $requestHeader.= "User-Agent:      Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0\r\n";
  27.        $requestHeader.= "Content-Type: application/x-www-form-urlencoded\r\n";
  28.  
  29.        if ($method == "POST")
  30.        {
  31.            $requestHeader.= "Content-Length: ".strlen($data)."\r\n";
  32.        }
  33.      
  34.        $requestHeader.= "Connection: close\r\n\r\n";
  35.      
  36.        if ($method == "POST")
  37.        {
  38.            $requestHeader.= $data;
  39.        }           
  40.  
  41.        fwrite($filePointer, $requestHeader);
  42.      
  43.        $responseHeader = '';
  44.        $responseContent = '';
  45.  
  46.        do
  47.        {
  48.            $responseHeader.= fread($filePointer, 1);
  49.        }
  50.        while (!preg_match('/\\r\\n\\r\\n$/', $responseHeader));
  51.      
  52.      
  53.        if (!strstr($responseHeader, "Transfer-Encoding: chunked"))
  54.        {
  55.            while (!feof($filePointer))
  56.            {
  57.                $responseContent.= fgets($filePointer, 128);
  58.            }
  59.        }
  60.        else
  61.        {
  62.  
  63.            while ($chunk_length = hexdec(fgets($filePointer)))
  64.            {
  65.                $responseContentChunk = '';
  66.                $read_length = 0;
  67.              
  68.                while ($read_length < $chunk_length)
  69.                {
  70.                    $responseContentChunk .= fread($filePointer, $chunk_length - $read_length);
  71.                    $read_length = strlen($responseContentChunk);
  72.                }
  73.  
  74.                $responseContent.= $responseContentChunk;
  75.              
  76.                fgets($filePointer);
  77.              
  78.            }
  79.          
  80.        }
  81.      
  82.      
  83.        return chop($responseContent);
  84.    }
  85.  
  86.         function parseChunked($response) {
  87.  
  88.           $do = true;
  89.           $return = "";
  90.           $size = NULL;
  91.  
  92.           while($do) {
  93.  
  94.            // suppress warnings w/ @ if offset not in string (past end)
  95.            $pos = @strpos($response, "\r\n", 0);
  96.  
  97.            if(!($pos === false) && $size === NULL) {
  98.  
  99.                  // we found CRLF, get len from hex
  100.                  $size = hexdec(substr($response, 0, $pos));
  101.                                   
  102.                  // get the actual chunk-len, reset $response, $size
  103.                  $return .= substr($response, $pos+2, $size);
  104.                  $response = substr($response, ($pos+2) + $size);
  105.                  $size = NULL;
  106.            }
  107.            else {
  108.                  $do = false;
  109.            } // end if/else
  110.  
  111.           } // end while
  112.  
  113.           return $return;
  114.         } // end function parseChunked()
  115.  
  116. $http_get_path = str_replace("http://feeds.feedburner.com","",$feedburner_image );
  117. require "date.php";//存放上次获取图标的时间
  118. if( date('YmdH') - $lastupdate > 4 ) {
  119. //如果相距上次获取图标的时间超过了4小时
  120. //则更新图标
  121. //reload the image every 4 hours
  122.        
  123.         $phpstr = "";
  124.         $contents = httpSocketConnection('feeds.feedburner.com', "GET", $http_get_path, $qString);
  125.         //write image  file
  126.         //将图标写入文件中
  127.         $fp =fopen( "feedburner.gif" ,"w+" );
  128.         fwrite($fp , $contents);
  129.         fclose($fp);
  130.         //同时更新本次获取图标的时间
  131.         $fp =fopen( "date.php" ,"w+" );
  132.         $datecontent = "< ?php \$lastupdate = ".date('YmdH').";\n ?>";
  133.         fwrite($fp , $datecontent);
  134.         fclose($fp);
  135.         die($contents);
  136. }
  137. readfile("feedburner.gif");
  138. ?>

代码在这里
fb.zip
请将fb.php用记事本打开,修改顶部$feedburner_image 的路径为你要转发图片的路径
然后将其他两个文件的属性设置成0666,最后在需要引用图片的地方引用fb.php的路径即可。

利用同样的原理大家也可以轻松的解决ajax http的跨域处理问题,不明白这一行在说什么的,忽略这一行。

该日志未加标签
发表于 2007-09-08 09:59:53 目录:PHP, Web技术|WEB Tech [RSS 2.0] 你可以发表评论, 或者从您的网站 trackback
如果您喜欢本blog,欢迎你的feed订阅,谢谢你的支持
上一篇: « 新闻时间 下一篇: 电路设计中的阻抗匹配 »

前2排已经被占据了 快抢好位置哦

  • 1楼 Jake 在 2007.09.09 22:33发表评论如下:

    看不懂。高手。

  • 2楼 istef 在 2007.09.15 18:10发表评论如下:

    我用的是 libcurl,只写了几行就可以了。而且 libcurl 的效率也不低。另外 bluehost 好像支持 curl 吧。[r]不错,curl是最好的选择,包括以前做缓存的时候都是用的这个东西,但是考虑到n多服务器没有这个lib,也考虑到n多服务器不支持fopen url,所以就用了这个socksopen,反正已经缓存,效率已经无所谓[/r]

  • 转到第
(Required)
(Required, not published)
如果留言未显示请不要重复留言,我将为你恢复!


生活点滴Enjoy Life is proudly powered by WordPress | admin| About Us | cosbeta| Bluehost| site map 5.436s & 29