php 解决项目中多个自动加载冲突问题
在有的框架中的自动加载机制,在发现无法加载时, 直接报错, 而没有把控制权转交给下一个自动加载方法., 如我要引入阿里云日志服务接口sdk,该sdk中自带自动加载方法,如下:
<?php/** * Copyright (C) Alibaba Cloud Computing * All rights reserved */$version = '0.6.0';function Aliyun_Log_PHP_Client_Autoload($className) { $classPath = explode('_', $className); if ($classPath[0] == 'Aliyun') { if(count($classPath)>4) $classPath = array_slice($classPath, 0, 4); $filePath = dirname(__FILE__) . '/' . implode('/', $classPath) . '.php'; if (file_exists($filePath)) require_once($filePath); }}spl_autoload_register('Aliyun_Log_PHP_Client_Autoload');
PHP 数组占用内存分析
下面的做法会占用多大的内存?
list($appid,$openid) = ["testcontent","test"];
测试
$m0 = memory_get_usage();$k = range(1,200000);$m1 = memory_get_usage();echo round(($m1-$m0)/pow(1024,2),4) ."MB\n";foreach ($k as $i){ $n1 = "kk$i"; $n2 = "tt$i"; list($$n1,$$n2) = [$i,$i*3];}$m2 = memory_get_usage();echo round(($m2-$m1)/pow(1024,2),4) ."MB\n";$m1 = memory_get_usage();foreach ($k as $i){ $n1 = "kk$i"; $n2 = "tt$i"; $$n1 = $i+time(); $$n2 = 2*time();}$m2 = memory_get_usage();echo round(($m2-$m1)/pow(1024,2),4) ."MB\n";
PHP Redis相关操作大全
PHP Redis相关操作大全
连接数据库
$redis = new Redis();$redis->connect('127.0.0.1',6379);//链接redis服务// 参数// `host: string`,服务地址// `port: int`,端口号// `timeout: float`,链接时长 (可选, 默认为 0 ,不限链接时间)// 注: 在redis.conf中也有时间,默认为300$redis->pconnect('127.0.0.1',6379);//不会主动关闭的链接$redis->setOption();//设置redis模式$redis->getOption();//查看redis设置的模式$redis->ping();//查看连接状态
【php开发】实现微信公众号图文消息回复与access_token(代码示例)
本篇文章主要讲述的是PHP实现微信公众号图文消息回复与access_token,具有一定的参考价值,感兴趣的朋友可以了解一下。
//代码如下<?phpclass IndexAction extends Action {public function __construct(){}public function index(){//获得参数 signature nonce token timestamp echostr$nonce = $_GET['nonce'];$token = 'imooc';$timestamp = $_GET['timestamp'];$echostr = $_GET['echostr'];$signature = $_GET['signature'];//形成数组,然后按字典序排序$array = array();$array = array($nonce, $timestamp, $token);sort($array);//拼接成字符串,sha1加密 ,然后与signature进行校验$str = sha1( implode( $array ) );if( $str == $signature && $echostr ){//第一次接入weixin api接口的时候echo $echostr;exit;}else{$this->reponseMsg();}}// 接收事件推送并回复public function reponseMsg(){//1.获取到微信推送过来post数据(xml格式)$postArr = $GLOBALS['HTTP_RAW_POST_DATA'];//2.处理消息类型,并设置回复类型和内容/*<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[FromUser]]></FromUserName><CreateTime>123456789</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDATA[subscribe]]></Event></xml>*/$postObj = simplexml_load_string( $postArr );//$postObj->ToUserName = '';//$postObj->FromUserName = '';//$postObj->CreateTime = '';//$postObj->MsgType = '';//$postObj->Event = '';// gh_e79a177814ed//判断该数据包是否是订阅的事件推送if( strtolower( $postObj->MsgType) == 'event'){//如果是关注 subscribe 事件if( strtolower($postObj->Event == 'subscribe') ){//回复用户消息(纯文本格式) $toUser = $postObj->FromUserName;$fromUser = $postObj->ToUserName;$time = time();$msgType = 'text';$content = '欢迎关注我们的微信公众账号'.$postObj->FromUserName.'-'.$postObj->ToUserName;$template = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content></xml>";$info = sprintf($template, $toUser, $fromUser, $time, $msgType, $content);echo $info;/*<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[fromUser]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[你好]]></Content></xml>*/}}//当微信用户发送imooc,公众账号回复‘imooc is very good'/*<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[fromUser]]></FromUserName><CreateTime>12345678</CreateTime><MsgType><![CDATA[text]]></MsgType><Content><![CDATA[你好]]></Content></xml>*//*if(strtolower($postObj->MsgType) == 'text'){switch( trim($postObj->Content) ){case 1:$content = '您输入的数字是1';break;case 2:$content = '您输入的数字是2';break;case 3:$content = '您输入的数字是3';break;case 4:$content = "<a href='http://www.imooc.com'>慕课</a>";break;case '英文':$content = 'imooc is ok';break;} $template = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content></xml>";//注意模板中的中括号 不能少 也不能多$fromUser = $postObj->ToUserName;$toUser = $postObj->FromUserName; $time = time();// $content = '18723180099';$msgType = 'text';echo sprintf($template, $toUser, $fromUser, $time, $msgType, $content);}}*///用户发送tuwen1关键字的时候,回复一个单图文if( strtolower($postObj->MsgType) == 'text' && trim($postObj->Content)=='tuwen2' ){$toUser = $postObj->FromUserName;$fromUser = $postObj->ToUserName;$arr = array(array('title'=>'imooc','description'=>"imooc is very cool",'picUrl'=>'http://www.imooc.com/static/img/common/logo.png','url'=>'http://www.imooc.com',),array('title'=>'hao123','description'=>"hao123 is very cool",'picUrl'=>'https://www.baidu.com/img/bdlogo.png','url'=>'http://www.hao123.com',),array('title'=>'qq','description'=>"qq is very cool",'picUrl'=>'http://www.imooc.com/static/img/common/logo.png','url'=>'http://www.qq.com',),);$template = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><ArticleCount>".count($arr)."</ArticleCount><Articles>";foreach($arr as $k=>$v){$template .="<item><Title><![CDATA[".$v['title']."]]></Title> <Description><![CDATA[".$v['description']."]]></Description><PicUrl><![CDATA[".$v['picUrl']."]]></PicUrl><Url><![CDATA[".$v['url']."]]></Url></item>";}$template .="</Articles></xml> ";echo sprintf($template, $toUser, $fromUser, time(), 'news');//注意:进行多图文发送时,子图文个数不能超过10个}else{switch( trim($postObj->Content) ){case 1:$content = '您输入的数字是1';break;case 2:$content = '您输入的数字是2';break;case 3:$content = '您输入的数字是3';break;case 4:$content = "<a href='http://www.imooc.com'>慕课</a>";break;case '英文':$content = 'imooc is ok';break;} $template = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content></xml>";//注意模板中的中括号 不能少 也不能多$fromUser = $postObj->ToUserName;$toUser = $postObj->FromUserName; $time = time();// $content = '18723180099';$msgType = 'text';echo sprintf($template, $toUser, $fromUser, $time, $msgType, $content);}//if end}//reponseMsg endfunction http_curl(){//获取imooc//1.初始化curl$ch = curl_init();$url = 'http://www.baidu.com';//2.设置curl的参数curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//3.采集$output = curl_exec($ch);//4.关闭curl_close($ch);var_dump($output);}function getWxAccessToken(){//1.请求url地址$appid = 'wx08d5c2cb632bb5e4';$appsecret = '06d3444fb9abd8d00314eb4a38ad61a8';$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$appsecret;//2初始化$ch = curl_init();//3.设置参数curl_setopt($ch , CURLOPT_URL, $url);curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);//4.调用接口 $res = curl_exec($ch);//5.关闭curlcurl_close( $ch );if( curl_errno($ch) ){var_dump( curl_error($ch) );}$arr = json_decode($res, true);var_dump( $arr );}function getWxServerIp(){$accessToken = "6vOlKOh7r5uWk_ZPCl3DS36NEK93VIH9Q9tacreuxJ5WzcVc235w_9zONy75NoO11gC9P0o4FBVxwvDiEtsdX6ZRFR0Lfs_ymkb8Bf6kRfo";$url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$accessToken;$ch = curl_init();curl_setopt($ch, CURLOPT_URL,$url);curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);$res = curl_exec($ch);curl_close($ch);if(curl_errno($ch)){var_dump(curl_error($ch));}$arr = json_decode($res,true);echo "<pre>";var_dump( $arr );echo "</pre>";}}//class end