分类目录归档:Php

一个简单的php生成excelxml的类

php-excel-v1.1-20090910

<?php

/**
 * Simple excel generating from PHP5
 *
 * @package Utilities
 * @license http://www.opensource.org/licenses/mit-license.php
 * @author Oliver Schwarz <oliver.schwarz@gmail.com>
 * @version 1.0
 */

/**
 * Generating excel documents on-the-fly from PHP5
 * 
 * Uses the excel XML-specification to generate a native
 * XML document, readable/processable by excel.
 * 
 * @package Utilities
 * @subpackage Excel
 * @author Oliver Schwarz <oliver.schwarz@vaicon.de>
 * @version 1.1
 * 
 * @todo Issue #4: Internet Explorer 7 does not work well with the given header
 * @todo Add option to give out first line as header (bold text)
 * @todo Add option to give out last line as footer (bold text)
 * @todo Add option to write to file
 */
class Excel_XML
{

	/**
	 * Header (of document)
	 * @var string
	 */
        private $header = "<?xml version=\"1.0\" encoding=\"%s\"?\>\n<Workbook xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns:ss=\"urn:schemas-microsoft-com:office:spreadsheet\" xmlns:html=\"http://www.w3.org/TR/REC-html40\">";

        /**
         * Footer (of document)
         * @var string
         */
        private $footer = "</Workbook>";

        /**
         * Lines to output in the excel document
         * @var array
         */
        private $lines = array();

        /**
         * Used encoding
         * @var string
         */
        private $sEncoding;
        
        /**
         * Convert variable types
         * @var boolean
         */
        private $bConvertTypes;
        
        /**
         * Worksheet title
         * @var string
         */
        private $sWorksheetTitle;

        /**
         * Constructor
         * 
         * The constructor allows the setting of some additional
         * parameters so that the library may be configured to
         * one's needs.
         * 
         * On converting types:
         * When set to true, the library tries to identify the type of
         * the variable value and set the field specification for Excel
         * accordingly. Be careful with article numbers or postcodes
         * starting with a '0' (zero)!
         * 
         * @param string $sEncoding Encoding to be used (defaults to UTF-8)
         * @param boolean $bConvertTypes Convert variables to field specification
         * @param string $sWorksheetTitle Title for the worksheet
         */
        public function __construct($sEncoding = 'UTF-8', $bConvertTypes = false, $sWorksheetTitle = 'Table1')
        {
                $this->bConvertTypes = $bConvertTypes;
        	$this->setEncoding($sEncoding);
        	$this->setWorksheetTitle($sWorksheetTitle);
        }
        
        /**
         * Set encoding
         * @param string Encoding type to set
         */
        public function setEncoding($sEncoding)
        {
        	$this->sEncoding = $sEncoding;
        }

        /**
         * Set worksheet title
         * 
         * Strips out not allowed characters and trims the
         * title to a maximum length of 31.
         * 
         * @param string $title Title for worksheet
         */
        public function setWorksheetTitle ($title)
        {
                $title = preg_replace ("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title);
                $title = substr ($title, 0, 31);
                $this->sWorksheetTitle = $title;
        }

        /**
         * Add row
         * 
         * Adds a single row to the document. If set to true, self::bConvertTypes
         * checks the type of variable and returns the specific field settings
         * for the cell.
         * 
         * @param array $array One-dimensional array with row content
         */
        private function addRow ($array)
        {
        	$cells = "";
                foreach ($array as $k => $v):
                        $type = 'String';
                        if ($this->bConvertTypes === true && is_numeric($v)):
                                $type = 'Number';
                        endif;
                        $v = htmlentities($v, ENT_COMPAT, $this->sEncoding);
                        $cells .= "<Cell><Data ss:Type=\"$type\">" . $v . "</Data></Cell>\n"; 
                endforeach;
                $this->lines[] = "<Row>\n" . $cells . "</Row>\n";
        }

        /**
         * Add an array to the document
         * @param array 2-dimensional array
         */
        public function addArray ($array)
        {
                foreach ($array as $k => $v)
                        $this->addRow ($v);
        }


        /**
         * Generate the excel file
         * @param string $filename Name of excel file to generate (...xls)
         */
        public function generateXML ($filename = 'excel-export')
        {
                // correct/validate filename
                $filename = preg_replace('/[^aA-zZ0-9\_\-]/', '', $filename);
    	
                // deliver header (as recommended in php manual)
                header("Content-Type: application/vnd.ms-excel; charset=" . $this->sEncoding);
                header("Content-Disposition: inline; filename=\"" . $filename . ".xls\"");

                // print out document to the browser
                // need to use stripslashes for the damn ">"
                echo stripslashes (sprintf($this->header, $this->sEncoding));
                echo "\n<Worksheet ss:Name=\"" . $this->sWorksheetTitle . "\">\n<Table>\n";
                foreach ($this->lines as $line)
                        echo $line;

                echo "</Table>\n</Worksheet>\n";
                echo $this->footer;
        }

}

?>

 

图片上传后自动旋转php处理代码

<?php 
/*
 开启 exif php扩展
 exif_read_data() 函数从 JPEG 或 TIFF 图像文件中读取 EXIF 头信息。这样就可以读取数码相机产生的原数据。
 而exif_read_data得到的值的Orientation为图片的上传时的原旋转数值
 !有些android上传的图片没有 Orientation 属性
  */

$source = imagecreatefromjpeg($_FILES['img']['tmp_name']);
$exif = exif_read_data($_FILES['img']['tmp_name']);
if(!empty($exif['Orientation'])) {
	switch($exif['Orientation']) {
		case 8:
			$source = imagerotate($source,90,0);
			break;
		case 3:
			$source = imagerotate($source,180,0);
			break;
		case 6:
			$source = imagerotate($source,-90,0);
			break;
	}
}

imagejpeg($source,"123.jpg");  //存储图片

 

php基于curl的get/post请求函数

以下为代码片段,需要根据自己的功能修改

function request($url, $params = array(), $httpMethod = 'GET', $multi = false)
    {
    	// when using bae(baidu app engine) to deploy the application,
    	// just comment the following line
    	$ch = curl_init();
    	// when using bae(baidu app engine) to deploy the application,
    	// and uncomment the following two lines
    	//$fetch= new BaeFetchUrl();
  		//$ch = $fetch->getHandle();
  		
    	$curl_opts = array(
			CURLOPT_CONNECTTIMEOUT	=> 3,
			CURLOPT_TIMEOUT			=> 5,
			CURLOPT_USERAGENT		=> 'baidu-apiclient-php-2.0',
	    	CURLOPT_HTTP_VERSION	=> CURL_HTTP_VERSION_1_1,
	    	CURLOPT_RETURNTRANSFER	=> true,
	    	CURLOPT_HEADER			=> false,
	    	CURLOPT_FOLLOWLOCATION	=> false,
		);

		if (stripos($url, 'https://') === 0) {
			$curl_opts[CURLOPT_SSL_VERIFYPEER] = false;
		}
		
		if (strtoupper($httpMethod) === 'GET') {
			$query = http_build_query($params, '', '&');
			$delimiter = strpos($url, '?') === false ? '?' : '&';
    		$curl_opts[CURLOPT_URL] = $url . $delimiter . $query;
    		$curl_opts[CURLOPT_POST] = false;
		} else {
			$headers = array();
			if ($multi && is_array($params) && !empty($params)) {
				$body = self::buildHttpMultipartBody($params);
				$headers[] = 'Content-Type: multipart/form-data; boundary=' . self::$boundary;
			} else {
				$body = http_build_query($params, '', '&');
			}
			$curl_opts[CURLOPT_URL] = $url;
    		$curl_opts[CURLOPT_POSTFIELDS] = $body;
    		$curl_opts[CURLOPT_HTTPHEADER] = $headers;
		}
    
    	curl_setopt_array($ch, $curl_opts);
        $result = curl_exec($ch);
        
    	if ($result === false) {
    		self::setError(curl_errno($ch), curl_error($ch));
            curl_close($ch);
            return false;
    	} elseif (empty($result)) {
    		$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    		if ($http_code != 200) {
    			self::setError($http_code, 'http response status code: ' . $http_code);
    			curl_close($ch);
    			return false;
    		}
    	}
        
    	curl_close($ch);
    	
    	return $result;
    }

 

discuz去掉手机版只保留触屏版

1、修改source\class\discuz\discuz_application.php 大约在 177行,改为如下

'mobiletpl' => array('1' => 'touch', '2' => 'touch', '3' => 'wml', 'yes' => 'touch'),

2、修改static\js\mobile\common.js  第二行,将后面 forum.php?mobile=1 改为forum.php?mobile=2,如下

var supporttouch = "ontouchend" in document;
!supporttouch && (window.location.href = 'forum.php?mobile=2');

版权www.yiyou.org

一个不错php_http_post函数

function post_request($url, $postdata = null){
	    	$data = http_build_query($postdata);
	    	if(function_exists('curl_exec')){
	    		$ch = curl_init();
	    		curl_setopt($ch, CURLOPT_URL, $url);
	    		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	    		if(!$postdata){
	    			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
	    			curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
	    		}else{
	    			curl_setopt($ch, CURLOPT_POST, 1);
	    			curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
	    		}
	    		$data = curl_exec($ch);
	    		curl_close($ch);
	    	}else{
	    		if($postdata){
		    		$url = $url.'?'.$data;
				$opts = array(
					'http' => array(
			            		'method' => 'POST',
			            		'header'=> "Content-type: application/x-www-form-urlencoded\r\n" . "Content-Length: " . strlen($data) . "\r\n",
			            		'content' => $data
			            		)
				    );
				$context = stream_context_create($opts);
		    		$data = file_get_contents($url, false, $context);
	    		}
	    	}
    	return $data;
    }

 

debian_php7.0安装

号秒杀php5.x的PHP7 已经发行,为了体验一下安装php7,所以测试一下

1、下载

http://php.net/downloads.php

2、解压

tar zxf   php7.0.0.tar.gz

3、配置

./configure --prefix=/usr/local/php7 --enable-fpm --enable-zip --enable-mbstring --with-mysqli=/usr/bin/mysql_config --enable-mysqlnd --with-mcrypt=/usr/bin/libmcrypt-config
make
make install

4、cd /usr/local/php7/etc

cp php-fpm.conf.default php-fpm.conf

cp php-fpm.d/www.conf.default php-fpm.d/www.conf

修改 php-fpm.d/www.conf

user = www-data
group = www-data

启动

/usr/local/php7/sbin/php-fpm

配置nginx访问php-fpm 即可得到下面的界面

QQ图片20151211150322

修改discuz系统插件weichat

想修改discuz系统插件 微信登录 1.1.5 (wechat) ,在插件列表中,发现没有设计按钮,为了出现设计按钮,查看了代码,发现内容在数据库做了限制 打开pre_common_plugin 表,找到identifier 为weichat 的条目,modules 字段是一个序列化的数据,反序列化后 ,有一个system=>2变量。只要把这个变量删除再保存就可以了。

如果你想偷懒,把下面的内容存回modules字段即可

a:15:{i:1;a:11:{s:4:"name";s:6:"wechat";s:5:"param";s:0:"";s:4:"menu";s:0:"";s:3:"url";s:0:"";s:4:"type";s:2:"11";s:7:"adminid";s:1:"0";s:12:"displayorder";s:1:"0";s:8:"navtitle";s:0:"";s:7:"navicon";s:0:"";s:10:"navsubname";s:0:"";s:9:"navsuburl";s:0:"";}i:2;a:11:{s:4:"name";s:7:"spacecp";s:5:"param";s:0:"";s:4:"menu";s:12:"微信绑定";s:3:"url";s:0:"";s:4:"type";s:1:"7";s:7:"adminid";s:1:"0";s:12:"displayorder";s:1:"0";s:8:"navtitle";s:0:"";s:7:"navicon";s:0:"";s:10:"navsubname";s:0:"";s:9:"navsuburl";s:0:"";}i:3;a:11:{s:4:"name";s:6:"wechat";s:5:"param";s:0:"";s:4:"menu";s:0:"";s:3:"url";s:0:"";s:4:"type";s:2:"28";s:7:"adminid";s:1:"0";s:12:"displayorder";s:1:"0";s:8:"navtitle";s:0:"";s:7:"navicon";s:0:"";s:10:"navsubname";s:0:"";s:9:"navsuburl";s:0:"";}i:4;a:11:{s:4:"name";s:11:"wsq_setting";s:5:"param";s:0:"";s:4:"menu";s:15:"微社区设置";s:3:"url";s:0:"";s:4:"type";s:1:"3";s:7:"adminid";s:1:"0";s:12:"displayorder";s:0:"";s:8:"navtitle";s:0:"";s:7:"navicon";s:0:"";s:10:"navsubname";s:0:"";s:9:"navsuburl";s:0:"";}i:5;a:11:{s:4:"name";s:20:"showactivity_setting";s:5:"param";s:0:"";s:4:"menu";s:6:"晒图";s:3:"url";s:0:"";s:4:"type";s:1:"3";s:7:"adminid";s:1:"0";s:12:"displayorder";s:0:"";s:8:"navtitle";s:0:"";s:7:"navicon";s:0:"";s:10:"navsubname";s:0:"";s:9:"navsuburl";s:0:"";}i:6;a:11:{s:4:"name";s:14:"wechat_setting";s:5:"param";s:0:"";s:4:"menu";s:12:"微信设置";s:3:"url";s:0:"";s:4:"type";s:1:"3";s:7:"adminid";s:1:"0";s:12:"displayorder";s:0:"";s:8:"navtitle";s:0:"";s:7:"navicon";s:0:"";s:10:"navsubname";s:0:"";s:9:"navsuburl";s:0:"";}i:7;a:11:{s:4:"name";s:12:"menu_setting";s:5:"param";s:0:"";s:4:"menu";s:12:"菜单设置";s:3:"url";s:0:"";s:4:"type";s:1:"3";s:7:"adminid";s:1:"0";s:12:"displayorder";s:0:"";s:8:"navtitle";s:0:"";s:7:"navicon";s:0:"";s:10:"navsubname";s:0:"";s:9:"navsuburl";s:0:"";}i:8;a:11:{s:4:"name";s:16:"response_setting";s:5:"param";s:0:"";s:4:"menu";s:12:"消息设置";s:3:"url";s:0:"";s:4:"type";s:1:"3";s:7:"adminid";s:1:"0";s:12:"displayorder";s:0:"";s:8:"navtitle";s:0:"";s:7:"navicon";s:0:"";s:10:"navsubname";s:0:"";s:9:"navsuburl";s:0:"";}i:9;a:11:{s:4:"name";s:16:"resource_setting";s:5:"param";s:0:"";s:4:"menu";s:12:"素材设置";s:3:"url";s:0:"";s:4:"type";s:1:"3";s:7:"adminid";s:1:"0";s:12:"displayorder";s:0:"";s:8:"navtitle";s:0:"";s:7:"navicon";s:0:"";s:10:"navsubname";s:0:"";s:9:"navsuburl";s:0:"";}i:10;a:11:{s:4:"name";s:11:"api_setting";s:5:"param";s:0:"";s:4:"menu";s:6:"接口";s:3:"url";s:0:"";s:4:"type";s:1:"3";s:7:"adminid";s:1:"0";s:12:"displayorder";s:0:"";s:8:"navtitle";s:0:"";s:7:"navicon";s:0:"";s:10:"navsubname";s:0:"";s:9:"navsuburl";s:0:"";}i:11;a:11:{s:4:"name";s:16:"masssend_setting";s:5:"param";s:0:"";s:4:"menu";s:12:"群发设置";s:3:"url";s:0:"";s:4:"type";s:1:"3";s:7:"adminid";s:1:"0";s:12:"displayorder";s:0:"";s:8:"navtitle";s:0:"";s:7:"navicon";s:0:"";s:10:"navsubname";s:0:"";s:9:"navsuburl";s:0:"";}i:12;a:11:{s:4:"name";s:8:"wsq_stat";s:5:"param";s:0:"";s:4:"menu";s:15:"微社区统计";s:3:"url";s:0:"";s:4:"type";s:1:"3";s:7:"adminid";s:1:"0";s:12:"displayorder";s:0:"";s:8:"navtitle";s:0:"";s:7:"navicon";s:0:"";s:10:"navsubname";s:0:"";s:9:"navsuburl";s:0:"";}i:13;a:11:{s:4:"name";s:7:"wsq_app";s:5:"param";s:0:"";s:4:"menu";s:12:"活动中心";s:3:"url";s:0:"";s:4:"type";s:1:"3";s:7:"adminid";s:1:"0";s:12:"displayorder";s:1:"0";s:8:"navtitle";s:0:"";s:7:"navicon";s:0:"";s:10:"navsubname";s:0:"";s:9:"navsuburl";s:0:"";}s:6:"system";i:2;s:5:"extra";a:2:{s:11:"installtype";s:0:"";s:10:"langexists";i:1;}}

 

HTTP_RAW_POST_DATA丢失重新获取方法

在discuz 二次开发微信支付时,在微信异步通知这里遇到一个很神奇的问题,加载discuz核心后,这个变量就为空了。

$GLOBALS[‘HTTP_RAW_POST_DATA’]=’www.yiyou.org’

为了重新获取变量,需要加载完discuz 核心后重新执行下面代码,参考如下:

require '../../../source/class/class_core.php';
require '../../../source/function/function_forum.php';

$discuz = C::app();
$discuz->init();

$GLOBALS['HTTP_RAW_POST_DATA']=isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");