易栈网-膘叔

发文热力图 2025-12-07 ~ 2026-12-06
少 多 近 365 天共 27 篇
浏览模式: 标准 | 列表 | 图文
ThinkPHP的主从数据库配置

ThinkPHP的主从数据库配置

最近涉及到的项目中用到了thinkphp,我都N年没有碰过这玩意了。但现在还是需要用一用。正好那些能够下载得到的wxcms也可以用这个来优化一下性能。。其实我就一台服务器。装逼而已

于是转一下别人的文章,因为他其中特别写了一句:只要是exec就一定从主库走,query一定从从库走。所以建议query中不要有实际的更新和插入操作。以防万一
原文地址:http://www.verydemo.com/demo_c327_i782.html
前言:
Thinkphp 框架本身是支持读写分离的,如何做呢?
最简单的做法是在配置文件中修改:
'DB_TYPE'=> 'mysql',
'DB_DEPLOY_TYPE' => 1, //开打支持多服务器
'DB_RW_SEPARATE'=>true, //读写<strong>分离</strong>分开
'DB_HOST'=> '192.168.100.78,192.168.100.60', // 数据库服务器地址 master(写)/slave(读)
'DB_NAME'=>'test',
'DB_USER'=>'root',
'DB_PWD'=>'root',
'DB_PREFIX' => 'fav_',
默认第一台数据库78是主数据库,负责写入操作,第二台60负责读操作,Thinkphp 会自动识别读和写,然后分配链接对应的数据库,来达到读写分离的效果。
注意事项:
1. 如果要设置分布式数据库,暂时不支持DB_DSN方式配置。
2.如果你当前采用了分布式数据库,并且设置了读写分离的话,query方法始终是在读服务器执行,因此query方法对应的都是读操作,而不管你的SQL语句是什么。
3.如果你当前采用了分布式数据库,并且设置了读写分离的话,execute方法始终是在写服务器执行,因此execute方法对应的都是写操作,而不管你的SQL语句是什么。
因此,'DB_DSN'=> 'mysql:host=192.168.100.60;dbname=51fanli_cang', 这种方式不支持主从分离,需要改。
当采用原生态的sql语句进行写入操作的时候,要用execute,读操作要用query,切记,否则乱掉了。
搞定!
---EOF---
curl和wget,折腾几个小时的服务器

curl和wget,折腾几个小时的服务器

标题其实没什么,主要原因是。我在两台机器上部署了同样的程序,一个访问的时候一直显示404,一个就一直正常。死活找不到原因

1、背景

两台服务器,都是CentOS,一个可以公网访问,一个只能内网访问,因此一个可以在浏览器里打开,一个只能用命令行访问,习惯性的,我使用了wget

2、浏览器访问正常,命令行显示404

3、配置完全一样,问题不明。

wget的时候显示404,无输出。。也就导致没有任何文件信息被下载

4、使用的YII框架。结果,记录的LOG,分组了只记录了system.db,404的LOG未被记录

5、调整策略:

a.用curl(有朋友建议)

b.把LOG改为全部记录

于是就发现问题了。LOG里显示无默认控制器。CURL直接输出错误信息

原来,WGET看到http头是404后就不再下载内容,即使我有在文件里有输出,而CURL默认就是按浏览器访问的显示而正常输出在屏幕上。

问题解决。记录一下。就因为WGET和CURL的默认机制不一样。吐血三升

2B的阿里系产品经理

积怨以久了。我不知道这个行为是怎么出来的。。。说说我的步骤和问题吧

1、我有一个淘宝帐号A,我有一个阿里妈妈帐号

2、我要登录阿里妈妈,阿里妈妈说你不能登录了,请使用淘宝帐号吧。在不明所以的情况下,翻看了阿里妈妈的N个帖子。学会了绑定,但是说不能绑定是卖家的帐号,或者曾经当过卖家的帐号。

3、重新注册一个新的淘宝帐号B(用邮箱注册因为我就一个手机,原来的帐号已经绑定手机了)。他们又顺便帮我开通了支付宝(真是感谢啊。。。所幸支付宝还能和现在的帐户绑定,毕竟我身份证只有一个。这理由真扯蛋)

4、上周五刚注册时。阿里妈妈能够登录了。好开心

5、周一,再登录。突然提示说:帐户存在异常,要我验证后才能开通

6、输入用户名密码,下一步的时候提示我,请输入手机号,还说:“1.请您放心,手机号码仅用于系统生成短信编码,不会对外泄露”,于是我输入了我的手机(绑定A的手机),结果它提示我:“此手机已绑定其他账户,无法进行验证,建议更换其他号码操作”

首先我想问的是,如果你只是用于生成短信编码,你为什么还要一个未使用过的手机?其次,我明明是采用邮箱注册的,你不用邮箱来验证,却要使用手机?

7、咨询客服。客服说无法解决,一定要我绑定手机,还要我将原来的淘宝帐号绑定的手机解绑。

这真是一个笑话。好吧,至此,如果我不能登录,相当于我毁了一个7年的阿里妈妈帐号,毁了一个新的淘宝帐号、毁了一个支付宝帐号。

虽然阿里妈妈帐号没什么钱,只是我觉得负责这两个项目,或者说让我这样验证的产品经理很2B。不管是阿里妈妈的还是淘宝的。。这种思维真有意思。

0029ab3c100e9fd6b376599911182b42

转:WebSphere MQ7 在Ubuntu上的安装

转:WebSphere MQ7 在Ubuntu上的安装

先申明。。。参考本文,我并没有成功的搭建成功。因为我在执行第一个rpm --nodeps -ivh Runtime的时候。不是按照预想的安装在了/opt/mqm下面,而是提示我安装在/ filesystem下。所以我就没有办法下一步。因为在执行第二个安装的时候,它提示我没有在/opt/mqm下找到runtime。。。

不过我还是做个记录,因为我是debian系统。也许ubuntu下就会成功呢??原文来自:http://qtlkw.iteye.com/blog/744052

我转贴的并不全。下面的一些介绍我没有转,如果有兴趣可以移步原文,以下是转贴:

可参照文档http://publib.boulder.ibm.com/infocenter/wmqv7/v7r0/index.jsp?topic=/com.ibm.mq.amq1ac.doc/lq10120_.htm 和http://blog.csdn.net/lang_ysh/archive/2010/03/18/5391971.aspx

1. Download WebSphere MQ7 tar.gz
2. run tar -zxvf CZ4VDML.tar.gz
3(optional). 创建WebSphere MQ 必需的文件系统,命令如下:
# for product code
mkdir /opt/mqm
# for working data
mkdir /var/mqm
网上看到说MQ安装时候默认的目录就是这两个
4. 创造MQ用户和用户组,命令如下:
groupadd mqm
useradd -d /var/mqm -g mqm -G mqm mqm
# change password to "password"
passwd mqm
5. 为mqm用户组添加root组
cd /etc
vi group
找到mqm:x:1003:mqm增加",root",修改成: mqm:x:1003:mqm,root
6(optional). 调整系统参数(视需要而定,我安装时并没修改)
make following kernel changes (/etc/sysctl.conf ):
kernel.msgmni = 1024
kernel.shmmni = 4096
kernel.shmall = 2097152
kernel.sem = 500 256000 250 1024
fs.file-max = 32768
net.ipv4.tcp_keepalive_time = 300

To load these sysctl values immediately, enter the command sysctl -p.
If you do not issue the sysctl -p command, the new values are loaded when the system is rebooted.
7. 安装rpm
sudo apt-get install rpm
8. 转到WebSphere MQ解压后的目录中,获得WebSphere MQ授权,命令如下:
./mqlicense.sh -text_only
阅读全文后,选择1接受授权。
9. 安装WebSphere MQ客户端和服务端,命令如下:
rpm --nodeps -ivh MQSeriesRuntime-7.0.1-0.i386.rpm
rpm --nodeps -ivh MQSeriesSDK-7.0.1-0.i386.rpm
rpm --nodeps -ivh MQSeriesJava-7.0.1-0.i386.rpm
rpm --nodeps -ivh MQSeriesClient-7.0.1-0.i386.rpm
rpm --nodeps -ivh MQSeriesSamples-7.0.1-0.i386.rpm

rpm -nodeps -ivh MQSeriesServer-7.0.1-0.i386.rpm(如果只装客户端,这个没必要安装)
原因: 不加-nodeps会报:
root@localhost:/apps# rpm -ivh MQSeriesRuntime-7.0.1-0.i386.rpm
error: Failed dependencies:
/bin/sh is needed by MQSeriesRuntime-7.0.1-0.i386
10. 安装校验
rpm -qa | grep MQSeries
MQSeriesSDK-7.0.1-0
MQSeriesClient-7.0.1-0
MQSeriesServer-7.0.1-0
MQSeriesRuntime-7.0.1-0
MQSeriesJava-7.0.1-0
MQSeriesSamples-7.0.1-0

安装MQ Explorer还需要安装其它的包:

MQSeriesConfig-7.0.1-0.i386.rpm

MQSeriesEclipseSDK33-7.0.1-0.i386.rpm

MQSeriesJRE-7.0.1-0.i386.rpm

至此MQ安装完成。

--END--

转:MQ+PHP – Linking IBM’s WebSphere MQ to PHP

转:MQ+PHP – Linking IBM’s WebSphere MQ to PHP

我在上一篇博客在centos下处理PHP+WebSphere客户端中提到了一个英文网站:http://blog.phpdeveloper.org/?p=140,我也是参考它才完成了最终的配置,为此我也写了中文的说明,但感觉文笔不行,所以我还是贴上原文吧:

During a recent project at work I had to get PHP linked with IBM’s WebSPhere MQ software we have running on another internal server. Our goal was to use our existing web service to take the requests from external vendors and push their XML data back into the queue inside our firewall. Thankfully there’s an extension in PECL that does just that.

Here’s the basic steps I took – hopefully it’ll be useful to someone else out there in the same spot I was. This all assumes you’re working on a web server that doesn’t have an MQ server installed already:

  • Get the extension: Head over to the PECL page for mqseries and download the latest version. Unpack it into a directory on your local server
  • Get the MQ client libs: You’ll need to go to IBM’s website to download the latest client/libraries for your install (you’ll need an IBM ID to get to the downloads):
    • Go to the IBM page for the MQ client listing
    • Look for the “WebSphere MQ Clients” link under the “Related products and technologies” section and click on it
    • Scroll down to the “Download Package” section and choose from one of the mirror locations
    • Select your package from the list (I went with “Linux for System x86″ for our setup)
    • Click on the download link and fill out some required information (you didn’t think you were getting off that easy, did you?)
    • Agree to the terms and conditions and you’ll get a “Download Now” link
    • Drop the archive file (tar, tar.gz, etc) into your server and unpack into a temporary directory (mine had an issue unpacking into the local directory, not a subdirectory)
  • Install the package(s): Once you have the IBM software extracted, you should have a series of packages. You’ll need to install the “MQSeriesSDK” to get the right libraries in place to compile the PHP extension
  • Build the mqseries extension: Go into the mqseries directory and run “phpize”, “./configure” and “make” to create the .so file. The process should drop it into the default extensions directory.
  • If needed, move it: Be sure that the shared module for the extension is in the right directory for the PHP install to find it. (You can make a phpinfo() page if you’re not sure where that is.)
  • Update your php.ini: Add in a line to include the extension in your current setup. Remember, after any changes to the php.ini, you need to restart the web server.

Now for the fun part – if everything’s working and the extension shows up in your phpinfo() as active, give this script a shot and see if you can connect to your MQ server:

1 $mq_host_ip ='127.0.0.1';
2 $queue_name = 'HOST.REMOTE.Q';
3 $mq_server = 'WBRK_QM_U49';
4 $mqcno = array(
5 'Version' => MQSERIES_MQCNO_VERSION_2,
6 'Options' => MQSERIES_MQCNO_STANDARD_BINDING,
7 'MQCD' => array(
8 'ChannelName' => 'CLIENT.CHANNEL',
9 'ConnectionName' => $mq_host_ip,
10 'TransportType' => MQSERIES_MQXPT_TCP
11 )
12 );
13
14 // Connect to the MQ server
15 mqseries_connx($mq_server,$mqcno,$conn,$comp_code,$reason);
16 if ($comp_code !== MQSERIES_MQCC_OK) {
17 trigger_error('Cannot open connection to server: '.$mq_server,E_USER_ERROR);
18 }else{
19 echo 'Connection good!';
20 }

Obviously you’ll need to adjust the settings to fit your server, but at least this gives you a start.

--END

在centos下处理PHP+WebSphere客户端

在centos下处理PHP+WebSphere客户端

事先申明。我是借鉴了这篇博客:http://blog.phpdeveloper.org/?p=140

当然在其中遇到了不少问题,我在这里一一说明:
1、准备工作
a. pecl mqseries :http://pecl.php.net/package/mqseries
b. ibm mq client: http://www-01.ibm.com/software/integration/wmq/clients/
2、步骤:
1、先安装ibm mq client :下载地址最终是要翻墙的,我这里不教翻墙技术。下载完后,解开压缩,在centos下面,直接rpm -ivh
MQSeriesRuntime-7.5.0-3.x86_64.rpm,和rpm -ivh MQSeriesSDK-7.5.0-3.x86_64.rpm ,由于我们只是当成一个client库来连接,因此安装这两个就够了
2、编译mqseries。在这里我吃药了,我下载的是0.14版的,我一看更新时间是2014年。我想这是最新的总没问题吧。结果。。。./configure到最后的时候,说--with-libdir=lib64找不到,让我指定。可是我明明指定了也不能安装。于是google了一下,发现这曾经是一个BUG。在2012年解决掉了。在bug.php.net里。写的是bug fixed。所以我下载了0.13的,然后再安装,果然成功了
3、测试
<?php
$mq_host_ip ='127.0.0.1';
$queue_name = 'HOST.REMOTE.Q';
$mq_server = 'WBRK_QM_U49';
$mqcno = array(
'Version' => MQSERIES_MQCNO_VERSION_2,
'Options' => MQSERIES_MQCNO_STANDARD_BINDING,
'MQCD' => array(
'ChannelName' => 'CLIENT.CHANNEL',
'ConnectionName' => $mq_host_ip,
'TransportType' => MQSERIES_MQXPT_TCP
)
);

// Connect to the MQ server
mqseries_connx($mq_server,$mqcno,$conn,$comp_code,$reason);
if ($comp_code !== MQSERIES_MQCC_OK) {
trigger_error('Cannot open connection to server: '.$mq_server,E_USER_ERROR);
}else{
echo 'Connection good!';
}
$mq_server从手册上看得知这应该是:queue_manager,所以应该是manager的名称
如果你访问的mq不是默认的端口号,还要加上端口号。我在这里走了不少弯路。我原来connectionName是写成:127.0.0.1:1234,报错2387,意思是host not avarible。查了下资料,原来端口号应该是 ip(port),也就是说得:127.0.0.1(1234) 这样的形式才OK。于是改好后再测试就通过啦。
----
我是在一台干净的Centos上安装的。所以做了很多准备工作:
1、 yum install php php-devel(devel里面才有phpize)
2、yum install gcc gcc-c++ g++
然后在安装的时候还有提示:you will need re2c ...于是找到这个博客:http://denghai260.blog.163.com/blog/static/726864092012260242533/
里面说道:
wget http://sourceforge.net/projects/re2c/files/re2c/0.13.5/re2c-0.13.5.tar.gz/download
tar -zxvf re2c-0.13.5.tar.gz
cd re2c-0.13.5
./configure && make && make install
至此,遇到的所有的问题都解决了。
如何在微信里关闭窗口

如何在微信里关闭窗口

众所周知,微信里的网页就是一个webview。但是它是在程序里面,所以原生的window.close()是无效的。那么我们该怎么办?其实很简单就一句代码:

WeixinJSBridge.call('closeWindow');

你可以在你的链接或者button上加一句就OK了:

<a href="###" onclick="WeixinJSBridge.call('closeWindow');">CLOSE </a>

或者

<a href="javascript:WeixinJSBridge.call('closeWindow');">CLOSE</a>

试试看。窗口是不是关掉了?

ssdb的Yii cache扩展

ssdb的Yii cache扩展

google的leveldb越来越被很多人接受。国内的ideawu基于leveldb还写了一个ssdb的前置扩展用来实现了很多功能,比如标准的getset和hget,hset还有zset,zget,也实现了队列。当然pub/sub就没有办法实现了。毕竟它和redis还是有点区别。

基于标准的ssdb的类,写了个小扩展,扩展了Yii的Cache类:

class CSsdbCache extends CCache
{
/**
* @var string hostname to use for connecting to the redis server. Defaults to '127.0.0.1'.
*/
public $hostname = '127.0.0.1';
/**
* @var int the port to use for connecting to the ssdb server. Default port is 8888.
*/
public $port = 8888;
/**
* @var float
*/
public $timeout = 2000;
public $serializer = false;
public $_cache;
protected $_cachekeys = 'ssdb_cachekey';

public function init() {
parent::init();
}
/**
* @return SSDB
*/
public function getSsdbCache() {
if ($this->_cache !== null)
return $this->_cache;
else {
return $this->_cache = new SimpleSSDB($this->hostname, $this->port, $this->timeout);
}
}
public function getkeys() {
return $this->getSsdbCache()->hkeys($this->_cachekeys, "", "", $this->getSsdbCache()->hsize($this->_cachekeys));
}
/**
* Retrieves a value from cache with a specified key.
* This is the implementation of the method declared in the parent class.
* @param string $key a unique key identifying the cached value
* @return string|boolean the value stored in cache, false if the value is not in the cache or expired.
*/
protected function getValue($key) {
return unserialize($this->getSsdbCache()->get($key));
}

/**
* Stores a value identified by a key in cache.
* This is the implementation of the method declared in the parent class.
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function setValue($key, $value, $expire) {
$this->getSsdbCache()->hset($this->_cachekeys, $key, 1);
if ($expire > 0) {
//$expire += time();
return $this->getSsdbCache()->setx($key, serialize($value), (int) $expire);
}
else {
return $this->getSsdbCache()->set($key, serialize($value));
}
}
/**
* Stores a value identified by a key into cache if the cache does not contain this key.
* This is the implementation of the method declared in the parent class.
* @param string $key the key identifying the value to be cached
* @param string $value the value to be cached
* @param integer $expire the number of seconds in which the cached value will expire. 0 means never expire.
* @return boolean true if the value is successfully stored into cache, false otherwise
*/
protected function addValue($key, $value, $expire) {
return $this->setValue($key, $value, $expire);
}
/**
* Deletes a value with the specified key from cache
* This is the implementation of the method declared in the parent class.
* @param string $key the key of the value to be deleted
* @return boolean if no error happens during deletion
*/
protected function deleteValue($key) {
$this->getSsdbCache()->hdel($this->_cachekeys, $key);
return $this->getSsdbCache()->del($key);
}
/**
* @return boolean whether the flush operation was successful.
*/
protected function flushValues() {
$this->getSsdbCache()->multi_del($this->getkeys());
return $this->getSsdbCache()->hclear($this->_cachekeys);
}
}

其实代码很简单,不过由于ssdb默认没有serialize功能,所以在存储之前,得先主动的serialize,然后get的时候unserialize。不然就没有办法存储数组了。

由于ssdb没有flush功能。所以利用hget/hset将所有的key存储下来。flush的时候把hget获取的key读出来删除。然后再清掉这个hget的key

最后还有expire。ssdb里的setx第三个参数。。。居然不是expire,而是ttl。开始的时候,一直都当成expire。结果浪费了很长时间

如何在终端输出带颜色的字体?

如何在终端输出带颜色的字体?

终端显示颜色,在以前的想法当中,都是因为有了.profile的配色方案。而我一般也都是 采用默认的(snakevil是写过一个bash带颜色的方案的。我觉得太花哨了就没有使用)

为什么突然间又想到这个?是因为在使用PHP输出LOG的时候,千篇一率,从屏幕中找关键字很累,所以就想着,是不是用PHP也可以输出这种带颜色的关键字?当然,这是因为我正好看到了一个PHP是这么输出的,它就是laraval,它的工具(laraval.phar)在命令行的输出就是有不同颜色的,它给了我指引,意思就是,这个想法是可以实现的。

OK。找资料,知道在终端中用指定的字符来做为背景色和字体色(http://blog.csdn.net/acmee/article/details/6613060)

文中这样介绍:

一、shell下的实现方法

先来讲在shell下,如何实现。用echo命令就可以实现,参看以下例子:

echo -e "\033[32mHello, world!"

当你在终端里敲下这条命令后,是不是发现系统用绿色输出了"Hello,world!",不止如此,连之后的命令提示符都变成了绿色?不要着急,听我继续说。echo命令-e选项的作用是激活终端对反斜线转义符(即\)的解释。引号内\033用于引导非常规字符序列,在这里的作用就是引导设置输出属性,后边的[32m就是将前景色设置为绿色,字母m表示设置的属性类别,数字代表属性值。设置可以单独使用,例如:

echo -e "\033[0m"

这行命令的作用是恢复属性为默认值,也就是说0m设置项用于恢复默认值。现在你的终端是不是又一切正常了?

理解了这些,剩下的就简单了。用这种命令,除了设置文本前景色,还可以设置很多属性。下边列出其他的设置项:

--------------------------------------------------------------------------

\033[0m 关闭所有属性
\033[1m 设置高亮度
\033[4m 下划线
\033[5m 闪烁
\033[7m 反显
\033[8m 消隐
\033[30m 至 \33[37m 设置前景色
\033[40m 至 \33[47m 设置背景色
\033[nA 光标上移n行
\033[nB 光标下移n行
\033[nC 光标右移n行
\033[nD 光标左移n行
\033[y;xH设置光标位置
\033[2J 清屏
\033[K 清除从光标到行尾的内容
\033[s 保存光标位置
\033[u 恢复光标位置
\033[?25l 隐藏光标
\033[?25h 显示光标

--------------------------------------------------------------------------

各数字所代表的颜色如下:

字背景颜色范围:40----49
40:黑
41:深红
42:绿
43:黄色
44:蓝色
45:紫色
46:深绿
47:白色

字颜色:30-----------39
30:黑
31:红
32:绿
33:黄
34:蓝色
35:紫色
36:深绿
37:白色

另外,同类的多种设置项可以组合在一起,中间用分号(;)隔开。如下:

echo -e "\033[20;1H\033[1;4;32mHello,world\033[0m"

这行命令首先\033[20;1H将光标移动到终端第20行第1列,之后的\033[1;4;32m将文本属性设置为高亮、带下划线且颜色为绿色,然后输出Hello,world;最后\033[0m将终端属性恢复为默认值,这样就不会看到连命令完成后的命令提示符也变了样儿了。

通过以上各种命令的组合就可以实现对终端输出地复杂控制。

二、如何在C编程中实现?

理解了以上在Shell中的实现方法,关于在C中如何实现就很简单了。可以说只需要用printf函数代替上边的echo -e就OK了。参见下例:

int color = 32;

printf("\033[20;1H\033[1;4;%dmHello, world.\033[0m", color);

这个例子类似上边shell中最后那个例子,只是这里颜色值通过变量color来指定(当然,也可以直接指定)。

三、联想

看到这里你可能会想,是不是在其他编程语言里也可以用类似的方法实现对终端输出的控制呢?答案是肯定的!比如在python中,可以如下输出:

color=32

print “\033[20;1H\033[1;4;%dHello, world.\033[0m"%color

这个例子的效果跟上边C的例子是相同的。

其实在看到这一段之前,snakevil在自己的github的项目(https://github.com/snakevil/bashrc.x)中也介绍过,其实我相对还是喜欢ubuntu的默认配色,snakevil的这个配色我是真心不是特别喜欢。。

但究竟怎么用PHP输出呢?在用PHP输出之前,找了一下网络,发现已经有有用perl实现过了。那么说实在的。如果没有使用到一些特别的函数,其实php和perl实在是太象了,所以,可以直接参考(http://perldoc.perl.org/Term/ANSIColor.html),这里的代码,除了那个类外,都还是可以复刻的。于是,再随便找了点,果然还是有现成的PHP代码的(http://www.if-not-true-then-false.com/2010/php-class-for-coloring-php-command-line-cli-scripts-output-php-output-colorizing-using-bash-shell-colors/)

好吧。这个URL太长了。还是直接贴代码吧:

<?php

class Colors {
private $foreground_colors = array();
private $background_colors = array();

public function __construct() {
// Set up shell colors
$this->foreground_colors['black'] = '0;30';
$this->foreground_colors['dark_gray'] = '1;30';
$this->foreground_colors['blue'] = '0;34';
$this->foreground_colors['light_blue'] = '1;34';
$this->foreground_colors['green'] = '0;32';
$this->foreground_colors['light_green'] = '1;32';
$this->foreground_colors['cyan'] = '0;36';
$this->foreground_colors['light_cyan'] = '1;36';
$this->foreground_colors['red'] = '0;31';
$this->foreground_colors['light_red'] = '1;31';
$this->foreground_colors['purple'] = '0;35';
$this->foreground_colors['light_purple'] = '1;35';
$this->foreground_colors['brown'] = '0;33';
$this->foreground_colors['yellow'] = '1;33';
$this->foreground_colors['light_gray'] = '0;37';
$this->foreground_colors['white'] = '1;37';

$this->background_colors['black'] = '40';
$this->background_colors['red'] = '41';
$this->background_colors['green'] = '42';
$this->background_colors['yellow'] = '43';
$this->background_colors['blue'] = '44';
$this->background_colors['magenta'] = '45';
$this->background_colors['cyan'] = '46';
$this->background_colors['light_gray'] = '47';
}

// Returns colored string
public function getColoredString($string, $foreground_color = null, $background_color = null) {
$colored_string = "";

// Check if given foreground color found
if (isset($this->foreground_colors[$foreground_color])) {
$colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . "m";
}
// Check if given background color found
if (isset($this->background_colors[$background_color])) {
$colored_string .= "\033[" . $this->background_colors[$background_color] . "m";
}

// Add string and end coloring
$colored_string .= $string . "\033[0m";

return $colored_string;
}

// Returns all foreground color names
public function getForegroundColors() {
return array_keys($this->foreground_colors);
}

// Returns all background color names
public function getBackgroundColors() {
return array_keys($this->background_colors);
}
}

用法也比较简单:

<?php

// Create new Colors class
$colors = new Colors();

// Test some basic printing with Colors class
echo $colors->getColoredString("Testing Colors class, this is purple string on yellow background.", "purple", "yellow") . "\n";
echo $colors->getColoredString("Testing Colors class, this is blue string on light gray background.", "blue", "light_gray") . "\n";
echo $colors->getColoredString("Testing Colors class, this is red string on black background.", "red", "black") . "\n";
echo $colors->getColoredString("Testing Colors class, this is cyan string on green background.", "cyan", "green") . "\n";
echo $colors->getColoredString("Testing Colors class, this is cyan string on default background.", "cyan") . "\n";
echo $colors->getColoredString("Testing Colors class, this is default string on cyan background.", null, "cyan") . "\n";

当然,如果你觉得这个代码太麻烦,还有一个简单的方法:

function colorize($text, $status) {
$out = "";
switch($status) {
case "SUCCESS":
$out = "[42m"; //Green background
break;
case "FAILURE":
$out = "[41m"; //Red background
break;
case "WARNING":
$out = "[43m"; //Yellow background
break;
case "NOTE":
$out = "[44m"; //Blue background
break;
default:
throw new Exception("Invalid status: " . $status);
}
return chr(27) . "$out" . "$text" . chr(27) . "[0m";
}

echo colorize("Your command was successfully executed...", "SUCCESS");

四种颜色也够了。不过。。。NOTE的blue background。。。如果你的字符还是黑色的,真心看不到字符串了。

至此,介绍完毕,你可以试试(我已经用在项目中了)

NSQ

突然发现好象很久没有写过东西了。自从到了现在的公司,好象很多精力都放在了整理需求和写功能需求上面,代码啥的确实写的少了。

之所以提到nsq是因为我在写东西的时候发现点事情。其实好象可能用nsq处理会更好。
去年在想nsq的时候是写类似聊天室的玩意。当时就在想。nsq的一个topic是不是就相当于一个聊天室?在里面pub和sub,不就是一个正常的聊天过程吗?但最终还是没有用nsq。却自己实现了一个烂玩意
这两天我想到它,是因为nsq很多人还是当成一个队列来处理,那么基于go的队列?
如果我在抓取数据的时候,由主服务器向某一个topic发送要抓取的信息,其他订阅这个topic的程序就可以收到这个信息。然后开始解析并处理。这样是不是可以解决类似于中心服务器和其他采集服务器的事情呢?
然后找了下,phpnsq和pynsq都已经有人实现了。看起来好象是时候可以用它来尝试了?(http://bitly.github.io/nsq/clients/client_libraries.html)
最后提一句:phpnsq用的是reactPHP。
好了,半夜不写东西了。我继续 摸一下,好象nsqadmin有点意思。。

225adaf846d94c5b26ad04013a96a951

17bcdc1ddc3764c355a6296b27735da7