php输出json格式数据
作者:admin 日期:2014-03-06
php有道翻译api调用方法
作者:admin 日期:2014-03-06
PHP Curl 模拟POST 可以https
作者:admin 日期:2014-03-06
PHP代码
- 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
PHP代码
- ////二纬码
- $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代码
- <?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代码
- <?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代码
- <?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代码
- <?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代码
- <?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
php合并图片,生成gif动态图片,保存到指定目录
作者:admin 日期:2014-03-06
PHP代码
- /*
- 图片合并需要
- 1 下载 php_imagick_st-Q8.dll
- 并将其放入 php/ext/
- 2 在php.ini 中加入
- extension=php_imagick_st-Q8.dll
- 重启apache (web 服务器)
- 3 访问phpinfo
- */
- //参数设置
- $filelist = array(
- '1.jpg',
- '2.jpg',
- '3.jpg',
- '4.jpg'
- );
- $type = 'gif';
- $num = 200;
- $qian = 'new_';
- $path = './tupian/';
- $is = 1;
- //调用函数生成gif图片
- get_img($filelist, $type, $num, $qian, $path, $is);
- /**
- * get_img 图片合并,生成gif动态
- *
- * $filelist 要合并的图片数组
- * $type 生成的类型
- * $num 生成的帧数
- * $qian 新文件名前缀
- * $path 保持路径
- * $is 是否预览
- *
- */
- function get_img($filelist, $type, $num, $qian, $path, $is)
- {
- //初始化类
- $animation = new Imagick();
- //设置生成的格式
- $animation->setFormat($type);
- foreach ( $filelist as $file ){
- $image = new Imagick();
- $image->readImage( $file ); //合并图片
- $animation->addImage( $image ); //加入到刚才建立的对象
- $animation->setImageDelay($num); //设定图片的帧数
- unset( $image ); //消除内存里的图像资源
- }
- //调试是否生成gif图片
- //header( "Content-Type: image/gif" );
- //echo( $animation->getImagesBlob() );
- //新图片文件名组合
- $images = $qian . time(). '.' . $type;
- //生成图片
- $animation->writeImages( $images,true );
- //保存都指定目录
- copy($images, $path . $images);
- //是否预览
- if($is)
- {
- echo '已生成gif图片: ' . $images . '<br />';
- echo "<img src='" . $path . $images . "' />";
- }
- else
- {
- echo '已生成gif图片: ' . $images . '<br />';
- }
- //删除最先保存的图片
- unlink($images);
- }
php批量下载搜狗词库
作者:admin 日期:2014-03-06
index.php:
PHP代码
- <?php
- /*//echo microtime();
- echo $start=strtotime(date("Y-m-01"))."<br/>";
- echo urlencode(iconv("UTF-8","GB2312//IGNORE","中国的") );
- echo urlencode("/");
- require_once "pinyin.php";
- $pinyin=new PY_deal();
- echo join($pinyin->pinyin("麦包包批发,[魅惑/微光摇曳/青春密语]三种款式,自由选择!现只需50元/款!"));*/
- $fp=fopen("dictory.txt", "a+");
- for($i=1;$i<=4;$i++){
- $curl=curl_init();
- $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}";
- $options=array(CURLOPT_URL=>$url,CURLOPT_RETURNTRANSFER=>true,CURLOPT_HEADER=>false);
- curl_setopt_array($curl, $options);
- $rs=curl_exec($curl);
- curl_close($curl);
- $pattern_list="/\<dl\s+class\=\"dictlist\"\>([\S\s]+)?\<\/dl\>/i";
- preg_match_all($pattern_list,$rs,$match);
- $rs=$match[1][0];
- $pattern="/\?id=(\d+?)\"/i";
- preg_match_all($pattern,$rs,$match);
- $rs=$match[1];
- foreach($rs as $v){
- $link="http://pinyin.sogou.com/dict/download_txt.php?id={$v}";
- $curl=curl_init();
- $options=array(CURLOPT_URL=>$link,CURLOPT_FILE=>$fp,CURLOPT_HEADER=>false);
- curl_setopt_array($curl, $options);
- curl_exec($curl);
- curl_close($curl);
- }
- }
- fclose($fp);
- ?>
广告位