WebBrowser与IE的关系,如何设置WebBrowser工作在IE9模式下?
作者:admin 日期:2014-12-02
一.问题的提出
偶然发现,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
VB中webbrowser调用IE版本问题
作者:admin 日期:2014-04-09
如何让WebBrowser引用IE 8 呢。可以通过注册表的新增键值实现
打开HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Internet Explorer\MAIN\FeatureControl\FEATURE_BROWSER_EMULATION
php输出json格式数据
作者:admin 日期:2014-03-06
php有道翻译api调用方法
作者:admin 日期:2014-03-06
PHP Curl 模拟POST 可以https
作者:admin 日期:2014-03-06
- function vpost($url,$data,$cookie){ // 模拟提交数据函数
- $curl = curl_init(); // 启动一个CURL会话
- curl_setopt($curl, CURLOPT_URL, $url); // 要访问的地址
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 从证书中检查SSL加密算法是否存在
- curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模拟用户使用的浏览器
- curl_setopt($curl, CURLOPT_COOKIE, $cookie);
- curl_setopt($curl, CURLOPT_REFERER,'https://www.baidu.com');// 设置Referer
- curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
- curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 设置超时限制防止死循环
- curl_setopt($curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
- $tmpInfo = curl_exec($curl); // 执行操作
- if (curl_errno($curl)) {
- echo 'Errno'.curl_error($curl);//捕抓异常
- }
- curl_close($curl); // 关闭CURL会话
- return $tmpInfo; // 返回数据
- }
PHP利用CURL进行POST数据上传
作者:admin 日期:2014-03-06
- ////二纬码
- $QRCode_URL="https://api.weixin.qq.com/cgi-bin/qrcode/create?access_token=".$ACC_TOKEN;
- $data ='{"expire_seconds": 1800, "action_name": "QR_SCENE", "action_info": {"scene": {"scene_id": 123}}} ';
- /*
- $ch = curl_init($MENU_URL);
- curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
- curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
- curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json','Content-Length:'.strlen($data)));
- $info = curl_exec($ch);
- */
- function post($url, $params = false, $header = array()){
- $ch = curl_init();
- $cookieFile = 'sdadsd_cookiejar.txt';
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 60);
- curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFile);
- curl_setopt($ch, CURLOPT_COOKIEFILE,$cookieFile);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
- curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
- curl_setopt($ch, CURLOPT_HTTPGET, true);
- curl_setopt($ch, CURLOPT_TIMEOUT, 30);
- if($params !== false){ curl_setopt($ch, CURLOPT_POSTFIELDS , $params);}
- curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (Windows NT 5.1; rv:21.0) Gecko/20100101 Firefox/21.0');
- curl_setopt($ch, CURLOPT_URL,$url);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
- $result = curl_exec($ch);
- curl_close($ch);
- return $result;
- }
- $result = post($QRCode_URL,$data);
php web下载网站代码
作者:admin 日期:2014-03-06
- <?php
- /**
- * @author MarkZhao(zhaody901#126.com)
- */
- $dir = dirname(__FILE__).'/';
- // zip and download
- // zip.php?type=zip&dir=subdir
- // unzip deflate way zipped zip file
- // zip.php?type=unzip&name=deflate
- $type = $_GET['type'];
- $type or exit('die no type to do!');
- $name = isset($_GET['name']) ? $_GET['name'].'.zip' : 'zip.zip';
- $subdir = $_GET['dir'];
- $subdir or die('no sub dir to zip');
- set_time_limit(0);
- if ($type=='zip') {
- $zip = new ZipArchive();
- if ($zip->open($name, ZipArchive::Create) === TRUE) {
- $files = map($dir.$subdir);
- foreach ($files as $file) {
- $dir = str_replace('\\', '/', $dir);
- $file = str_replace('\\', '/', $file);
- $zip->addFile($file, '_ROOT_/' . str_replace($dir, '', $file));
- }
- $zip->close();
- down($dir.$name);
- echo 'ok';
- } else {
- echo 'failed';
- }
- } else if ($type=='unzip') {
- $zip = new ZipArchive();
- if ($zip->open($name) === TRUE) {
- $zip->extractTo(dirname(__FILE__).'/');
- $zip->close();
- echo 'ok';
- } else {
- echo 'failed';
- }
- }
- function map($directory) {
- $files = array();
- if (file_exists($directory) && is_dir($directory)) {
- foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory)) as $file) {
- $files[] = $file->getRealPath();
- }
- }
- return $files;
- }
- function down($file) {
- $filename = $filename ? $filename : basename($file);
- $filesize = filesize($file);
- ob_end_clean();
- @set_time_limit(0);
- if (strpos($_SERVER['HTTP_USER_AGENT'], 'MSIE') !== false) {
- header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
- header('Pragma: public');
- } else {
- header('Pragma: no-cache');
- }
- header('Expires: ' . gmdate('D, d M Y H:i:s') . ' GMT');
- header('Content-Encoding: none');
- header('Content-Length: ' . $filesize);
- header('Content-Disposition: attachment; filename=' . $filename);
- header('Content-Type: ' . $filetype);
- readfile($file);
- exit;
- }
1. 打包代码, 并直接通过浏览器下载
2. 解压zip文件到服务器
php合成或者创建gif动画
作者:admin 日期:2014-03-06
1. 首先需要确认GD库是否正常。
2. 如果是合成图片,请确保把分解的图片放在frames的文件夹里面。
3. GIFEncoder.class.php 类
- <?php
- include "GIFEncoder.class.php";
- ob_start();
- $board_width = 60;
- $board_height = 60;
- $pad_width = 5;
- $pad_height = 15;
- $ball_size = 5;
- $game_width = $board_width - $pad_width*2 - $ball_size;
- $game_height = $board_height-$ball_size;
- $x = 0;
- $y = rand(0,$game_height);
- $xv = rand(1,10);
- $yv = rand(1,10);
- $pt[] = array($x,$y);
- do{
- $x += $xv;
- $y += $yv;
- if($x > $game_width){
- $xv = -1*$xv;
- $x = $game_width - ($x-$game_width);
- }elseif($x < 0){
- $xv = -1*$xv;
- $x = abs($x);
- }
- if($y>$game_height){
- $yv = -1*$yv;
- $y = $game_height - ($y - $game_height);
- }elseif($y<0){
- $yv = -1*$yv;
- $y = abs($y);
- }
- $pt[] = array($x,$y);
- }while($x!=$pt[0][0]||$y!=$pt[0][1]);
- $i = 0;
- while(isset($pt[$i])){
- $image = imagecreate($board_width,$board_height);
- imagecolorallocate($image, 0,0,0);
- $color = imagecolorallocate($image, 255,255,255);
- $color2 = imagecolorallocate($image, 255,0,0);
- if($pt[$i][1] + $pad_height < $board_width){
- imagefilledrectangle($image,0,$pt[$i][1],$pad_width, $pt[$i][1]+$pad_height,$color);
- }else{
- imagefilledrectangle($image,0,$board_width-$pad_height,$pad_width, $board_width,$color);
- }
- imagefilledrectangle($image,$board_width-$pad_width,0,$board_width, $board_height,$color2);
- 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);
- //imagesetpixel($image,$pt[$i][0],$pt[$i][1],$color);
- imagegif($image);
- imagedestroy($image);
- $imagedata[] = ob_get_contents();
- ob_clean();
- ++$i;
- }
- $gif = new GIFEncoder(
- $imagedata,
- 100,
- 0,
- 2,
- 0, 0, 1,
- "bin"
- );
- Header ('Content-type:image/gif');
- echo $gif->GetAnimation();
- ?>
php识别电脑还是手机访问网站
作者:admin 日期:2014-03-06
- <?php
- function isMobile(){
- $useragent=isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : '';
- $useragent_commentsblock=preg_match('|\(.*?\)|',$useragent,$matches)>0?$matches[0]:'';
- function CheckSubstrs($substrs,$text){
- foreach($substrs as $substr)
- if(false!==strpos($text,$substr)){
- return true;
- }
- return false;
- }
- $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');
- $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');
- $found_mobile=CheckSubstrs($mobile_os_list,$useragent_commentsblock) ||
- CheckSubstrs($mobile_token_list,$useragent);
- if ($found_mobile){
- return true;
- }else{
- return false;
- }
- }
- if (isMobile())
- echo '手机登录www.cuangs.com';
- else
- echo '电脑登录www.ydutv.com';
- ?>
PHP SMTP邮件发送类,支持SSL连接
作者:admin 日期:2014-03-06
- <?php
- /**
- * 邮件发送类
- * 支持发送纯文本邮件和HTML格式的邮件,可以多收件人,多抄送,多秘密抄送,带附件(单个或多个附件),支持到服务器的ssl连接
- * 需要的php扩展:sockets、Fileinfo和openssl。
- * 编码格式是UTF-8,传输编码格式是base64
- * @example
- * $mail = new MySendMail();
- * $mail->setServer("smtp@126.com", "XXXXX@126.com", "XXXXX"); //设置smtp服务器,普通连接方式
- * $mail->setServer("smtp.gmail.com", "XXXXX@gmail.com", "XXXXX", 465, true); //设置smtp服务器,到服务器的SSL连接
- * $mail->setFrom("XXXXX"); //设置发件人
- * $mail->setReceiver("XXXXX"); //设置收件人,多个收件人,调用多次
- * $mail->setCc("XXXX"); //设置抄送,多个抄送,调用多次
- * $mail->setBcc("XXXXX"); //设置秘密抄送,多个秘密抄送,调用多次
- * $mail->addAttachment("XXXX"); //添加附件,多个附件,调用多次
- * $mail->setMail("test", "<b>test</b>"); //设置邮件主题、内容
- * $mail->sendMail(); //发送
- */
- class MySendMail {
- /**
- * @var string 邮件传输代理用户名
- * @access protected
- */
- protected $_userName;
- /**
- * @var string 邮件传输代理密码
- * @access protected
- */
- protected $_password;
- /**
- * @var string 邮件传输代理服务器地址
- * @access protected
- */
- protected $_sendServer;
- /**
- * @var int 邮件传输代理服务器端口
- * @access protected
- */
- protected $_port;
- /**
- * @var string 发件人
- * @access protected
- */
- protected $_from;
- /**
- * @var array 收件人
- * @access protected
- */
- protected $_to = array();
- /**
- * @var array 抄送
- * @access protected
- */
- protected $_cc = array();
- /**
- * @var array 秘密抄送
- * @access protected
- */
- protected $_bcc = array();
- /**
- * @var string 主题
- * @access protected
- */
- protected $_subject;
- /**
- * @var string 邮件正文
- * @access protected
- */
- protected $_body;
- /**
- * @var array 附件
- * @access protected
- */
- protected $_attachment = array();
- /**
- * @var reource socket资源
- * @access protected
- */
- protected $_socket;
- /**
- * @var reource 是否是安全连接
- * @access protected
- */
- protected $_isSecurity;
- /**
- * @var string 错误信息
- * @access protected
- */
- protected $_errorMessage;
- /**
- * 设置邮件传输代理,如果是可以匿名发送有邮件的服务器,只需传递代理服务器地址就行
- * @access public
- * @param string $server 代理服务器的ip或者域名
- * @param string $username 认证账号
- * @param string $password 认证密码
- * @param int $port 代理服务器的端口,smtp默认25号端口
- * @param boolean $isSecurity 到服务器的连接是否为安全连接,默认false
- * @return boolean
- */
- public function setServer($server, $username="", $password="", $port=25, $isSecurity=false) {
- $this->_sendServer = $server;
- $this->_port = $port;
- $this->_isSecurity = $isSecurity;
- $this->_userName = emptyempty($username) ? "" : base64_encode($username);
- $this->_password = emptyempty($password) ? "" : base64_encode($password);
- return true;
- }
- /**
- * 设置发件人
- * @access public
- * @param string $from 发件人地址
- * @return boolean
- */
- public function setFrom($from) {
- $this->_from = $from;
- return true;
- }
- /**
- * 设置收件人,多个收件人,调用多次.
- * @access public
- * @param string $to 收件人地址
- * @return boolean
- */
- public function setReceiver($to) {
- $this->_to[] = $to;
- return true;
- }
- /**
- * 设置抄送,多个抄送,调用多次.
- * @access public
- * @param string $cc 抄送地址
- * @return boolean
- */
- public function setCc($cc) {
- $this->_cc[] = $cc;
- return true;
- }
- /**
- * 设置秘密抄送,多个秘密抄送,调用多次
- * @access public
- * @param string $bcc 秘密抄送地址
- * @return boolean
- */
- public function setBcc($bcc) {
- $this->_bcc[] = $bcc;
- return true;
- }
- /**
- * 设置邮件附件,多个附件,调用多次
- * @access public
- * @param string $file 文件地址
- * @return boolean
- */
- public function addAttachment($file) {
- if(!file_exists($file)) {
- $this->_errorMessage = "file " . $file . " does not exist.";
- return false;
- }
- $this->_attachment[] = $file;
- return true;
- }
- /**
- * 设置邮件信息
- * @access public
- * @param string $body 邮件主题
- * @param string $subject 邮件主体内容,可以是纯文本,也可是是HTML文本
- * @return boolean
- */
- public function setMail($subject, $body) {
- $this->_subject = base64_encode($subject);
- $this->_body = base64_encode($body);
- return true;
- }
- /**
- * 发送邮件
- * @access public
- * @return boolean
- */
- public function sendMail() {
- $command = $this->getCommand();
- $this->_isSecurity ? $this->socketSecurity() : $this->socket();
- foreach ($command as $value) {
- $result = $this->_isSecurity ? $this->sendCommandSecurity($value[0], $value[1]) : $this->sendCommand($value[0], $value[1]);
- if($result) {
- continue;
- }
- else{
- return false;
- }
- }
- //其实这里也没必要关闭,smtp命令:QUIT发出之后,服务器就关闭了连接,本地的socket资源会自动释放
- $this->_isSecurity ? $this->closeSecutity() : $this->close();
- return true;
- }
- /**
- * 返回错误信息
- * @return string
- */
- public function error(){
- if(!isset($this->_errorMessage)) {
- $this->_errorMessage = "";
- }
- return $this->_errorMessage;
- }
- /**
- * 返回mail命令
- * @access protected
- * @return array
- */
- protected function getCommand() {
- $separator = "----=_Part_" . md5($this->_from . time()) . uniqid(); //分隔符
- $command = array(
- array("HELO sendmail\r\n", 250)
- );
- if(!emptyempty($this->_userName)){
- $command[] = array("AUTH LOGIN\r\n", 334);
- $command[] = array($this->_userName . "\r\n", 334);
- $command[] = array($this->_password . "\r\n", 235);
- }
- //设置发件人
- $command[] = array("MAIL FROM: <" . $this->_from . ">\r\n", 250);
- $header = "FROM: <" . $this->_from . ">\r\n";
- //设置收件人
- if(!emptyempty($this->_to)) {
- $count = count($this->_to);
- if($count == 1){
- $command[] = array("RCPT TO: <" . $this->_to[0] . ">\r\n", 250);
- $header .= "TO: <" . $this->_to[0] .">\r\n";
- }
- else{
- for($i=0; $i<$count; $i++){
- $command[] = array("RCPT TO: <" . $this->_to[$i] . ">\r\n", 250);
- if($i == 0){
- $header .= "TO: <" . $this->_to[$i] .">";
- }
- elseif($i + 1 == $count){
- $header .= ",<" . $this->_to[$i] .">\r\n";
- }
- else{
- $header .= ",<" . $this->_to[$i] .">";
- }
- }
- }
- }
- //设置抄送
- if(!emptyempty($this->_cc)) {
- $count = count($this->_cc);
- if($count == 1){
- $command[] = array("RCPT TO: <" . $this->_cc[0] . ">\r\n", 250);
- $header .= "CC: <" . $this->_cc[0] .">\r\n";
- }
- else{
- for($i=0; $i<$count; $i++){
- $command[] = array("RCPT TO: <" . $this->_cc[$i] . ">\r\n", 250);
- if($i == 0){
- $header .= "CC: <" . $this->_cc[$i] .">";
- }
- elseif($i + 1 == $count){
- $header .= ",<" . $this->_cc[$i] .">\r\n";
- }
- else{
- $header .= ",<" . $this->_cc[$i] .">";
- }
- }
- }
- }
- //设置秘密抄送
- if(!emptyempty($this->_bcc)) {
- $count = count($this->_bcc);
- if($count == 1) {
- $command[] = array("RCPT TO: <" . $this->_bcc[0] . ">\r\n", 250);
- $header .= "BCC: <" . $this->_bcc[0] .">\r\n";
- }
- else{
- for($i=0; $i<$count; $i++){
- $command[] = array("RCPT TO: <" . $this->_bcc[$i] . ">\r\n", 250);
- if($i == 0){
- $header .= "BCC: <" . $this->_bcc[$i] .">";
- }
- elseif($i + 1 == $count){
- $header .= ",<" . $this->_bcc[$i] .">\r\n";
- }
- else{
- $header .= ",<" . $this->_bcc[$i] .">";
- }
- }
- }
- }
- //主题
- $header .= "Subject: =?UTF-8?B?" . $this->_subject ."?=\r\n";
- if(isset($this->_attachment)) {
- //含有附件的邮件头需要声明成这个
- $header .= "Content-Type: multipart/mixed;\r\n";
- }
- elseif(false){
- //邮件体含有图片资源的,且包含的图片在邮件内部时声明成这个,如果是引用的远程图片,就不需要了
- $header .= "Content-Type: multipart/related;\r\n";
- }
- else{
- //html或者纯文本的邮件声明成这个
- $header .= "Content-Type: multipart/alternative;\r\n";
- }
- //邮件头分隔符
- $header .= "\t" . 'boundary="' . $separator . '"';
- $header .= "\r\nMIME-Version: 1.0\r\n";
- //这里开始是邮件的body部分,body部分分成几段发送
- $header .= "\r\n--" . $separator . "\r\n";
- $header .= "Content-Type:text/html; charset=utf-8\r\n";
- $header .= "Content-Transfer-Encoding: base64\r\n\r\n";
- $header .= $this->_body . "\r\n";
- $header .= "--" . $separator . "\r\n";
- //加入附件
- if(!emptyempty($this->_attachment)){
- $count = count($this->_attachment);
- for($i=0; $i<$count; $i++){
- $header .= "\r\n--" . $separator . "\r\n";
- $header .= "Content-Type: " . $this->getMIMEType($this->_attachment[$i]) . '; name="=?UTF-8?B?' . base64_encode( basename($this->_attachment[$i]) ) . '?="' . "\r\n";
- $header .= "Content-Transfer-Encoding: base64\r\n";
- $header .= 'Content-Disposition: attachment; filename="=?UTF-8?B?' . base64_encode( basename($this->_attachment[$i]) ) . '?="' . "\r\n";
- $header .= "\r\n";
- $header .= $this->readFile($this->_attachment[$i]);
- $header .= "\r\n--" . $separator . "\r\n";
- }
- }
- //结束邮件数据发送
- $header .= "\r\n.\r\n";
- $command[] = array("DATA\r\n", 354);
- $command[] = array($header, 250);
- $command[] = array("QUIT\r\n", 221);
- return $command;
- }
- /**
- * 发送命令
- * @access protected
- * @param string $command 发送到服务器的smtp命令
- * @param int $code 期望服务器返回的响应吗
- * @return boolean
- */
- protected function sendCommand($command, $code) {
- echo 'Send command:' . $command . ',expected code:' . $code . '<br />';
- //发送命令给服务器
- try{
- if(socket_write($this->_socket, $command, strlen($command))){
- //当邮件内容分多次发送时,没有$code,服务器没有返回
- if(emptyempty($code)) {
- return true;
- }
- //读取服务器返回
- $data = trim(socket_read($this->_socket, 1024));
- echo 'response:' . $data . '<br /><br />';
- if($data) {
- $pattern = "/^".$code."+?/";
- if(preg_match($pattern, $data)) {
- return true;
- }
- else{
- $this->_errorMessage = "Error:" . $data . "|**| command:";
- return false;
- }
- }
- else{
- $this->_errorMessage = "Error:" . socket_strerror(socket_last_error());
- return false;
- }
- }
- else{
- $this->_errorMessage = "Error:" . socket_strerror(socket_last_error());
- return false;
- }
- }catch(Exception $e) {
- $this->_errorMessage = "Error:" . $e->getMessage();
- }
- }
- /**
- * 安全连接发送命令
- * @access protected
- * @param string $command 发送到服务器的smtp命令
- * @param int $code 期望服务器返回的响应吗
- * @return boolean
- */
- protected function sendCommandSecurity($command, $code) {
- echo 'Send command:' . $command . ',expected code:' . $code . '<br />';
- try {
- if(fwrite($this->_socket, $command)){
- //当邮件内容分多次发送时,没有$code,服务器没有返回
- if(emptyempty($code)) {
- return true;
- }
- //读取服务器返回
- $data = trim(fread($this->_socket, 1024));
- echo 'response:' . $data . '<br /><br />';
- if($data) {
- $pattern = "/^".$code."+?/";
- if(preg_match($pattern, $data)) {
- return true;
- }
- else{
- $this->_errorMessage = "Error:" . $data . "|**| command:";
- return false;
- }
- }
- else{
- return false;
- }
- }
- else{
- $this->_errorMessage = "Error: " . $command . " send failed";
- return false;
- }
- }catch(Exception $e) {
- $this->_errorMessage = "Error:" . $e->getMessage();
- }
- }
- /**
- * 读取附件文件内容,返回base64编码后的文件内容
- * @access protected
- * @param string $file 文件
- * @return mixed
- */
- protected function readFile($file) {
- if(file_exists($file)) {
- $file_obj = file_get_contents($file);
- return base64_encode($file_obj);
- }
- else {
- $this->_errorMessage = "file " . $file . " dose not exist";
- return false;
- }
- }
- /**
- * 获取附件MIME类型
- * @access protected
- * @param string $file 文件
- * @return mixed
- */
- protected function getMIMEType($file) {
- if(file_exists($file)) {
- $mime = mime_content_type($file);
- /*if(! preg_match("/gif|jpg|png|jpeg/", $mime)){
- $mime = "application/octet-stream";
- }*/
- return $mime;
- }
- else {
- return false;
- }
- }
- /**
- * 建立到服务器的网络连接
- * @access protected
- * @return boolean
- */
- protected function socket() {
- //创建socket资源
- $this->_socket = socket_create(AF_INET, SOCK_STREAM, getprotobyname('tcp'));
- if(!$this->_socket) {
- $this->_errorMessage = socket_strerror(socket_last_error());
- return false;
- }
- socket_set_block($this->_socket);//设置阻塞模式
- //连接服务器
- if(!socket_connect($this->_socket, $this->_sendServer, $this->_port)) {
- $this->_errorMessage = socket_strerror(socket_last_error());
- return false;
- }
- $str = socket_read($this->_socket, 1024);
- if(!preg_match("/220+?/", $str)){
- $this->_errorMessage = $str;
- return false;
- }
- return true;
- }
- /**
- * 建立到服务器的SSL网络连接
- * @access protected
- * @return boolean
- */
- protected function socketSecurity() {
- $remoteAddr = "tcp://" . $this->_sendServer . ":" . $this->_port;
- $this->_socket = stream_socket_client($remoteAddr, $errno, $errstr, 30);
- if(!$this->_socket){
- $this->_errorMessage = $errstr;
- return false;
- }
- //设置加密连接,默认是ssl,如果需要tls连接,可以查看php手册stream_socket_enable_crypto函数的解释
- stream_socket_enable_crypto($this->_socket, true, STREAM_CRYPTO_METHOD_SSLv23_CLIENT);
- stream_set_blocking($this->_socket, 1); //设置阻塞模式
- $str = fread($this->_socket, 1024);
- if(!preg_match("/220+?/", $str)){
- $this->_errorMessage = $str;
- return false;
- }
- return true;
- }
- /**
- * 关闭socket
- * @access protected
- * @return boolean
- */
- protected function close() {
- if(isset($this->_socket) && is_object($this->_socket)) {
- $this->_socket->close();
- return true;
- }
- $this->_errorMessage = "No resource can to be close";
- return false;
- }
- /**
- * 关闭安全socket
- * @access protected
- * @return boolean
- */
- protected function closeSecutity() {
- if(isset($this->_socket) && is_object($this->_socket)) {
- stream_socket_shutdown($this->_socket, STREAM_SHUT_WR);
- return true;
- }
- $this->_errorMessage = "No resource can to be close";
- return false;
- }
- }
- ?>
QQ邮箱已测试可用,另附录QQ邮箱的SMTP地址:
地址:smtp.qq.com 端口号:465 必须SSL连接。
php网页图片采集
作者:admin 日期:2014-03-06
- <?php
- header('Content-Type:text/html;charset=utf-8');
- /**
- * 一个用于抓去图片的类
- * @author tangpan <tang0pan@qq.com>
- */
- class download_image {
- public $_save_path = NULL; //图片保存路劲
- public $_limit_size = NULL; //图片限制大小
- public static $_img_url_old = array(); //存储已抓取过的图片链接地址
- public static $_a_page_url = array(); //存储抓取过的页面
- public function __construct( $_save_path, $_limit_size) {
- $this->_save_path = $_save_path;
- $this->_limit_size = $_limit_size;
- }
- public function get_all_page_image( $site_url ) {
- if ( $site_url == '' ) {
- return false;
- }
- if ( ! in_array( $site_url, self::$_a_page_url ) ) { //判断当前页面是否抓取过
- self::$_a_page_url[] = $site_url; //将超链接存入静态数组中
- } else {
- return; //若抓取过则跳出
- }
- $this->download_the_page_image( $site_url );
- $content = @file_get_contents($site_url);
- $a_page_url = "|<a[^>]+href=['\" ]?([^ '\"?]+)['\" >]|U";
- $all_url = array();
- preg_match_all( $a_page_url, $content, $all_url, PREG_SET_ORDER );
- if ( $all_url != NULL ) {
- foreach( $all_url as $key => $val ) {
- /**
- * 静态化超链接,防止进入死循环
- * 出去当前页面链接表示方式('','#','/')
- */
- if ( trim($val[1]) != '' && ! in_array( $val[1], self::$_a_page_url ) && ! in_array( $val[1], array('#','/',$site_url) ) ) {
- self::$_a_page_url[] = $val[1]; //将符合要求的超链接写入静态数组中
- }
- }
- }
- if ( self::$_a_page_url != NULL ) {
- foreach( self::$_a_page_url as $keys => $vals ) {
- if ( strpos( $vals, 'http://' ) === false ) { //超链接不包含http://时,不能直接访问
- // 当图片链接地址为相对地址是重新拼凑地址
- $a_domain_url = substr( $site_url, 0, strpos( $site_url, '/',8 ) + 1 );
- $a_img_url = $a_domain_url.$vals;
- }
- //递归调用,访问每一个超链接页面
- $this->get_all_page_image( $a_img_url );
- }
- }
- }
- /**
- * 下载当前页面下的所有图片链接
- * @param $site_url <页面地址>
- */
- public function download_the_page_image( $site_url ) {
- // 获取当前链接地址页面的所有内容
- $img_pattern = NULL;
- $content = @file_get_contents( $site_url );
- $img_pattern = "|<img[^>]+src=['\" ]?([^ '\"?]+)['\" >]|U";
- //全局匹配所有的<img >中的图片链接
- $img_out = array();
- preg_match_all( $img_pattern, $content, $img_out, PREG_SET_ORDER );
- echo '<h1>'. $site_url . '共找到' . count($img_out) . '张图片</h1>';
- //print_r($img_out[1]);
- foreach( $img_out as $key => $val ) {
- //echo htmlspecialchars($val[1]).'<br />';
- $this->save_one_image( $site_url, $val[1]);
- }
- }
- public function save_one_image( $site_url, $img_url ) {
- if ( strpos( $img_url, 'http://' ) === false ) {
- // 当图片链接地址为相对地址是重新拼凑地址
- $domain_url = substr( $site_url, 0, strpos( $site_url, '/',8 ) + 1 );
- $img_url = $domain_url.$img_url;
- }
- $pic_name = basename( $img_url ); //获取图片名称
- if ( in_array( $img_url, self::$_img_url_old ) ) {
- echo $img_url .'<span style="color:red;margin-left:50px">该图片已经抓取过!</span><br/>';
- return;
- }
- //获取图片内容,并写入一个字符串
- $img_data = @file_get_contents( $img_url );
- if ( strlen($img_data) < $this->_limit_size ) { //图片大小在限制范围内
- $img_boo = @file_put_contents( $this->_save_path.md5(microtime()).$pic_name, $img_data );
- if ( $img_boo ) {
- echo $img_url .'<span style="color:green;margin-left:50px;">图片保存成功!</span><br/>';
- self::$_img_url_old[] = $img_url;
- } else {
- echo $img_url .'<span style="color:red;margin-left:50px;">图片保存失败!</span><br />';
- }
- } else {
- echo $img_url .'<span style="color:red;margin-left:50px;">图片大小在限制范围之外!</span><br />';
- }
- }
- }
- set_time_limit(0);
- $download_images = new download_image('surces_Img/',1024*1024*100);
- $download_images->get_all_page_image('http://www.22mm.cc/');
- ?>
PHP判断变量内容是什么编码(gbk?utf-8) mb_detect_encoding
作者:admin 日期:2014-03-06
广告位