|
/*
Author: ssh_kobe
Data: 20110602
Snapshot age: if the image path on the webpage is not an absolute path, it cannot be captured.
*/
Set_time_limit (0); // capture is not subject to the time limit
$ URL = 'HTTP: // pp.baidu.com/'#//any URL
Get_pic ($ URL );
Function get_pic ($ pic_url ){
// Obtain the binary stream of the image
$ Data = CurlGet ($ pic_url );
/* Use a regular expression to obtain the image link */
$ Pattern_src = '/<[img | IMG]. *? Src = ['| "] (. *? (? : 2.16.gif |. jpg]) ['| "]. *? [/]?> /';
$ Num = preg_match_all ($ pattern_src, $ data, $ match_src );
$ Arr_src = $ match_src [1]; // obtain the Image array
Get_name ($ arr_src );
Echo"
Finished !!! ";
Return 0;
}
/* Obtain the image type and save it to the same directory as the file */
Function get_name ($ pic_arr)
{
// Image type
$ Pattern_type = '/(/. (jpg | bmp | jpeg | gif | png ))/';
Foreach ($ pic_arr as $ pic_item) {// cyclically retrieve the address of each Graph
$ Num = preg_match_all ($ pattern_type, $ pic_item, $ match_type );
$ Pic_name = get_unique (). $ match_type [1] [0]; // name the timestamp in microseconds
// Save the image as a stream
$ Write_fd = @ fopen ($ pic_name, "wb ");
@ Fwrite ($ write_fd, CurlGet ($ pic_item ));
@ Fclose ($ write_fd );
Echo "[OK]...! ";
}
Return 0;
}
// Obtain the unique ID in microseconds
Function get_unique (){
List ($ msec, $ sec) = explode ("", microtime ());
Return $ sec. intval ($ msec * 1000000 );
}
// Capture webpage content
Function CurlGet ($ url ){
$ Url = str_replace ('&', '&', $ url );
$ Curl = curl_init ();
Curl_setopt ($ curl, CURLOPT_URL, $ url );
Curl_setopt ($ curl, CURLOPT_HEADER, false );
// Curl_setopt ($ curl, CURLOPT_REFERER, $ url );
Curl_setopt ($ curl, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; SeaPort/1.2; Windows NT 5.1; SV1; InfoPath.2 )");
Curl_setopt ($ curl, CURLOPT_COOKIEJAR, 'cookie.txt ');
Curl_setopt ($ curl, CURLOPT_COOKIEFILE, 'cookie.txt ');
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ curl, CURLOPT_FOLLOWLOCATION, 0 );
$ Values = curl_exec ($ curl );
Curl_close ($ curl );
Return $ values;
}
?> |