PHP下载google相册到本地

调用方式 xxx.php?user=xxx

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

默认存放方式为

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

PHP Code 复制内容到剪贴板

    1. function open( $host , $file ){
    1. $return = "" ;
    1. $header = '' ;
    1. while ( $header != '200' ){
    1. $fp = fsockopen ( $host , 80, $errno , $errstr , 30);
    1. if (! $fp ) {
    1. echo "$errstr ($errno)
1<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)

1<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 . "

1<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 > $all ) die ( '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 '

1&lt;meta content="2;url=?aid='  .  $aid  .  '&amp;amp;user='  .  $user  .  '" http-equiv="refresh"/&gt;

' ; 210. 211.

1  212.   213.
下载google相册``` 214. 215. ```
216. 217. 218. 219. 220. 221.
``` 222.
Published At
Categories with Web编程
Tagged with
comments powered by Disqus