投递文章 投稿指南 RSS订阅 站点通告:网站升级,部分数据正在导入之中,请等待..
您的位置:首页 >> Web编程 >> php

PHP下载google相册到本地

发布时间:2009-06-21 15:58:03  文章来源:动态网站  浏览次数:1803  
承德互联 专业的unix(freebsd)主机 无限子域 BGP多线 自主开发面版 更快 更稳 更安全

调用方式 xxx.php?user=xxx

保证程序放的当前目录可写文件夹 可写文件

默认存放方式为

./用户名/相册1描述/图片描述-1.图片后缀

PHP Code复制内容到剪贴板
  1. <?php   
  2.   
  3. function open($host,$file){   
  4.   
  5. $return = "";   
  6.   
  7. $header = '';   
  8.   
  9. while($header!='200'){   
  10.   
  11. $fp = fsockopen($host, 80, $errno$errstr, 30);   
  12.   
  13. if (!$fp) {   
  14.   
  15.     echo "$errstr ($errno)<br />\n";   
  16.   
  17. else {   
  18.   
  19.     $out = "GET $file HTTP/1.1\r\n";   
  20.   
  21.     $out .= "Host: $host\r\n";   
  22.   
  23.     $out .= "P3P: CP=\"CAO PSA OUR\"\r\n";   
  24.   
  25.     $out .= "User-Agent: Baiduspider+(+[url]http://www.baidu.com/search/spider.htm[/url])\r\n";   
  26.   
  27.     $out .= "Connection: Close\r\n\r\n";   
  28.   
  29.     fwrite($fp$out);   
  30.   
  31.     while (!feof($fp)) {   
  32.   
  33.         $return .= fgets($fp,1024);   
  34.   
  35.     }   
  36.   
  37.     fclose($fp);   
  38.   
  39. }   
  40.   
  41.     
  42.   
  43. preg_match("/HTTP\/1.1 (.*?) OK/",$return,$output);   
  44.   
  45. $header = $output[1];   
  46.   
  47. }   
  48.   
  49. preg_match("/\r\n\r\n(.+)/is"$return$out);   
  50.   
  51. $return = $out[1];   
  52.   
  53. return mb_convert_encoding($return"GB2312""UTF-8");;   
  54.   
  55. }   
  56.   
  57. function openimg($host,$file){   
  58.   
  59. $return = "";   
  60.   
  61. $header = '302';   
  62.   
  63. while($header=='302'){   
  64.   
  65.         $fp = fsockopen($host, 80, $errno$errstr, 30);   
  66.   
  67. if (!$fp) {   
  68.   
  69.     echo "$errstr ($errno)<br />\n";   
  70.   
  71. else {   
  72.   
  73.     $out = "GET $file HTTP/1.1\r\n";   
  74.   
  75.     $out .= "Host: $host\r\n";   
  76.   
  77.     $out .= "User-Agent: Baiduspider+(+[url]http://www.baidu.com/search/spider.htm[/url])\r\n";   
  78.   
  79.     $out .= "Connection: Close\r\n\r\n";   
  80.   
  81.     fwrite($fp$out);   
  82.   
  83.     while (!feof($fp)) {   
  84.   
  85.         $return .= fgets($fp,1024);   
  86.   
  87.     }   
  88.   
  89.     fclose($fp);   
  90.   
  91. }   
  92.   
  93. preg_match("/HTTP\/1.0 (.*?) /",$return,$output);   
  94.   
  95. $header = $output[1];   
  96.   
  97. }   
  98.   
  99. preg_match("/\r\n\r\n(.+)/is"$return$out);   
  100.   
  101. $return = $out[1];   
  102.   
  103. if($header=='404'$return='';   
  104.   
  105. return $return;   
  106.   
  107. }   
  108.   
  109. function img($host,$file,$dir,$name){   
  110.   
  111. $data = openimg($host,$file);   
  112.   
  113. // echo $dir;   
  114.   
  115. $blankimg = '../images/blank.jpg';   
  116.   
  117. $filename = $dir.'/'.$name;   
  118.   
  119. // echo $filename;   
  120.   
  121. if(!is_dir($dir)){   
  122.   
  123.         if(!mkdir($dir)) die('create dir error');   
  124.   
  125. }   
  126.   
  127. // echo $data;   
  128.   
  129.     if (!$handle = fopen($filename'w+')) {   
  130.   
  131.          echo "can't open $filename";   
  132.   
  133.          exit;   
  134.   
  135.     }   
  136.   
  137.     if (fwrite($handle$data) === FALSE) {   
  138.   
  139.         echo "can't write $filename";   
  140.   
  141.         exit;   
  142.   
  143.     }   
  144.   
  145.     // echo "write to $filename done";   
  146.   
  147.     fclose($handle);   
  148.   
  149. echo $filename."<br />\r\n";   
  150.   
  151. }   
  152.   
  153. $aid = ($_GET['aid']!='')?$_GET['aid']:0;   
  154.   
  155. $user = $_GET['user'];   
  156.   
  157. if(!is_dir($user)){   
  158.   
  159.         if(!mkdir($user)) die('create dir error');   
  160.   
  161. }   
  162.   
  163. if($user==''die("user string can't be blank");   
  164.   
  165. $userstr = '/'.$user.'/';  
  166.  
  167. $html = str_replace("'",'',open('picasaweb.google.com',$userstr));   
  168.   
  169. preg_match_all("/,access:public\n,title:(.*?)\n,url:(.*?)\n,src/s",$html,$album);   
  170.   
  171. $all = count($album[1]);   
  172.   
  173. if($aid>$alldie('OK');   
  174.   
  175. $aurl = str_replace('\x2F','/',$album[2][$aid]);   
  176.   
  177. $astr = './'.$user.'/'.$album[1][$aid];   
  178.   
  179. $afile = str_replace('http://picasaweb.google.com','',$aurl);   
  180.   
  181. $html = str_replace('$','',open('picasaweb.google.com',$afile));   
  182.   
  183. preg_match_all("/\"summarytype\":\"text\",\"summary\":\"(.*?)\",\"contenttype\":\"(.*?)\",\"contentsrc\":\"(.*?)\",\"link\":/s",$html,$pics);   
  184.   
  185. $allpic = count($pics[3]);   
  186.   
  187. for($i=0;$i<$allpic;$i++){   
  188.   
  189.         $pic = $pics[3][$i];   
  190.   
  191.         $typetemp = explode('.',$pic);   
  192.   
  193.         $type = $typetemp[(count($typetemp)-1)];   
  194.   
  195.         $pname = $pics[1][$i].'-'.$i.'.'.$type;   
  196.   
  197.         $temp = explode('ggpht.com',$pic);   
  198.   
  199.         $imghost = str_replace('http://','',$temp[0].'ggpht.com');   
  200.   
  201.         $imgfile = $temp[1];   
  202.   
  203.         img($imghost,$imgfile,$astr,$pname);   
  204.   
  205. }   
  206.   
  207. $aid++;   
  208.   
  209. echo '<meta http-equiv="refresh" content="2;url=?aid='.$aid.'&user='.$user.'">';   
  210.   
  211. ?>   
  212.   
  213. <title>下载google相册</title>   
  214.   
  215. <form>   
  216.   
  217. <input name=user type=text>   
  218.   
  219. <input type=submit>   
  220.   
  221. </form>   
  222.   


 

作者:
与好友一起分享精彩内容:

收藏到QQ书签】【返回顶部】【返回首页
关于站点 - 广告服务 - 联系我们 - 版权隐私 - 免责声明 - 程序支持 - 网站地图 - 意见反馈 - 返回顶部