预览模式: 普通 | 列表

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 | 查看次数: 2693

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 | 查看次数: 2923

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 | 查看次数: 3389

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 | 查看次数: 2510

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 | 查看次数: 2308

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 | 查看次数: 5853

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 | 查看次数: 2519

 

PHP代码
  1. /*  
  2. 图片合并需要 
  3. 1   下载   php_imagick_st-Q8.dll    
  4. 并将其放入 php/ext/ 
  5.  2 在php.ini 中加入 
  6.     extension=php_imagick_st-Q8.dll           
  7.  重启apache (web 服务器) 
  8. 3 访问phpinfo  
  9. */    
  10.   
  11.    
  12. //参数设置  
  13. $filelist = array(    
  14.     '1.jpg',    
  15.     '2.jpg',    
  16.     '3.jpg',    
  17.     '4.jpg'  
  18. );    
  19. $type = 'gif';  
  20. $num  = 200;  
  21. $qian = 'new_';  
  22. $path = './tupian/';  
  23. $is   = 1;  
  24.   
  25. //调用函数生成gif图片  
  26. get_img($filelist$type$num$qian$path$is);  
  27.   
  28.   
  29. /** 
  30.  * get_img  图片合并,生成gif动态 
  31.  * 
  32.  * $filelist  要合并的图片数组 
  33.  * $type      生成的类型 
  34.  * $num       生成的帧数 
  35.  * $qian      新文件名前缀 
  36.  * $path      保持路径 
  37.  * $is        是否预览 
  38.  *  
  39.  */  
  40. function get_img($filelist$type$num$qian$path$is)  
  41. {  
  42.     //初始化类  
  43.     $animation = new Imagick();   
  44.     //设置生成的格式  
  45.     $animation->setFormat($type);     
  46.   
  47.     foreach ( $filelist as $file ){    
  48.         $image = new Imagick();    
  49.         $image->readImage( $file );      //合并图片  
  50.         $animation->addImage( $image );  //加入到刚才建立的对象  
  51.         $animation->setImageDelay($num); //设定图片的帧数  
  52.         unset( $image );                 //消除内存里的图像资源  
  53.     }    
  54.   
  55.     //调试是否生成gif图片  
  56.     //header( "Content-Type: image/gif" );    
  57.     //echo( $animation->getImagesBlob() );    
  58.       
  59.     //新图片文件名组合  
  60.     $images = $qian . time(). '.' . $type;  
  61.       
  62.     //生成图片  
  63.     $animation->writeImages( $images,true );   
  64.       
  65.     //保存都指定目录  
  66.     copy($images$path . $images);  
  67.   
  68.     //是否预览  
  69.     if($is)  
  70.     {  
  71.         echo '已生成gif图片: ' . $images . '<br />';  
  72.         echo "<img src='" . $path . $images . "' />";  
  73.     }  
  74.     else  
  75.     {  
  76.         echo '已生成gif图片: ' . $images . '<br />';  
  77.     }  
  78.       
  79.     //删除最先保存的图片  
  80.     unlink($images);  
  81. }  

 

Tags: php

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

php批量下载搜狗词库

 index.php:

PHP代码
  1. <?php  
  2.     /*//echo microtime(); 
  3.     echo $start=strtotime(date("Y-m-01"))."<br/>"; 
  4.     echo urlencode(iconv("UTF-8","GB2312//IGNORE","中国的") ); 
  5.     echo urlencode("/"); 
  6.     require_once "pinyin.php"; 
  7.     $pinyin=new PY_deal(); 
  8.     echo join($pinyin->pinyin("麦包包批发,[魅惑/微光摇曳/青春密语]三种款式,自由选择!现只需50元/款!"));*/  
  9.   
  10.     $fp=fopen("dictory.txt""a+");  
  11.     for($i=1;$i<=4;$i++){  
  12.         $curl=curl_init();  
  13.         $url="http://pinyin.sogou.com/dict/search.php?word=%B0%A2%C0%EF%B0%CD%B0%CD%B2%FA%C6%B7%B4%CA%BB%E3&type=0&personal=1&page={$i}";  
  14.         $options=array(CURLOPT_URL=>$url,CURLOPT_RETURNTRANSFER=>true,CURLOPT_HEADER=>false);  
  15.         curl_setopt_array($curl$options);  
  16.         $rs=curl_exec($curl);  
  17.         curl_close($curl);  
  18.         $pattern_list="/\<dl\s+class\=\"dictlist\"\>([\S\s]+)?\<\/dl\>/i";  
  19.         preg_match_all($pattern_list,$rs,$match);  
  20.         $rs=$match[1][0];  
  21.         $pattern="/\?id=(\d+?)\"/i";  
  22.         preg_match_all($pattern,$rs,$match);  
  23.         $rs=$match[1];  
  24.           
  25.         foreach($rs as $v){  
  26.             $link="http://pinyin.sogou.com/dict/download_txt.php?id={$v}";  
  27.             $curl=curl_init();  
  28.             $options=array(CURLOPT_URL=>$link,CURLOPT_FILE=>$fp,CURLOPT_HEADER=>false);  
  29.             curl_setopt_array($curl$options);  
  30.             curl_exec($curl);  
  31.             curl_close($curl);  
  32.               
  33.   
  34.         }  
  35.   
  36.     }  
  37.       
  38.   
  39.     fclose($fp);  
  40. ?>  

 

Tags: php

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

 广告位

↑返回顶部↑