预览模式: 普通 | 列表

一.问题的提出


偶然发现,Winform里的WebBrowser和IE实际安装的版本似乎并不同步,很有趣!

 

下面有张图,里面一个窗口是用IE9打开某网站,另一个窗口是用Winform+WebBrowser打开同样的网站,有意思的事情出现了。

 

在IE9窗口中,这个网站左边菜单树无法显示,原因是IE9使用的技术较新,而网站使用的技术较旧,未能及时同步更新到支持IE9所致。该如何办呢?

微软在IE9中提供一个兼容性视图功能,只要一点击兼容性视图就可以兼容旧版本浏览器的网站。
 


引用MSDN “To overcome potential compatibility issues, Internet Explorer supports a feature called Compatibility View that allows users to display troublesome pages in IE7 mode. “ URL: http://msdn.microsoft.com/en-us/library/dd567845(v=VS.85).aspx

也就是说,这个兼容模式就是IE7的模式。

 

又查了些ms资料,发现这个兼容性视图在IE8时首次引入,在IE9,IE10中都将继续支持。

 

再翻回来看Winform+WebBrowser这个窗口,它打开同样的网站却能显示左边菜单树,这样看来,似乎WebBrowser不用设置就默认自动打开了兼容性视图模式。

 

现在就去仔细查一下权威资料,核实一下两个问题:
1.Webbrowser与IE到底是什么关系?是否确实用ie内核, 是否本质上和360安全浏览器,傲游浏览器和腾讯TT等IE内核浏览器相同。
2.Webbrowser是否使用兼容浏览模式,以及这个模式是否能改?

 

二.查询结果

1.webbrowser调用的就是本机IE9,并且webbrowser默认就是运行在IE7 mode下,除非你改变它.

发现一个msdn的帖子,明确表示webbrowser调用的就是本机IE9,并且webbrowser默认就是运行在IE7 mode下,除非你改变它。
How to make c# WebBrowser equivalent to IE browser  
http://social.msdn.microsoft.com/Forums/en/winforms/thread/2ed65b9d-c601-4ca8-bde1-64584fc87515

摘几句:
Wow first post with such bold claim without any source backing up. You probably should read the IE SDK (the manual you need to read if you want to use the webbrowser control) or dig through the IE programming forums (that's the place others often go when they are stuck on IE programming) if you want to use the webbrowser control.

Webbrowser is a wrapper around IE APIs. There is no such thing as multiple versions of IE coexisting on the same computer. You will always get the one and only version of IE installed on the computer from webbrowser control.

There are many, many documented setting differences between default IE and webbrowser. Basically you don't have to opt out new features in webbrowser that may break your app (the Visual Studio team learned a hard lesson here, when IE8 breaks Visual Studio's wizards) , you have to write code to opt in, unless the improvement is security related. That means the webbrowser will run in IE7 mode unless you change the mode in feature control.

Note some web site declare their requirement of IE7 or IE8 mode. It may not be wise to force the IE9 mode. 

 

2.微软新闻组的一个帖子,Webbrowser Control without IE,里面明确提到,不装IE,无法用webbrowser.
http://groups.google.com/group/microsoft.public.vb.controls/browse_thread/thread/7575bd25e0730ded/aa40f3dfc799407d?lnk=gst&q=WebBrowser+ie#aa40f3dfc799407d

IE must be installed on the machine for you to use Webbrowser Control.

Internet Explorer MUST be installed to use the WebBrowser control.  There are simply no ifs, ands, or buts about it.  How can you expect to use IE functionality if IE is not installed?

 

3.如何设置WebBrowser在IE9 mode下工作呢?
答曰:需要修改注册表,具体看下面4,5,6,尤其6最全面,可以光看6。

 

4.WPF webbrowser control using IE7 instead of IE9
http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/4431908e-1869-4435-bcea-a3ec0820edfb

摘抄几句:
How do I make it so the WPF WebBrowser control will use IE9 as the browser engine instead of IE7?
I have some HTML that is rendering differently in the WebBrowser control than in the IE9 browser. When I run the following javascript in the WebBrowser, the result is "7". as in IE7.

I found an article by Rick Strahl that describes registry settings that will get the WebBrowser to use IE9. But I would like to avoid that. And I am interested to know how IE7 comes about being used.http://www.west-wind.com/weblog/posts/2011/May/21/Web-Browser-Control-Specifying-the-IE-Version
回答:You want to avoid the only documented way to set document compatibility mode for webbrowser hosts? Why?

 

5.WebBrowser and CSS3 ?
http://social.msdn.microsoft.com/Forums/en-AU/winforms/thread/1b656af7-bda9-47d9-8f9a-1d886d3688ca
Web browser control by default runs in compatibility mode unless you set the feature browser emulation registry key. The fact that IE9 is able to render CSS3 correctly and browser control is not seems to suggest browser control is not running in IE9 standards mode.

You'll need to set Browser emulation feature key (FEATURE_BROWSER_EMULATION) described at this link http://msdn.microsoft.com/en-us/library/ee330730%28v=vs.85%29.aspx

You can use 9000 value, unless you want to force IE 9 standards mode for all pages. In case of later, you need to use 9999.

hklm

If hklm and 64bit machine used, you need to check is Wow6432Node needs to be changed.

And finally you need to add process name hosting browser control as value name in the registry key.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION]
"prevhost.exe"=dword:00001f40
"sllauncher.exe"=dword:00001f40
"WindowsFormsApplication1.exe"=dword:0000270f

 

6.Web Browser Control – Specifying the IE Version 
http://www.west-wind.com/weblog/posts/2011/May/21/Web-Browser-Control-Specifying-the-IE-Version

I use the Internet Explorer Web Browser Control in a lot of my applications to display document type layout. HTML happens to be one of the most common document formats and displaying data in this format – even in desktop applications, is often way easier than using normal desktop technologies.

One issue the Web Browser Control has that it’s perpetually stuck in IE 7 rendering mode by default. Even though IE 8 and now 9 have significantly upgraded the IE rendering engine to be more CSS and HTML compliant by default the Web Browser control will have none of it. IE 9 in particular – with its much improved CSS support and basic HTML 5 support is a big improvement and even though the IE control uses some of IE’s internal rendering technology it’s still stuck in the old IE 7 rendering by default.

This applies whether you’re using the Web Browser control in a WPF application, a WinForms app, a FoxPro or VB classic application using the ActiveX control. Behind the scenes all these UI platforms use the COM interfaces and so you’re stuck by those same rules.

Feature Delegation via Registry Hacks
Fortunately starting with Internet Explore 8 and later there’s a fix for this problem via a registry setting. You can specify a registry key to specify which rendering mode and version of IE should be used by that application. These are not global mind you – they have to be enabled for each application individually.

There are two different sets of keys for 32 bit and 64 bit applications.

32 bit:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

Value Key: yourapplication.exe

64 bit:

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

Value Key: yourapplication.exe

The value to set this key to is (taken from MSDN here) as decimal values:

9999 (0x270F) 
Internet Explorer 9. Webpages are displayed in IE9 Standards mode, regardless of the !DOCTYPE directive.

9000 (0x2328) 
Internet Explorer 9. Webpages containing standards-based !DOCTYPE directives are displayed in IE9 mode.

8888 (0x22B8) 
Webpages are displayed in IE8 Standards mode, regardless of the !DOCTYPE directive.

8000 (0x1F40) 
Webpages containing standards-based !DOCTYPE directives are displayed in IE8 mode.

7000 (0x1B58) 
Webpages containing standards-based !DOCTYPE directives are displayed in IE7 Standards mode.

 

没错, 64位就是写这里, 它和32位的修改点不同.


There are two different sets of keys for 32 bit and 64 bit applications.

32 bit: 

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

Value Key: yourapplication.exe

64 bit: 

HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

 

 

分类:技术文章 | 固定链接 | 评论: 1 | 引用: 0 | 查看次数: 3955

VB中webbrowser调用IE版本问题

  做了一个Winform的项目。项目里使用了WebBrowser控件。以前一直都以为WebBrowser是直接调用的系统自带的IE,IE是呈现出什么样的页面WebBrowser就呈现出什么样的页面。其实并非如此。我的Winform程序加载了一个Web项目。这个项目对IE8一下的浏览器布局兼容不是太好。本机使用的是IE8,项目是.net4.0。用本机浏览器查看Web项目没有任何问题。可是调用Winform里的WebBrowser浏览Web项目页面却出现了布局问题。所以我就开始质疑到底WebBrowser是不是IE8的模式啊。于是打开IETester,从IE9一直到IE5都查看了一遍,发现最像WebBrowser布局呈现的是IE7模式下。在搜了一些相关资料原来WebBrowser使用的是IE的兼容模式进行浏览(IE7模式)。

  如何让WebBrowser引用IE 8 呢。可以通过注册表的新增键值实现

  打开HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION

查看更多...

Tags: vb webbrowser

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 3071

php输出json格式数据

 

PHP代码
  1. <?php  
  2.   
  3. header(‘Content-type:text/json’);     //这句是重点,它告诉接收数据的对象此页面输出的是json数据;  
  4.   
  5. $json={“n”:”name”,”p”:”password”};           //虽然这行数据形式上是json格式,如果没有上面那句的话,它是不会被当做json格式的数据被处理的;  
  6.   
  7. echo $json;  
  8.   
  9. ?>  

 

Tags: php

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 2694

php有道翻译api调用方法

 

PHP代码
  1. $url = "http://fanyi.youdao.com/openapi.do?keyfrom=xujiangtao&key=1490852988&type=data&doctype=json&version=1.1&q=".$content;  
  2. $list = file_get_contents($url);//来源同穷论坛(http://www.tongqiong.com)  
  3. $js_de = json_decode($list,true);  

 

Tags: php

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 2924

PHP Curl 模拟POST 可以https

 

PHP代码
  1. function vpost($url,$data,$cookie){ // 模拟提交数据函数  
  2.     $curl = curl_init(); // 启动一个CURL会话  
  3.     curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址  
  4.     curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查  
  5.     curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在  
  6.     curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器  
  7.     curl_setopt($curl, CURLOPT_COOKIE, $cookie);  
  8.     curl_setopt($curl, CURLOPT_REFERER,'https://www.baidu.com');// 设置Referer  
  9.     curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求  
  10.     curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包  
  11.     curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环  
  12.     curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容  
  13.     curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回  
  14.     $tmpInfo = curl_exec($curl); // 执行操作  
  15.     if (curl_errno($curl)) {  
  16.        echo 'Errno'.curl_error($curl);//捕抓异常  
  17.     }  
  18.     curl_close($curl); // 关闭CURL会话  
  19.     return $tmpInfo// 返回数据  
  20. }  

 

Tags: php

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 3390

PHP利用CURL进行POST数据上传

 

PHP代码
  1. ////二纬码  
  2. $QRCode_URL="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$ACC_TOKEN;  
  3.   
  4. $data ='{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}} ';  
  5. /* 
  6. $ch = curl_init($MENU_URL); 
  7. curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST"); 
  8. curl_setopt($ch, CURLOPT_POSTFIELDS, $data); 
  9. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
  10. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);  
  11. curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length:'.strlen($data))); 
  12. $info = curl_exec($ch); 
  13. */  
  14.     function post($url$params = false, $header = array()){  
  15.     $ch = curl_init();  
  16.     $cookieFile = 'sdadsd_cookiejar.txt';  
  17.   
  18.     curl_setopt($ch, CURLOPT_POST, 1);  
  19.     curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);  
  20.     curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);  
  21.     curl_setopt($ch, CURLOPT_COOKIEFILE,$cookieFile);  
  22.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);  
  23.     curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  
  24.     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);  
  25.     curl_setopt($ch, CURLOPT_HTTPGET, true);  
  26.     curl_setopt($ch, CURLOPT_TIMEOUT, 30);  
  27.     if($params !== false){ curl_setopt($ch, CURLOPT_POSTFIELDS , $params);}  
  28.     curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0');  
  29.     curl_setopt($ch, CURLOPT_URL,$url);  
  30.     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);  
  31.   
  32.     $result = curl_exec($ch);  
  33.     curl_close($ch);  
  34.   
  35.     return $result;  
  36. }  
  37. $result = post($QRCode_URL,$data);  

 

Tags: php

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 2511

php web下载网站代码

PHP代码
  1. <?php  
  2. /** 
  3.  * @author MarkZhao(zhaody901#126.com) 
  4.  */  
  5. $dir = dirname(__FILE__).'/';  
  6. // zip and download  
  7. // zip.php?type=zip&dir=subdir  
  8.   
  9. // unzip deflate way zipped zip file  
  10. // zip.php?type=unzip&name=deflate  
  11.   
  12. $type = $_GET['type'];  
  13. $type or exit('die no type to do!');  
  14. $name = isset($_GET['name']) ? $_GET['name'].'.zip' : 'zip.zip';  
  15. $subdir = $_GET['dir'];  
  16. $subdir or die('no sub dir to zip');  
  17. set_time_limit(0);  
  18. if ($type=='zip') {  
  19.     $zip = new ZipArchive();  
  20.     if ($zip->open($name, ZipArchive::Create) === TRUE) {  
  21.         $files = map($dir.$subdir);  
  22.         foreach ($files as $file) {  
  23.             $dir  = str_replace('\\', '/', $dir); 
  24.             $file = str_replace('\\', '/', $file); 
  25.             $zip->addFile($file, '_ROOT_/' . str_replace($dir, '', $file)); 
  26.         } 
  27.         $zip->close(); 
  28.         down($dir.$name); 
  29.         echo 'ok'; 
  30.     } else { 
  31.         echo 'failed'; 
  32.     } 
  33. } else if ($type=='unzip') { 
  34.  
  35.     $zip = new ZipArchive(); 
  36.     if ($zip->open($name) === TRUE) { 
  37.         $zip->extractTo(dirname(__FILE__).'/'); 
  38.         $zip->close(); 
  39.         echo 'ok'; 
  40.     } else { 
  41.         echo 'failed'; 
  42.     } 
  43. } 
  44.  
  45.  
  46. function map($directory) { 
  47.     $files = array(); 
  48.     if (file_exists($directory) && is_dir($directory)) { 
  49.         foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) { 
  50.             $files[] = $file->getRealPath(); 
  51.         } 
  52.     } 
  53.     return $files; 
  54. } 
  55.  
  56. function down($file) { 
  57.     $filename = $filename ? $filename : basename($file); 
  58.     $filesize = filesize($file); 
  59.     ob_end_clean(); 
  60.     @set_time_limit(0); 
  61.     if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) { 
  62.         header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
  63.         header('Pragma: public'); 
  64.     } else { 
  65.         header('Pragma: no-cache'); 
  66.     } 
  67.     header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT'); 
  68.     header('Content-Encoding: none'); 
  69.     header('Content-Length: ' . $filesize); 
  70.     header('Content-Disposition: attachment; filename=' . $filename); 
  71.     header('Content-Type: ' . $filetype);  
  72.     readfile($file);  
  73.     exit;  
  74. }  

 

1. 打包代码, 并直接通过浏览器下载
2. 解压zip文件到服务器

Tags: php

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 2938

php合成或者创建gif动画

1. 首先需要确认GD库是否正常。

2. 如果是合成图片,请确保把分解的图片放在frames的文件夹里面。

3. GIFEncoder.class.php 类

GIFEncoder.zip

PHP代码
  1. <?php  
  2. include "GIFEncoder.class.php";  
  3.   
  4. ob_start();  
  5. $board_width = 60;  
  6. $board_height = 60;  
  7. $pad_width = 5;  
  8. $pad_height = 15;  
  9. $ball_size = 5;  
  10. $game_width = $board_width - $pad_width*2 - $ball_size;  
  11. $game_height = $board_height-$ball_size;  
  12.   
  13. $x = 0;  
  14. $y = rand(0,$game_height);  
  15. $xv = rand(1,10);  
  16. $yv = rand(1,10);  
  17. $pt[] = array($x,$y);  
  18. do{  
  19.         $x += $xv;  
  20.         $y += $yv;  
  21.         if($x > $game_width){  
  22.                 $xv = -1*$xv;  
  23.                 $x = $game_width - ($x-$game_width);  
  24.         }elseif($x < 0){  
  25.                 $xv = -1*$xv;  
  26.                 $x = abs($x);  
  27.         }  
  28.         if($y>$game_height){  
  29.                 $yv = -1*$yv;  
  30.                 $y = $game_height - ($y - $game_height);  
  31.         }elseif($y<0){  
  32.                 $yv = -1*$yv;  
  33.                 $y = abs($y);  
  34.         }  
  35.         $pt[] = array($x,$y);  
  36. }while($x!=$pt[0][0]||$y!=$pt[0][1]);  
  37.   
  38. $i = 0;  
  39. while(isset($pt[$i])){  
  40.         $image = imagecreate($board_width,$board_height);  
  41.         imagecolorallocate($image, 0,0,0);  
  42.         $color = imagecolorallocate($image, 255,255,255);  
  43.         $color2 = imagecolorallocate($image, 255,0,0);  
  44.           
  45.         if($pt[$i][1] + $pad_height < $board_width){  
  46.                 imagefilledrectangle($image,0,$pt[$i][1],$pad_width$pt[$i][1]+$pad_height,$color);  
  47.         }else{  
  48.                 imagefilledrectangle($image,0,$board_width-$pad_height,$pad_width$board_width,$color);  
  49.         }  
  50.         imagefilledrectangle($image,$board_width-$pad_width,0,$board_width$board_height,$color2);  
  51.         imagefilledrectangle($image,$pad_width+$pt[$i][0], $ball_size+$pt[$i][1]-$ball_size$pad_width+$pt[$i][0]+$ball_size$ball_size+$pt[$i][1],$color);  
  52.         //imagesetpixel($image,$pt[$i][0],$pt[$i][1],$color);  
  53.         imagegif($image);  
  54.         imagedestroy($image);  
  55.         $imagedata[] = ob_get_contents();  
  56.         ob_clean();  
  57.         ++$i;  
  58. }  
  59.   
  60. $gif = new GIFEncoder(  
  61.                             $imagedata,  
  62.                             100,  
  63.                             0,  
  64.                             2,  
  65.                             0, 0, 1,  
  66.                             "bin"  
  67.         );  
  68.           
  69. Header ('Content-type:image/gif');  
  70. echo $gif->GetAnimation();  
  71. ?>  

 

Tags: php

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 2309

php识别电脑还是手机访问网站

 

PHP代码
  1. <?php  
  2. function isMobile(){    
  3.     $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';    
  4.     $useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';          
  5.     function CheckSubstrs($substrs,$text){    
  6.         foreach($substrs as $substr)    
  7.             if(false!==strpos($text,$substr)){    
  8.                 return true;    
  9.             }    
  10.             return false;    
  11.     }  
  12.     $mobile_os_list=array('Google Wireless Transcoder','Windows CE','WindowsCE','Symbian','Android','armv6l','armv5','Mobile','CentOS','mowser','AvantGo','Opera Mobi','J2ME/MIDP','Smartphone','Go.Web','Palm','iPAQ');  
  13.     $mobile_token_list=array('Profile/MIDP','Configuration/CLDC-','160×160','176×220','240×240','240×320','320×240','UP.Browser','UP.Link','SymbianOS','PalmOS','PocketPC','SonyEricsson','Nokia','BlackBerry','Vodafone','BenQ','Novarra-Vision','Iris','NetFront','HTC_','Xda_','SAMSUNG-SGH','Wapaka','DoCoMo','iPhone','iPod');    
  14.             
  15.     $found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock) ||    
  16.               CheckSubstrs($mobile_token_list,$useragent);    
  17.             
  18.     if ($found_mobile){    
  19.         return true;    
  20.     }else{    
  21.         return false;    
  22.     }    
  23. }  
  24. if (isMobile())  
  25.     echo '手机登录www.cuangs.com';  
  26. else  
  27.     echo '电脑登录www.ydutv.com';  
  28. ?>  

 

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 2248

PHP SMTP邮件发送类,支持SSL连接

PHP代码
  1. <?php    
  2. /**  
  3. * 邮件发送类  
  4. * 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送,带附件(单个或多个附件),支持到服务器的ssl连接  
  5. * 需要的php扩展:sockets、Fileinfo和openssl。  
  6. * 编码格式是UTF-8,传输编码格式是base64  
  7. * @example  
  8. * $mail = new MySendMail();  
  9. * $mail->setServer("smtp@126.com", "XXXXX@126.com", "XXXXX"); //设置smtp服务器,普通连接方式  
  10. * $mail->setServer("smtp.gmail.com", "XXXXX@gmail.com", "XXXXX", 465, true); //设置smtp服务器,到服务器的SSL连接  
  11. * $mail->setFrom("XXXXX"); //设置发件人  
  12. * $mail->setReceiver("XXXXX"); //设置收件人,多个收件人,调用多次  
  13. * $mail->setCc("XXXX"); //设置抄送,多个抄送,调用多次  
  14. * $mail->setBcc("XXXXX"); //设置秘密抄送,多个秘密抄送,调用多次  
  15. * $mail->addAttachment("XXXX"); //添加附件,多个附件,调用多次  
  16. * $mail->setMail("test", "<b>test</b>"); //设置邮件主题、内容  
  17. * $mail->sendMail(); //发送  
  18. */    
  19. class MySendMail {    
  20.     /**  
  21.     * @var string 邮件传输代理用户名  
  22.     * @access protected  
  23.     */    
  24.     protected $_userName;    
  25.     
  26.     /**  
  27.     * @var string 邮件传输代理密码  
  28.     * @access protected  
  29.     */    
  30.     protected $_password;    
  31.     
  32.     /**  
  33.     * @var string 邮件传输代理服务器地址  
  34.     * @access protected  
  35.     */    
  36.     protected $_sendServer;    
  37.     
  38.     /**  
  39.     * @var int 邮件传输代理服务器端口  
  40.     * @access protected  
  41.     */    
  42.     protected $_port;    
  43.     
  44.     /**  
  45.     * @var string 发件人  
  46.     * @access protected  
  47.     */    
  48.     protected $_from;    
  49.     
  50.     /**  
  51.     * @var array 收件人  
  52.     * @access protected  
  53.     */    
  54.     protected $_to = array();    
  55.     
  56.     /**  
  57.     * @var array 抄送  
  58.     * @access protected  
  59.     */    
  60.     protected $_cc = array();    
  61.     
  62.     /**  
  63.     * @var array 秘密抄送  
  64.     * @access protected  
  65.     */    
  66.     protected $_bcc = array();    
  67.     
  68.     /**  
  69.     * @var string 主题  
  70.     * @access protected  
  71.     */    
  72.     protected $_subject;    
  73.     
  74.     /**  
  75.     * @var string 邮件正文  
  76.     * @access protected  
  77.     */    
  78.     protected $_body;    
  79.     
  80.     /**  
  81.     * @var array 附件  
  82.     * @access protected  
  83.     */    
  84.     protected $_attachment = array();    
  85.     
  86.     /**  
  87.     * @var reource socket资源  
  88.     * @access protected  
  89.     */    
  90.     protected $_socket;    
  91.     
  92.     /**  
  93.     * @var reource 是否是安全连接  
  94.     * @access protected  
  95.     */    
  96.     protected $_isSecurity;    
  97.     
  98.     /**  
  99.     * @var string 错误信息  
  100.     * @access protected  
  101.     */    
  102.     protected $_errorMessage;    
  103.     
  104.     
  105.     /**  
  106.     * 设置邮件传输代理,如果是可以匿名发送有邮件的服务器,只需传递代理服务器地址就行  
  107.     * @access public  
  108.     * @param string $server 代理服务器的ip或者域名  
  109.     * @param string $username 认证账号  
  110.     * @param string $password 认证密码  
  111.     * @param int $port 代理服务器的端口,smtp默认25号端口  
  112.     * @param boolean $isSecurity 到服务器的连接是否为安全连接,默认false  
  113.     * @return boolean  
  114.     */    
  115.     public function setServer($server$username=""$password=""$port=25, $isSecurity=false) {    
  116.         $this->_sendServer = $server;    
  117.         $this->_port = $port;    
  118.         $this->_isSecurity = $isSecurity;    
  119.         $this->_userName = emptyempty($username) ? "" : base64_encode($username);    
  120.         $this->_password = emptyempty($password) ? "" : base64_encode($password);    
  121.         return true;    
  122.     }    
  123.     
  124.     /**  
  125.     * 设置发件人  
  126.     * @access public  
  127.     * @param string $from 发件人地址  
  128.     * @return boolean  
  129.     */    
  130.     public function setFrom($from) {    
  131.         $this->_from = $from;    
  132.         return true;    
  133.     }    
  134.     
  135.     /**  
  136.     * 设置收件人,多个收件人,调用多次.  
  137.     * @access public  
  138.     * @param string $to 收件人地址  
  139.     * @return boolean  
  140.     */    
  141.     public function setReceiver($to) {    
  142.         $this->_to[] = $to;    
  143.         return true;    
  144.     }    
  145.     
  146.     /**  
  147.     * 设置抄送,多个抄送,调用多次.  
  148.     * @access public  
  149.     * @param string $cc 抄送地址  
  150.     * @return boolean  
  151.     */    
  152.     public function setCc($cc) {    
  153.         $this->_cc[] = $cc;    
  154.         return true;    
  155.     }    
  156.     
  157.     /**  
  158.     * 设置秘密抄送,多个秘密抄送,调用多次  
  159.     * @access public  
  160.     * @param string $bcc 秘密抄送地址  
  161.     * @return boolean  
  162.     */    
  163.     public function setBcc($bcc) {    
  164.         $this->_bcc[] = $bcc;    
  165.         return true;    
  166.     }    
  167.     
  168.     /**  
  169.     * 设置邮件附件,多个附件,调用多次  
  170.     * @access public  
  171.     * @param string $file 文件地址  
  172.     * @return boolean  
  173.     */    
  174.     public function addAttachment($file) {    
  175.         if(!file_exists($file)) {    
  176.             $this->_errorMessage = "file " . $file . " does not exist.";    
  177.             return false;    
  178.         }    
  179.         $this->_attachment[] = $file;    
  180.         return true;    
  181.     }    
  182.     
  183.     /**  
  184.     * 设置邮件信息  
  185.     * @access public  
  186.     * @param string $body 邮件主题  
  187.     * @param string $subject 邮件主体内容,可以是纯文本,也可是是HTML文本  
  188.     * @return boolean  
  189.     */    
  190.     public function setMail($subject$body) {    
  191.         $this->_subject = base64_encode($subject);    
  192.         $this->_body = base64_encode($body);    
  193.         return true;    
  194.     }    
  195.     
  196.     /**  
  197.     * 发送邮件  
  198.     * @access public  
  199.     * @return boolean  
  200.     */    
  201.     public function sendMail() {    
  202.         $command = $this->getCommand();    
  203.     
  204.         $this->_isSecurity ? $this->socketSecurity() : $this->socket();    
  205.             
  206.         foreach ($command as $value) {    
  207.             $result = $this->_isSecurity ? $this->sendCommandSecurity($value[0], $value[1]) : $this->sendCommand($value[0], $value[1]);    
  208.             if($result) {    
  209.                 continue;    
  210.             }    
  211.             else{    
  212.                 return false;    
  213.             }    
  214.         }    
  215.             
  216.         //其实这里也没必要关闭,smtp命令:QUIT发出之后,服务器就关闭了连接,本地的socket资源会自动释放    
  217.         $this->_isSecurity ? $this->closeSecutity() : $this->close();    
  218.         return true;    
  219.     }    
  220.     
  221.     /**  
  222.     * 返回错误信息  
  223.     * @return string  
  224.     */    
  225.     public function error(){    
  226.         if(!isset($this->_errorMessage)) {    
  227.             $this->_errorMessage = "";    
  228.         }    
  229.         return $this->_errorMessage;    
  230.     }    
  231.     
  232.     /**  
  233.     * 返回mail命令  
  234.     * @access protected  
  235.     * @return array  
  236.     */    
  237.     protected function getCommand() {    
  238.         $separator = "----=_Part_" . md5($this->_from . time()) . uniqid(); //分隔符    
  239.     
  240.         $command = array(    
  241.                 array("HELO sendmail\r\n", 250)    
  242.             );    
  243.         if(!emptyempty($this->_userName)){    
  244.             $command[] = array("AUTH LOGIN\r\n", 334);    
  245.             $command[] = array($this->_userName . "\r\n", 334);    
  246.             $command[] = array($this->_password . "\r\n", 235);    
  247.         }    
  248.     
  249.         //设置发件人    
  250.         $command[] = array("MAIL FROM: <" . $this->_from . ">\r\n", 250);    
  251.         $header = "FROM: <" . $this->_from . ">\r\n";    
  252.     
  253.         //设置收件人    
  254.         if(!emptyempty($this->_to)) {    
  255.             $count = count($this->_to);    
  256.             if($count == 1){    
  257.                 $command[] = array("RCPT TO: <" . $this->_to[0] . ">\r\n", 250);    
  258.                 $header .= "TO: <" . $this->_to[0] .">\r\n";    
  259.             }    
  260.             else{    
  261.                 for($i=0; $i<$count$i++){    
  262.                     $command[] = array("RCPT TO: <" . $this->_to[$i] . ">\r\n", 250);    
  263.                     if($i == 0){    
  264.                         $header .= "TO: <" . $this->_to[$i] .">";    
  265.                     }    
  266.                     elseif($i + 1 == $count){    
  267.                         $header .= ",<" . $this->_to[$i] .">\r\n";    
  268.                     }    
  269.                     else{    
  270.                         $header .= ",<" . $this->_to[$i] .">";    
  271.                     }    
  272.                 }    
  273.             }    
  274.         }    
  275.     
  276.         //设置抄送    
  277.         if(!emptyempty($this->_cc)) {    
  278.             $count = count($this->_cc);    
  279.             if($count == 1){    
  280.                 $command[] = array("RCPT TO: <" . $this->_cc[0] . ">\r\n", 250);    
  281.                 $header .= "CC: <" . $this->_cc[0] .">\r\n";    
  282.             }    
  283.             else{    
  284.                 for($i=0; $i<$count$i++){    
  285.                     $command[] = array("RCPT TO: <" . $this->_cc[$i] . ">\r\n", 250);    
  286.                     if($i == 0){    
  287.                     $header .= "CC: <" . $this->_cc[$i] .">";    
  288.                     }    
  289.                     elseif($i + 1 == $count){    
  290.                         $header .= ",<" . $this->_cc[$i] .">\r\n";    
  291.                     }    
  292.                     else{    
  293.                         $header .= ",<" . $this->_cc[$i] .">";    
  294.                     }    
  295.                 }    
  296.             }    
  297.         }    
  298.     
  299.         //设置秘密抄送    
  300.         if(!emptyempty($this->_bcc)) {    
  301.             $count = count($this->_bcc);    
  302.             if($count == 1) {    
  303.                 $command[] = array("RCPT TO: <" . $this->_bcc[0] . ">\r\n", 250);    
  304.                 $header .= "BCC: <" . $this->_bcc[0] .">\r\n";    
  305.             }    
  306.             else{    
  307.                 for($i=0; $i<$count$i++){    
  308.                     $command[] = array("RCPT TO: <" . $this->_bcc[$i] . ">\r\n", 250);    
  309.                     if($i == 0){    
  310.                     $header .= "BCC: <" . $this->_bcc[$i] .">";    
  311.                     }    
  312.                     elseif($i + 1 == $count){    
  313.                         $header .= ",<" . $this->_bcc[$i] .">\r\n";    
  314.                     }    
  315.                     else{    
  316.                         $header .= ",<" . $this->_bcc[$i] .">";    
  317.                     }    
  318.                 }    
  319.             }    
  320.         }    
  321.     
  322.         //主题    
  323.         $header .= "Subject: =?UTF-8?B?" . $this->_subject ."?=\r\n";    
  324.         if(isset($this->_attachment)) {    
  325.             //含有附件的邮件头需要声明成这个    
  326.             $header .= "Content-Type: multipart/mixed;\r\n";    
  327.         }    
  328.         elseif(false){    
  329.             //邮件体含有图片资源的,且包含的图片在邮件内部时声明成这个,如果是引用的远程图片,就不需要了    
  330.             $header .= "Content-Type: multipart/related;\r\n";    
  331.         }    
  332.         else{    
  333.             //html或者纯文本的邮件声明成这个    
  334.             $header .= "Content-Type: multipart/alternative;\r\n";    
  335.         }    
  336.     
  337.         //邮件头分隔符    
  338.         $header .= "\t" . 'boundary="' . $separator . '"';    
  339.     
  340.         $header .= "\r\nMIME-Version: 1.0\r\n";    
  341.     
  342.         //这里开始是邮件的body部分,body部分分成几段发送    
  343.         $header .= "\r\n--" . $separator . "\r\n";    
  344.         $header .= "Content-Type:text/html; charset=utf-8\r\n";    
  345.         $header .= "Content-Transfer-Encoding: base64\r\n\r\n";    
  346.         $header .= $this->_body . "\r\n";    
  347.         $header .= "--" . $separator . "\r\n";    
  348.     
  349.         //加入附件    
  350.         if(!emptyempty($this->_attachment)){    
  351.             $count = count($this->_attachment);    
  352.             for($i=0; $i<$count$i++){    
  353.                 $header .= "\r\n--" . $separator . "\r\n";    
  354.                 $header .= "Content-Type: " . $this->getMIMEType($this->_attachment[$i]) . '; name="=?UTF-8?B?' . base64_encodebasename($this->_attachment[$i]) ) . '?="' . "\r\n";    
  355.                 $header .= "Content-Transfer-Encoding: base64\r\n";    
  356.                 $header .= 'Content-Disposition: attachment; filename="=?UTF-8?B?' . base64_encodebasename($this->_attachment[$i]) ) . '?="' . "\r\n";    
  357.                 $header .= "\r\n";    
  358.                 $header .= $this->readFile($this->_attachment[$i]);    
  359.                 $header .= "\r\n--" . $separator . "\r\n";    
  360.             }    
  361.         }    
  362.     
  363.         //结束邮件数据发送    
  364.         $header .= "\r\n.\r\n";    
  365.     
  366.     
  367.         $command[] = array("DATA\r\n", 354);    
  368.         $command[] = array($header, 250);    
  369.         $command[] = array("QUIT\r\n", 221);    
  370.             
  371.         return $command;    
  372.     }    
  373.     
  374.     /**  
  375.     * 发送命令  
  376.     * @access protected  
  377.     * @param string $command 发送到服务器的smtp命令  
  378.     * @param int $code 期望服务器返回的响应吗  
  379.     * @return boolean  
  380.     */    
  381.     protected function sendCommand($command$code) {    
  382.         echo 'Send command:' . $command . ',expected code:' . $code . '<br />';    
  383.         //发送命令给服务器    
  384.         try{    
  385.             if(socket_write($this->_socket, $commandstrlen($command))){    
  386.     
  387.                 //当邮件内容分多次发送时,没有$code,服务器没有返回    
  388.                 if(emptyempty($code))  {    
  389.                     return true;    
  390.                 }    
  391.     
  392.                 //读取服务器返回    
  393.                 $data = trim(socket_read($this->_socket, 1024));    
  394.                 echo 'response:' . $data . '<br /><br />';    
  395.     
  396.                 if($data) {    
  397.                     $pattern = "/^".$code."+?/";    
  398.                     if(preg_match($pattern$data)) {    
  399.                         return true;    
  400.                     }    
  401.                     else{    
  402.                         $this->_errorMessage = "Error:" . $data . "|**| command:";    
  403.                         return false;    
  404.                     }    
  405.                 }    
  406.                 else{    
  407.                     $this->_errorMessage = "Error:" . socket_strerror(socket_last_error());    
  408.                     return false;    
  409.                 }    
  410.             }    
  411.             else{    
  412.                 $this->_errorMessage = "Error:" . socket_strerror(socket_last_error());    
  413.                 return false;    
  414.             }    
  415.         }catch(Exception $e) {    
  416.             $this->_errorMessage = "Error:" . $e->getMessage();    
  417.         }    
  418.     }    
  419.     
  420.     /**  
  421.     * 安全连接发送命令  
  422.     * @access protected  
  423.     * @param string $command 发送到服务器的smtp命令  
  424.     * @param int $code 期望服务器返回的响应吗  
  425.     * @return boolean  
  426.     */    
  427.     protected function sendCommandSecurity($command$code) {    
  428.         echo 'Send command:' . $command . ',expected code:' . $code . '<br />';    
  429.         try {    
  430.             if(fwrite($this->_socket, $command)){    
  431.                 //当邮件内容分多次发送时,没有$code,服务器没有返回    
  432.                 if(emptyempty($code))  {    
  433.                     return true;    
  434.                 }    
  435.                 //读取服务器返回    
  436.                 $data = trim(fread($this->_socket, 1024));    
  437.                 echo 'response:' . $data . '<br /><br />';    
  438.     
  439.                 if($data) {    
  440.                     $pattern = "/^".$code."+?/";    
  441.                     if(preg_match($pattern$data)) {    
  442.                         return true;    
  443.                     }    
  444.                     else{    
  445.                         $this->_errorMessage = "Error:" . $data . "|**| command:";    
  446.                         return false;    
  447.                     }    
  448.                 }    
  449.                 else{    
  450.                     return false;    
  451.                 }    
  452.             }    
  453.             else{    
  454.                 $this->_errorMessage = "Error: " . $command . " send failed";    
  455.                 return false;    
  456.             }    
  457.         }catch(Exception $e) {    
  458.             $this->_errorMessage = "Error:" . $e->getMessage();    
  459.         }    
  460.     }     
  461.     
  462.     /**  
  463.     * 读取附件文件内容,返回base64编码后的文件内容  
  464.     * @access protected  
  465.     * @param string $file 文件  
  466.     * @return mixed  
  467.     */    
  468.     protected function readFile($file) {    
  469.         if(file_exists($file)) {    
  470.             $file_obj = file_get_contents($file);    
  471.             return base64_encode($file_obj);    
  472.         }    
  473.         else {    
  474.             $this->_errorMessage = "file " . $file . " dose not exist";    
  475.             return false;    
  476.         }    
  477.     }    
  478.     
  479.     /**  
  480.     * 获取附件MIME类型  
  481.     * @access protected  
  482.     * @param string $file 文件  
  483.     * @return mixed  
  484.     */    
  485.     protected function getMIMEType($file) {    
  486.         if(file_exists($file)) {    
  487.             $mime = mime_content_type($file);    
  488.             /*if(! preg_match("/gif|jpg|png|jpeg/", $mime)){  
  489.                 $mime = "application/octet-stream";  
  490.             }*/    
  491.             return $mime;    
  492.         }    
  493.         else {    
  494.             return false;    
  495.         }    
  496.     }    
  497.     
  498.     /**  
  499.     * 建立到服务器的网络连接  
  500.     * @access protected  
  501.     * @return boolean  
  502.     */    
  503.     protected function socket() {    
  504.         //创建socket资源    
  505.         $this->_socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));    
  506.             
  507.         if(!$this->_socket) {    
  508.             $this->_errorMessage = socket_strerror(socket_last_error());    
  509.             return false;    
  510.         }    
  511.     
  512.         socket_set_block($this->_socket);//设置阻塞模式    
  513.     
  514.         //连接服务器    
  515.         if(!socket_connect($this->_socket, $this->_sendServer, $this->_port)) {    
  516.             $this->_errorMessage = socket_strerror(socket_last_error());    
  517.             return false;    
  518.         }    
  519.         $str = socket_read($this->_socket, 1024);    
  520.         if(!preg_match("/220+?/"$str)){    
  521.             $this->_errorMessage = $str;    
  522.             return false;    
  523.         }    
  524.             
  525.         return true;    
  526.     }    
  527.     
  528.     /**  
  529.     * 建立到服务器的SSL网络连接  
  530.     * @access protected  
  531.     * @return boolean  
  532.     */    
  533.     protected function socketSecurity() {    
  534.         $remoteAddr = "tcp://" . $this->_sendServer . ":" . $this->_port;    
  535.         $this->_socket = stream_socket_client($remoteAddr$errno$errstr, 30);    
  536.         if(!$this->_socket){    
  537.             $this->_errorMessage = $errstr;    
  538.             return false;    
  539.         }    
  540.     
  541.         //设置加密连接,默认是ssl,如果需要tls连接,可以查看php手册stream_socket_enable_crypto函数的解释    
  542.         stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);    
  543.     
  544.         stream_set_blocking($this->_socket, 1); //设置阻塞模式    
  545.         $str = fread($this->_socket, 1024);    
  546.         if(!preg_match("/220+?/"$str)){    
  547.             $this->_errorMessage = $str;    
  548.             return false;    
  549.         }    
  550.     
  551.         return true;    
  552.     }    
  553.     
  554.     /**  
  555.     * 关闭socket  
  556.     * @access protected  
  557.     * @return boolean  
  558.     */    
  559.     protected function close() {    
  560.         if(isset($this->_socket) && is_object($this->_socket)) {    
  561.             $this->_socket->close();    
  562.             return true;    
  563.         }    
  564.         $this->_errorMessage = "No resource can to be close";    
  565.         return false;    
  566.     }    
  567.     
  568.     /**  
  569.     * 关闭安全socket  
  570.     * @access protected  
  571.     * @return boolean  
  572.     */    
  573.     protected function closeSecutity() {    
  574.         if(isset($this->_socket) && is_object($this->_socket)) {    
  575.             stream_socket_shutdown($this->_socket, STREAM_SHUT_WR);    
  576.             return true;    
  577.         }    
  578.         $this->_errorMessage = "No resource can to be close";    
  579.         return false;    
  580.     }    
  581. }     
  582. ?>   

 QQ邮箱已测试可用,另附录QQ邮箱的SMTP地址:

地址:smtp.qq.com 端口号:465 必须SSL连接。

 

Tags: php

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 5854

php网页图片采集

 

PHP代码
  1. <?php  
  2. header('Content-Type:text/html;charset=utf-8');  
  3.   
  4. /** 
  5.  * 一个用于抓去图片的类 
  6.  * @author tangpan <tang0pan@qq.com> 
  7.  */  
  8. class download_image {  
  9.     public $_save_path = NULL; //图片保存路劲  
  10.     public $_limit_size = NULL; //图片限制大小  
  11.     public static $_img_url_old = array();  //存储已抓取过的图片链接地址  
  12.     public static $_a_page_url = array();    //存储抓取过的页面  
  13.     public function __construct( $_save_path$_limit_size) {  
  14.         $this->_save_path = $_save_path;  
  15.         $this->_limit_size = $_limit_size;  
  16.     }  
  17.       
  18.     public function get_all_page_image( $site_url ) {  
  19.         if ( $site_url == '' ) {  
  20.             return false;  
  21.         }  
  22.         if ( ! in_array( $site_url, self::$_a_page_url ) ) {  //判断当前页面是否抓取过  
  23.             self::$_a_page_url[] = $site_url;   //将超链接存入静态数组中  
  24.         } else {  
  25.             return;     //若抓取过则跳出  
  26.         }  
  27.         $this->download_the_page_image( $site_url );  
  28.         $content = @file_get_contents($site_url);  
  29.         $a_page_url = "|<a[^>]+href=['\" ]?([^ '\"?]+)['\" >]|U";  
  30.         $all_url = array();  
  31.         preg_match_all( $a_page_url$content$all_url, PREG_SET_ORDER );  
  32.         if ( $all_url != NULL ) {  
  33.             foreach$all_url as $key => $val ) {  
  34.                 /** 
  35.                  * 静态化超链接,防止进入死循环 
  36.                  * 出去当前页面链接表示方式('','#','/') 
  37.                  */  
  38.                 if ( trim($val[1]) != '' && ! in_array( $val[1], self::$_a_page_url ) && ! in_array( $val[1], array('#','/',$site_url) ) ) { 
  39.                     self::$_a_page_url[] = $val[1];   //将符合要求的超链接写入静态数组中 
  40.                 }  
  41.             } 
  42.         } 
  43.         if ( self::$_a_page_url != NULL ) { 
  44.             foreach( self::$_a_page_url as $keys => $vals ) { 
  45.                 if ( strpos( $vals, 'http://' ) === false ) { //超链接不包含http://时,不能直接访问  
  46.                     // 当图片链接地址为相对地址是重新拼凑地址  
  47.                     $a_domain_url = substr$site_url, 0, strpos$site_url, '/',8 ) + 1 ); 
  48.                     $a_img_url = $a_domain_url.$vals;  
  49.                 } 
  50.                 //递归调用,访问每一个超链接页面 
  51.                 $this->get_all_page_image( $a_img_url ); 
  52.             } 
  53.         } 
  54.     } 
  55.      
  56.     /** 
  57.      * 下载当前页面下的所有图片链接 
  58.      * @param $site_url   <页面地址> 
  59.      */ 
  60.     public function download_the_page_image( $site_url ) { 
  61.         // 获取当前链接地址页面的所有内容 
  62.         $img_pattern = NULL; 
  63.         $content = @file_get_contents( $site_url ); 
  64.         $img_pattern = "|<img[^>]+src=['\" ]?([^ '\"?]+)['\" >]|U";  
  65.         //全局匹配所有的<img >中的图片链接  
  66.         $img_out = array();  
  67.         preg_match_all( $img_pattern$content$img_out, PREG_SET_ORDER );  
  68.         echo '<h1>'$site_url . '共找到' . count($img_out) . '张图片</h1>';  
  69.         //print_r($img_out[1]);  
  70.         foreach$img_out as $key => $val ) {  
  71.             //echo htmlspecialchars($val[1]).'<br />';  
  72.             $this->save_one_image( $site_url$val[1]);  
  73.         }  
  74.           
  75.     }  
  76.       
  77.     public function save_one_image( $site_url$img_url ) {  
  78.         if ( strpos$img_url'http://' ) === false ) {  
  79.             // 当图片链接地址为相对地址是重新拼凑地址  
  80.             $domain_url = substr$site_url, 0, strpos$site_url'/',8 ) + 1 );  
  81.             $img_url = $domain_url.$img_url;   
  82.         }  
  83.         $pic_name = basename$img_url ); //获取图片名称  
  84.           
  85.         if ( in_array( $img_url, self::$_img_url_old ) ) {  
  86.             echo $img_url .'<span style="color:red;margin-left:50px">该图片已经抓取过!</span><br/>';  
  87.             return;  
  88.         }  
  89.         //获取图片内容,并写入一个字符串  
  90.         $img_data = @file_get_contents$img_url );  
  91.         if ( strlen($img_data) < $this->_limit_size ) { //图片大小在限制范围内  
  92.             $img_boo = @file_put_contents$this->_save_path.md5(microtime()).$pic_name$img_data );  
  93.             if ( $img_boo ) {  
  94.                 echo $img_url .'<span style="color:green;margin-left:50px;">图片保存成功!</span><br/>';  
  95.                 self::$_img_url_old[] = $img_url;  
  96.             } else {  
  97.                 echo $img_url .'<span style="color:red;margin-left:50px;">图片保存失败!</span><br />';  
  98.             }  
  99.         } else {  
  100.             echo $img_url .'<span style="color:red;margin-left:50px;">图片大小在限制范围之外!</span><br />';  
  101.         }  
  102.     }  
  103. }  
  104. set_time_limit(0);  
  105. $download_images = new download_image('surces_Img/',1024*1024*100);  
  106. $download_images->get_all_page_image('http://www.22mm.cc/');  
  107. ?>  

 

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 1749

 

PHP代码
  1. function convToUtf8($str) {  
  2.     if( mb_detect_encoding($str,"UTF-8, ISO-8859-1, GBK")!="UTF-8" ) {//判断是否不是UTF-8编码,如果不是UTF-8编码,则转换为UTF-8编码  
  3.         return  iconv("gbk","utf-8",$str);  
  4.     } else {  
  5.         return $str;  
  6.     }  
  7. }  

 

Tags: php

分类:技术文章 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 2520

 广告位

↑返回顶部↑