php判断是不是https的方法

php判断是不是https的方法

    public function is_https()
    {
        if (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) !== 'off') {
            return true;
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] === 'https') {
            return true;
        } elseif (!empty($_SERVER['HTTP_FRONT_END_HTTPS']) && strtolower($_SERVER['HTTP_FRONT_END_HTTPS']) !== 'off') {
            return true;
        }

        return false;
    }

PHP curl下载图片的方法

PHP curl下载图片的方法


<?php


$images = [
  'http://wx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTKPkia3rxjuBoSQ1sUSmqYXDazibuIp8p2CCialBPjeYvcNJgky9P7hmlnDKgzX91utEQPu7VtsDqAIg/0',
];
function download($url, $path = 'dowloadceshi/2015-09-22_00-17-06j.png')
{
  $ch = curl_init();
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
  $file = curl_exec($ch);

  curl_close($ch);
  $filename = pathinfo($url, PATHINFO_BASENAME);

  $resource = fopen($path, 'a');
  fwrite($resource, $file);
  fclose($resource);
}

foreach ( $images as $url ) {
  download($url);
}
?>

ps:如果采用file_get_contents方法发生图片下载过慢

php sublime常用插件

php sublime常用插件

1 Sublime Text的默认设置是不开启显示编码的,如果想开启,可通过菜单Perference → Settings – User,在打开的配置文件里 ,在大括号后面,增加以下内容:// Display file encoding in the status bar”show_encoding”: true,// Display line endings in the status bar”show_line_endings”: true,此时保存该配置文件,就能够看到sublime最底下一行会显示文件编码格式了。以上的配置内容在Perference → Setting─Default都是false的。 注意需要复制到user才能改 默认改不了

2 常用的快捷键
ctrl+p可以快速查找文件

3安装风格spacegray
直接用pci安装 搜索spacegray
安装玩然后选择就行了 另外修改下配置文件

   "theme": "Spacegray.sublime-theme",
"color_scheme": "Packages/Theme - Spacegray/base16-ocean.dark.tmTheme",
    "highlight_line": true,
 
    "spacegray_sidebar_font_xlarge": true,
    "spacegray_sidebar_tree_large": true,
    "spacegray_tabs_auto_width": true,
    "spacegray_tabs_font_large": true,
    "spacegray_tabs_xlarge": true,
 
    "line_padding_bottom": 3,
    "line_padding_top": 3,
 
	"font_size": 14

4 SideBarEnhancements插件安装 用于新建 复制文件

5ctrl+R查找函数 还有个brow 什么package里面 新建个Default (Windows).sublime-mousemap 然后

[
{
"button": "button2",
"count": 1,
"modifiers": ["ctrl"],
"command": "jump_back"
},


{
"button": "button1",
"count": 1,
"modifiers": ["ctrl"],
"press_command": "drag_select",
"command": "goto_definition"
}
]

复制进去 就支持ctrl+左键了 ctrl+右键还可以跳回

5 设置键盘快捷键 格式化代码

{ "keys": ["ctrl+alt+l"], "command": "reindent" }

6 Clipboard History插件
Ctrl+shift+v:可以看到粘贴历史

7 Bracket Highlighter
功能:代码匹配

简介:可匹配[], (), {}, “”, ”, ,高亮标记,便于查看起始和结束标记

使用:点击对应代码即可

8 开启PHP报错提示
sublimeliner

sublimeliner-php 两个

安装好了之后

打开preferences -> package-setting -> sublimeliner-> settings–user

“mark_style”: “outline”,
“no_column_highlights_line”: false,
“passive_warnings”: true,
“paths”: {
“linux”: [],
“osx”: [],
“windows”: [
“D:\\wamp\\bin\\php\\php5.5.12”
]
},
“python_paths”: {
“linux”: [],
“osx”: [],
“windows”: []
},

就是添加一下 paths的参数值,指定一下你本地php目录。

然后关闭sublime,再次打开一个php脚本,试试吧。

有错误的地方在行号上会有红点提示在代码上会有红色方框,鼠标放红色方框上,错误信息在编辑器底部状态栏显示。

9格式化用codeformatter插件 php设置下路径就好
建议不用codeformatter格式化html

用 HTML-CSS-JS Prettify 插件来格式化 需要装nodejs 然后设置路径
格式化快捷键ctrl+shift+h

ps:导出sublime3所有插件和配置方法

C:\Users\Administrator\AppData\Roaming\Sublime Text 3
Packages和Installed Packages放到新的sublime3相应的路径即可

php [poolwww] seemsbusy (youmayneedto increasepm.start_servers, or pm.min/max_spare_servers)错误解决方法

php [poolwww] seemsbusy (youmayneedto increasepm.start_servers, or pm.min/max_spare_servers)错误解决方法
修改php-fpm.conf配置文件

pm.max_children = 1024

; The number of child processes created on startup.
; Note: Used only when pm is set to ‘dynamic’
; Default Value: min_spare_servers + (max_spare_servers – min_spare_servers) / 2
pm.start_servers = 512

; The desired minimum number of idle server processes.
; Note: Used only when pm is set to ‘dynamic’
; Note: Mandatory when pm is set to ‘dynamic’
pm.min_spare_servers = 4

; The desired maximum number of idle server processes.
; Note: Used only when pm is set to ‘dynamic’
; Note: Mandatory when pm is set to ‘dynamic’
pm.max_spare_servers = 512

PHP failed to ptrace(PEEKDATA) pid 13659: Input/output error错误解决方法

PHP failed to ptrace(PEEKDATA) pid 13659: Input/output error错误解决方法

现在改linux内核文件打开限制

ulimit -SHn 51200

再修改nginx.conf配置文件

worker_rlimit_nofile 51200;

最后修改PHP-fpm.conf配置文件

rlimit_files = 51200

这3个一定要设置一样哦

2个必须保持一致就可以了

php 微信消息接口

其实消息接口原理 就是微信 请求你后台写的地址接口签名验证 然后就可以操作了

<?php
/**
 *	微信公众平台PHP-SDK, 官方API部分
 *  @author  dodge <dodgepudding@gmail.com>
 *  @link https://github.com/dodgepudding/wechat-php-sdk
 *  @version 1.1
 *  usage:
 *   $options = array(
 *			'token'=>'tokenaccesskey' //填写你设定的key
 *		);
 *	 $weObj = new Wechat1($options);
 *   $weObj->valid();
 *   $type = $weObj->getRev()->getRevType();
 *   switch($type) {
 *   		case Wechat::MSGTYPE_TEXT:
 *   			$weObj->text("hello, I'm wechat")->reply();
 *   			exit;
 *   			break;
 *   		case Wechat::MSGTYPE_EVENT:
 *   			....
 *   			break;
 *   		case Wechat::MSGTYPE_IMAGE:
 *   			...
 *   			break;
 *   		default:
 *   			$weObj->text("help info")->reply();
 *   }
 */
namespace Com\Wechat;
class Wechat1
{
	const MSGTYPE_TEXT = 'text';
	const MSGTYPE_IMAGE = 'image';
	const MSGTYPE_LOCATION = 'location';
	const MSGTYPE_LINK = 'link';
	const MSGTYPE_EVENT = 'event';
	const MSGTYPE_MUSIC = 'music';
	const MSGTYPE_NEWS = 'news';
	private $token;
	private $_msg;
	private $_funcflag = false;
	private $_receive;
	public $debug =  false;
	private $_logcallback;
	
	public function __construct($options)
	{
	
		$this->token = isset($options['token'])?$options['token']:'';
		$this->debug = isset($options['debug'])?$options['debug']:false;
		$this->_logcallback = isset($options['logcallback'])?$options['logcallback']:false;
	}
	
	/**
	 * For weixin server validation 
	 */	
	private function checkSignature()
	{
        $signature = $_GET["signature"];
	
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];	
        
		$token = $this->token;
		$tmpArr = array($token, $timestamp, $nonce);
		sort($tmpArr);
		$tmpStr = implode( $tmpArr );
		$tmpStr = sha1( $tmpStr );
		
		if( $tmpStr == $signature ){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * For weixin server validation 
	 * @param bool $return 是否返回
	 */
	public function valid($return=false)
    {
        $echoStr = isset($_GET["echostr"]) ? $_GET["echostr"]: '';
        if ($return) {
        		if ($echoStr) {
        			if ($this->checkSignature()) 
        				return $echoStr;
        			else
        				return false;
        		} else 
        			return $this->checkSignature();
        } else {
	        	if ($echoStr) {
	        		if ($this->checkSignature())
						file_put_contents(__DIR__.'/log.log', date("Y-m-d H:i:s"). " " . $echoStr.PHP_EOL, FILE_APPEND | LOCK_EX);
	        		else 
						file_put_contents(__DIR__.'/log.log', date("Y-m-d H:i:s"). " " . 'no access1'.PHP_EOL, FILE_APPEND | LOCK_EX);
	        	}  else {
	        		if ($this->checkSignature())
	        			return true;
	        		else
						file_put_contents(__DIR__.'/log.log', date("Y-m-d H:i:s"). " " . 'no access2'.PHP_EOL, FILE_APPEND | LOCK_EX);
	        	}
        }
        return false;
    }
    
	/**
	 * 设置发送消息
	 * @param array $msg 消息数组
	 * @param bool $append 是否在原消息数组追加
	 */
    public function Message($msg = '',$append = false){
    		if (is_null($msg)) {
    			$this->_msg =array();
    		}elseif (is_array($msg)) {
    			if ($append)
    				$this->_msg = array_merge($this->_msg,$msg);
    			else
    				$this->_msg = $msg;
    			return $this->_msg;
    		} else {
    			return $this->_msg;
    		}
    }
    
    public function setFuncFlag($flag) {
    		$this->_funcflag = $flag;
    		return $this;
    }
    
  
    
    /**
     * 获取微信服务器发来的信息
     */
	public function getRev()
	{
		$postStr = file_get_contents("php://input");
	    file_put_contents(__DIR__.'/log.log', date("Y-m-d H:i:s"). " " . '收到的xml:'.$postStr.PHP_EOL, FILE_APPEND | LOCK_EX);
		if (!empty($postStr)) {
			$this->_receive = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
		}
		
		return $this;
	}
	
	/**
	 * 获取消息发送者
	 */
	public function getRevFrom() {
		if ($this->_receive)
			return $this->_receive['FromUserName'];
		else 
			return false;
	}
	
	/**
	 * 获取消息接受者
	 */
	public function getRevTo() {
		if ($this->_receive)
			return $this->_receive['ToUserName'];
		else 
			return false;
	}
	
	/**
	 * 获取接收消息的类型
	 */
	public function getRevType() {
		if (isset($this->_receive['MsgType']))
			return $this->_receive['MsgType'];
		else 
			return false;
	}
	
	/**
	 * 获取消息ID
	 */
	public function getRevID() {
		if (isset($this->_receive['MsgId']))
			return $this->_receive['MsgId'];
		else 
			return false;
	}
	
	/**
	 * 获取消息发送时间
	 */
	public function getRevCtime() {
		if (isset($this->_receive['CreateTime']))
			return $this->_receive['CreateTime'];
		else 
			return false;
	}
	
	/**
	 * 获取接收消息内容正文
	 */
	public function getRevContent(){
		if (isset($this->_receive['Content']))
			return $this->_receive['Content'];
		else 
			return false;
	}
	
	/**
	 * 获取接收消息图片
	 */
	public function getRevPic(){
		if (isset($this->_receive['PicUrl']))
			return $this->_receive['PicUrl'];
		else 
			return false;
	}
	
	/**
	 * 获取接收消息链接
	 */
	public function getRevLink(){
		if (isset($this->_receive['Url'])){
			return array(
				'url'=>$this->_receive['Url'],
				'title'=>$this->_receive['Title'],
				'description'=>$this->_receive['Description']
			);
		} else 
			return false;
	}
	
	/**
	 * 获取接收地理位置
	 */
	public function getRevGeo(){
		if (isset($this->_receive['Location_X'])){
			return array(
				'x'=>$this->_receive['Location_X'],
				'y'=>$this->_receive['Location_Y'],
				'scale'=>$this->_receive['Scale'],
				'label'=>$this->_receive['Label']
			);
		} else 
			return false;
	}
	
	/**
	 * 获取接收事件推送
	 */
	public function getRevEvent(){
		if (isset($this->_receive['Event'])){
			return array(
				'event'=>$this->_receive['Event'],
				'key'=>$this->_receive['EventKey'],
			);
		} else 
			return false;
	}
	
	public static function xmlSafeStr($str)
	{   
		return '<![CDATA['.preg_replace("/[\\x00-\\x08\\x0b-\\x0c\\x0e-\\x1f]/",'',$str).']]>';   
	} 
	
	/**
	 * 数据XML编码
	 * @param mixed $data 数据
	 * @return string
	 */
	public static function data_to_xml($data) {
	    $xml = '';
	    foreach ($data as $key => $val) {
	        is_numeric($key) && $key = "item id=\"$key\"";
	        $xml    .=  "<$key>";
	        $xml    .=  ( is_array($val) || is_object($val)) ? self::data_to_xml($val)  : self::xmlSafeStr($val);
	        list($key, ) = explode(' ', $key);
	        $xml    .=  "</$key>";
	    }
	    return $xml;
	}	
	
	/**
	 * XML编码
	 * @param mixed $data 数据
	 * @param string $root 根节点名
	 * @param string $item 数字索引的子节点名
	 * @param string $attr 根节点属性
	 * @param string $id   数字索引子节点key转换的属性名
	 * @param string $encoding 数据编码
	 * @return string
	*/
	public function xml_encode($data, $root='xml', $item='item', $attr='', $id='id', $encoding='utf-8') {
	    if(is_array($attr)){
	        $_attr = array();
	        foreach ($attr as $key => $value) {
	            $_attr[] = "{$key}=\"{$value}\"";
	        }
	        $attr = implode(' ', $_attr);
	    }
	    $attr   = trim($attr);
	    $attr   = empty($attr) ? '' : " {$attr}";
	    $xml   .= "<{$root}{$attr}>";
	    $xml   .= self::data_to_xml($data, $item, $id);
	    $xml   .= "</{$root}>";
	    return $xml;
	}
	
	/**
	 * 设置回复消息
	 * Examle: $obj->text('hello')->reply();
	 * @param string $text
	 */
	public function text($text='')
	{
		$FuncFlag = $this->_funcflag ? 1 : 0;
		$msg = array(
			'ToUserName' => $this->getRevFrom(),
			'FromUserName'=>$this->getRevTo(),
			'MsgType'=>self::MSGTYPE_TEXT,
			'Content'=>$text,
			'CreateTime'=>time(),
			'FuncFlag'=>$FuncFlag
		);
		$this->Message($msg);
		return $this;
	}
	
	/**
	 * 设置回复音乐
	 * @param string $title
	 * @param string $desc
	 * @param string $musicurl
	 * @param string $hgmusicurl
	 */
	public function music($title,$desc,$musicurl,$hgmusicurl='') {
		$FuncFlag = $this->_funcflag ? 1 : 0;
		$msg = array(
			'ToUserName' => $this->getRevFrom(),
			'FromUserName'=>$this->getRevTo(),
			'CreateTime'=>time(),
			'MsgType'=>self::MSGTYPE_MUSIC,
			'Music'=>array(
				'Title'=>$title,
				'Description'=>$desc,
				'MusicUrl'=>$musicurl,
				'HQMusicUrl'=>$hgmusicurl
			),
			'FuncFlag'=>$FuncFlag
		);
		$this->Message($msg);
		return $this;
	}
	
	/**
	 * 设置回复图文
	 * @param array $newsData 
	 * 数组结构:
	 *  array(
	 *  	[0]=>array(
	 *  		'Title'=>'msg title',
	 *  		'Description'=>'summary text',
	 *  		'PicUrl'=>'http://www.domain.com/1.jpg',
	 *  		'Url'=>'http://www.domain.com/1.html'
	 *  	),
	 *  	[1]=>....
	 *  )
	 */
	public function news($newsData=array())
	{
		$FuncFlag = $this->_funcflag ? 1 : 0;
		$count = count($newsData);
		
		$msg = array(
			'ToUserName' => $this->getRevFrom(),
			'FromUserName'=>$this->getRevTo(),
			'MsgType'=>self::MSGTYPE_NEWS,
			'CreateTime'=>time(),
			'ArticleCount'=>$count,
			'Articles'=>$newsData,
			'FuncFlag'=>$FuncFlag
		);
		$this->Message($msg);
		return $this;
	}
	
	/**
	 * 
	 * 回复微信服务器, 此函数支持炼师操作
	 * Example: $this->text('msg tips')->reply();
	 * @param string $msg 要发送的信息, 默认取$this->_msg
	 * @param bool $return 是否返回信息而不抛出到浏览器 默认:否
	 */
	public function reply($msg=array(),$return = false)
	{
		if (empty($msg)) 
			$msg = $this->_msg;
		$xmldata=  $this->xml_encode($msg);
		file_put_contents(__DIR__.'/log.log', date("Y-m-d H:i:s"). " " . '回复的xml:'.$xmldata.PHP_EOL, FILE_APPEND | LOCK_EX);
		if ($return)
			return $xmldata;
		else
			echo $xmldata;
	}
	
}