<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Dongsheng&#039;s thoughts &#187; php</title>
	<atom:link href="http://log.dongsheng.org/tag/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://log.dongsheng.org</link>
	<description></description>
	<lastBuildDate>Sat, 19 May 2012 13:25:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>PHP 5.4 released</title>
		<link>http://log.dongsheng.org/2012/03/06/php-5-4-released/</link>
		<comments>http://log.dongsheng.org/2012/03/06/php-5-4-released/#comments</comments>
		<pubDate>Mon, 05 Mar 2012 16:00:49 +0000</pubDate>
		<dc:creator>Dongsheng Cai</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://log.dongsheng.org/?p=136</guid>
		<description><![CDATA[PHP 5.4 just released a few days ago, it introduced new features I quite like: Short array syntax $langs = [...]]]></description>
			<content:encoded><![CDATA[<p>PHP 5.4 just released a few days ago, it introduced new features I quite like:</p>
<ol>
<li>Short array syntax
<pre>
$langs = ["php", "python", "lua"];
$person = ["firstname" => "Dongsheng", "lastname" => "Cai"];
</pre>
<p>I wish to see short class syntax, not sure how it will look like.
</li>
<li>&#8220;&lt;?=&#8221; is officially available all the time, there was an argument wether or not it should be used in project due to the short tag option in php.ini, now it&#8217;s out of question.</li>
<li><a href="http://www.php.net/manual/en/session.upload-progress.php">Session upload progress</a>, I will give it a try later</li>
<li><a href="http://au.php.net/traits">Traits</a> to ease the limitations of single-inheritance</li>
<li>Build-in web server! Start it using `php -S localhost:8080` command, this makes testing/debugging easier&#8230;a little bit</li>
<li>Massive performance improvement</li>
<li>No more safe mode, magic quotes, register_globals and allow call time pass reference</li>
</ol>
<p>I compiled it on Lion 10.7, my configure is here: <a href="https://gist.github.com/1978565">https://gist.github.com/1978565</a></p>
<p>Notes</p>
<p>- pcre library must be update-to-date, otherwise, some pattern modifiers won&#8217;t be available in php.<br />
- libjpeg isn&#8217;t installed on OS X, I installed it by <a href="https://github.com/mxcl/homebrew">homebrew</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://log.dongsheng.org/2012/03/06/php-5-4-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP成员函数中竟然也能用static声明变量</title>
		<link>http://log.dongsheng.org/2010/04/07/using-static-variable-in-class-method/</link>
		<comments>http://log.dongsheng.org/2010/04/07/using-static-variable-in-class-method/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 02:08:57 +0000</pubDate>
		<dc:creator>Dongsheng Cai</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://log.dongsheng.org/?p=84</guid>
		<description><![CDATA[又发现一个PHP Feature，PHP成员函数中竟然也能用static声明变量。怎么想怎么奇怪，你都创建新实例了，怎么static还是有效？不过有时候还蛮有用的。 class static_var { function __construct($name) { $this->name = $name; } function output() { static $counter; $counter++; echo $counter.'&#124;'.$this->name.'&#124;'; [...]]]></description>
			<content:encoded><![CDATA[<p>又发现一个PHP Feature，PHP成员函数中竟然也能用static声明变量。怎么想怎么奇怪，你都创建新实例了，怎么static还是有效？不过有时候还蛮有用的。</p>
<pre>
class static_var {
    function __construct($name) {
        $this->name = $name;
    }
    function output() {
        static $counter;
        $counter++;
        echo $counter.'|'.$this->name.'|';
    }
}
$t1 = new static_var('take 1');
$t1->output();
$t2 = new static_var('take 2');
$t2->output();
</pre>
]]></content:encoded>
			<wfw:commentRss>http://log.dongsheng.org/2010/04/07/using-static-variable-in-class-method/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Mac OS X 上构建 PHP5</title>
		<link>http://log.dongsheng.org/2009/02/28/build-php5-on-mac/</link>
		<comments>http://log.dongsheng.org/2009/02/28/build-php5-on-mac/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 04:47:44 +0000</pubDate>
		<dc:creator>Dongsheng Cai</dc:creator>
				<category><![CDATA[OS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://log.dongsheng.org/?p=36</guid>
		<description><![CDATA[在安装 PHP 前先安装 Mysql，Apache2 我用的是 Mac 内置的那个。下载 PHP 5.2.9 源码，然后执行： ./configure &#8211;prefix=/usr/local &#8211;with-iconv &#8211;with-gd &#8211;with-xmlrpc &#8211;enable-zip &#8211;with-openssl=/usr &#8211;enable-ftp &#8211;enable-sockets &#8211;enable-mbstring [...]]]></description>
			<content:encoded><![CDATA[<p>在安装 PHP 前先安装 Mysql，Apache2 我用的是 Mac 内置的那个。下载 PHP 5.2.9 源码，然后执行：<br />
./configure &#8211;prefix=/usr/local &#8211;with-iconv &#8211;with-gd<br />
&#8211;with-xmlrpc &#8211;enable-zip &#8211;with-openssl=/usr &#8211;enable-ftp &#8211;enable-sockets<br />
&#8211;enable-mbstring &#8211;enable-bcmath &#8211;with-curl &#8211;with-zlib-dir=/usr<br />
&#8211;with-mysqli=/usr/local/mysql/bin/mysql_config<br />
&#8211;with-mysql=/usr/local/mysql<br />
&#8211;with-config-file-path=/etc/php.ini &#8211;with-apxs2=/usr/sbin/apxs<br />
配置过程中会因为缺少某些开发包而出错，用 Mac port 安装一下就可以了</p>
<p>make &#038;&#038; sudo make install</p>
<p>这样创建的 libphp5.so 是无法被 Apache 载入的，需要用 lipo 处理一下 Apache 的二进制程序</p>
<p>/usr/sbin$ sudo cp httpd httpd-fat<br />
/usr/sbin$ sudo lipo httpd -thin i386 -output httpd</p>
<p>完了重启 Apache 就可以使用了：<br />
sudo apachectl restart</p>
]]></content:encoded>
			<wfw:commentRss>http://log.dongsheng.org/2009/02/28/build-php5-on-mac/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>原来 Mac 自带了 PHP</title>
		<link>http://log.dongsheng.org/2009/01/11/buildin-php-on-mac/</link>
		<comments>http://log.dongsheng.org/2009/01/11/buildin-php-on-mac/#comments</comments>
		<pubDate>Sun, 11 Jan 2009 09:30:47 +0000</pubDate>
		<dc:creator>Dongsheng Cai</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://log.dongsheng.org/?p=31</guid>
		<description><![CDATA[1. 启用 Apache，System Preferences -> Sharing -> Web Sharing 2. 编辑 /etc/apache2/httpd.conf，把启用 php5 的那一行，反注释掉，然后修改 DirectoryIndex，加上 index.php，然后创建 php 配置文件： mv /etc/php.ini.default [...]]]></description>
			<content:encoded><![CDATA[<p>1. 启用 Apache，System Preferences -> Sharing -> Web Sharing<br />
2. 编辑 /etc/apache2/httpd.conf，把启用 php5 的那一行，反注释掉，然后修改 DirectoryIndex，加上 index.php，然后创建 php 配置文件：</p>
<pre>
mv /etc/php.ini.default /etc/php.ini
</pre>
<p>重启 apache：</p>
<pre>
apachectl restart
</pre>
<p>3. 文档根目录在 /Library/WebServer/Documents，看了看 phpninfo 发现支持的扩展还不算太少 <img src='http://log.dongsheng.org/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://log.dongsheng.org/2009/01/11/buildin-php-on-mac/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>编译 PHP GD 模块</title>
		<link>http://log.dongsheng.org/2008/12/16/build-gd-extension-for-php/</link>
		<comments>http://log.dongsheng.org/2008/12/16/build-gd-extension-for-php/#comments</comments>
		<pubDate>Tue, 16 Dec 2008 09:20:31 +0000</pubDate>
		<dc:creator>Dongsheng Cai</dc:creator>
				<category><![CDATA[OS]]></category>
		<category><![CDATA[gd]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://log.dongsheng.org/?p=27</guid>
		<description><![CDATA[在编译 PHP 使用 &#8211;with-gd 选项无法正常将 GD 编译进 PHP，所以我尝试将 GD 编译成一个 PHP 扩展，方法如下： 1. 进入 PHP 源码 ./ext/gd 2. 运行 phpize [...]]]></description>
			<content:encoded><![CDATA[<p>在编译 PHP 使用 &#8211;with-gd 选项无法正常将 GD 编译进 PHP，所以我尝试将 GD 编译成一个 PHP 扩展，方法如下：<br />
1. 进入 PHP 源码 ./ext/gd<br />
2. 运行 phpize<br />
3. 进行 ./configure &#038;&#038; make &#038;&#038; make install<br />
编译完成后修改 PHP 配置文件，设置本扩展的路径。<br />
重启 Apache 问题依旧，查看 Apache 的错误日志，显示该模块无效，猜想可能是没有正确链接 GD 的支持库，看了一下 configure 的选项，意识到 libjpeg 和 libpgn 库没有安装，用 pacman 安装这两个库以后，重新编译 GD 库，问题解决。</p>
]]></content:encoded>
			<wfw:commentRss>http://log.dongsheng.org/2008/12/16/build-gd-extension-for-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在 Gentoo 上安装 Nginx + php</title>
		<link>http://log.dongsheng.org/2008/12/06/nginx-php-gentoo/</link>
		<comments>http://log.dongsheng.org/2008/12/06/nginx-php-gentoo/#comments</comments>
		<pubDate>Sat, 06 Dec 2008 09:06:29 +0000</pubDate>
		<dc:creator>Dongsheng Cai</dc:creator>
				<category><![CDATA[OS]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[fastcgi]]></category>
		<category><![CDATA[nginx]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://log.dongsheng.org/?p=23</guid>
		<description><![CDATA[从源码安装 Nginx ./configure --prefix=/usr --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx_error.log --http-log-path=/var/log/nginx_access.log --pid-path=/var/run/nginx.pid --http-client-body-temp-path=/var/tmp/nginx_client_body.tmp --http-proxy-temp-path=/var/tmp/nginx_proxy.tmp --http-fastcgi-temp-path=/var/tmp/nginx_fastcgi.tmp --with-http_stub_status_module --with-http_ssl_module --with-openssl=/usr/src/openssl （启用了监控和ssl模块，重新设置文件路径） make make install 注意，&#8211;with-openssl 指向的是 [...]]]></description>
			<content:encoded><![CDATA[<h4>从源码安装 Nginx</h4>
<pre>./configure --prefix=/usr --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx_error.log --http-log-path=/var/log/nginx_access.log --pid-path=/var/run/nginx.pid --http-client-body-temp-path=/var/tmp/nginx_client_body.tmp --http-proxy-temp-path=/var/tmp/nginx_proxy.tmp  --http-fastcgi-temp-path=/var/tmp/nginx_fastcgi.tmp --with-http_stub_status_module --with-http_ssl_module --with-openssl=/usr/src/openssl</pre>
<p>（启用了监控和ssl模块，重新设置文件路径）<br />
<code>make<br />
make install</code><br />
注意，&#8211;with-openssl 指向的是 openssl 的完整源码树。</p>
<h4>Nginx 的配置</h4>
<p>在 server 段中加入 root 指令，指向网站根目录，然后把 location / 段改为：</p>
<pre>location / {
  stub_status on;
  access_log off;
}</pre>
<p>这样就能在首页显示状态信息了，在这里用作测试。</p>
<h4>从源码安装 PHP 5.2.7</h4>
<pre>./configure --prefix=/usr --with-config-file-path=/etc/php --with-curl=shared --with-curlwrappers --enable-fastcgi --enable-force-cgi-redirect --with-openssl=shared --with-mysql=shared --with-mysqli=shared --enable-zip=shared --with-xmlrpc=shared --enable-mbstring --enable-pdo=shared --with-pdo-sqlite=shared --with-sqlite=shared --with-gd=shared --with-zlib
make
make install
cp php.ini-dist /etc/php/php.ini</pre>
<p>在这种情况下，模块会编译进 PHP 而不是作为模块动态加载，如果想要创建模块需要设置 shared 选项，比如我要把 PDO 作为模块加载：</p>
<pre>./configure --enable-pdo=shared --with-pdo-sqlite=shared --with-sqlite=shared</pre>
<p>如果是 apache 的话要加上  &#8211;with-apxs2 项以创建模块。</p>
<h4>设置 FASTCGI</h4>
<p>我尝试用 <a href="http://blog.kovyrin.net/2006/05/30/nginx-php-fastcgi-howto/">php-cgi 直接创建 FCGI 服务</a>，但无法成功，只好使用 lighttpd 的 spawn-fcgi 程序创建 FCGI 服务（这里偷懒用 emerge 装的 lighttpd）：</p>
<pre>spawn-fcgi -a 127.0.0.1 -p 9000 -C 5 -f /usr/bin/php-cgi</pre>
<p>最后修改 nginx 的配置文件使之调用 php FCGI 服务：</p>
<pre>location / {
 index index.php index.htm index.html;
}
location ~ .php$ {
 fastcgi_pass 127.0.0.1:9000;
 fastcgi_index index.php;
 include fastcgi_params;
 fastcgi_param SCRIPT_FILENAME /var/www$fastcgi_script_name;
}</pre>
<p>创建一个 phpinfo 页面测试一下吧！</p>
<h4>参考</h4>
<p><a href="http://wiki.codemongers.com/NginxInstallOptions">Nginx 编译参数说明</a><br />
<a href="http://php.mirrors.ilisys.com.au/manual/en/configure.php">PHP 编译参数说明</a><br />
<a href="http://blog.s135.com/post/366.htm">Nginx 0.7.x + PHP 5.2.6（FastCGI）搭建胜过Apache十倍的Web服务器（第4版）</a><br />
<a href="http://blog.kovyrin.net/2006/05/30/nginx-php-fastcgi-howto/">Nginx With PHP As FastCGI Howto</a></p>
]]></content:encoded>
			<wfw:commentRss>http://log.dongsheng.org/2008/12/06/nginx-php-gentoo/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在 PHP 中使用 XMLRPC</title>
		<link>http://log.dongsheng.org/2008/09/25/xmlrpc-php/</link>
		<comments>http://log.dongsheng.org/2008/09/25/xmlrpc-php/#comments</comments>
		<pubDate>Thu, 25 Sep 2008 13:22:03 +0000</pubDate>
		<dc:creator>Dongsheng Cai</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[xmlrpc]]></category>

		<guid isPermaLink="false">http://log.dongsheng.org/?p=16</guid>
		<description><![CDATA[这几天在研究 Moodle Network 组件，通过这个模块，各个孤立的系统被连接到了一起，实现了单点登录和资源共享（对于 e-learning 有很大的意义），通信协议是 XML-RPC，信息安全不是依赖 SSL 的方案，而是在采用了 XML 签名（不对称加密算法），想法非常好，但实现的不太完整，因为这个模块的原作者离开了在新西兰的公司，整得像个半完成作品。 今天看了一下代码，打算在这上面解决多 Moodle 系统的资源共享问题（包含点对点和 HUB 模式），之前没用过 XMLRPC，所以现在开始学一下，我用的是 PHP 的 XMLRPC [...]]]></description>
			<content:encoded><![CDATA[<p>这几天在研究 Moodle Network 组件，通过这个模块，各个孤立的系统被连接到了一起，实现了单点登录和资源共享（对于 e-learning 有很大的意义），通信协议是 XML-RPC，信息安全不是依赖 SSL 的方案，而是在采用了 XML 签名（不对称加密算法），想法非常好，但实现的不太完整，因为这个模块的原作者离开了在新西兰的公司，整得像个半完成作品。</p>
<p>今天看了一下代码，打算在这上面解决多 Moodle 系统的资源共享问题（包含点对点和 HUB 模式），之前没用过 XMLRPC，所以现在开始学一下，我用的是 PHP 的 XMLRPC 模块（比纯 PHP 实现快了不少）。</p>
<p>首先实现一个 XMLRPC 服务，我封装了一个简单的类：</p>
<pre>
class xmlrpc_server {
    private $server;
    public function __construct() {
        $this->server = xmlrpc_server_create();
    }
    public function add($name, $func) {
        xmlrpc_server_register_method($this->server, $name, $func);
    }
    public function run() {
        $req = file_get_contents('php://input');
        $response = xmlrpc_server_call_method($this->server, $req, null);
        echo $response;
        xmlrpc_server_destroy($this->server);
    }
}
</pre>
<p>调用这个类创建一个服务：</p>
<pre>
header('Content-Type: text/xml');
function func_add($method, $params) {
    return $params[0]+$params[1];
}
$xmlrpc = new xmlrpc_server;
$xmlrpc->add('add', 'func_add');
$xmlrpc->run();
</pre>
<p>PHP 客户端的调用是通过 HTTP 的 POST 方法，我用的是 <a href="http://code.anbutu.com/n-342">cURL</a> 扩展：</p>
<pre>
require_once('curl.class.php');
class xmlrpc_client {
    public function __construct($url, $autoload = false) {
        $this->connection = null;
        $this->url = $url;
        $this->connection = new curl;
        $this->methods = array();
        if ($autoload) {
            $resp = $this->call('system.listMethods', null);
            $this->methods = $resp;
        }
    }
    public function call($method, $params = null) {
        $post = xmlrpc_encode_request($method, $params);
        return xmlrpc_decode($this->connection->post($this->url, $post));
    }
}
header('Content-Type: text/plain');
$rpc = "http://10.0.0.10/api.php";
$client = new xmlrpc_client($rpc, true);
print_r($client->call('add', array(199,2)));
</pre>
<p>待续，下一步优化代码并实现 XML 签名技术。</p>
<p>资料：</p>
<p><a href="http://www.w3.org/TR/xmldsig-core/">XML Signature Syntax and Processing</a><br />
<a href="http://au2.php.net/manual/en/book.openssl.php">OpenSSH</a><br />
<a href="http://www.w3.org/TR/xmlenc-core/">XML Encryption Syntax and Processing</a></p>
<p>== TO BE CONTINUED ==</p>
]]></content:encoded>
			<wfw:commentRss>http://log.dongsheng.org/2008/09/25/xmlrpc-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>cURL 并发访问</title>
		<link>http://log.dongsheng.org/2008/07/16/curl-multiple-handlers/</link>
		<comments>http://log.dongsheng.org/2008/07/16/curl-multiple-handlers/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 03:10:23 +0000</pubDate>
		<dc:creator>Dongsheng Cai</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[curl]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://log.dongsheng.org/?p=8</guid>
		<description><![CDATA[通常情况下 PHP 中的 cURL 是阻塞运行的，就是说创建一个 cURL 请求以后必须等它执行成功或者超时才会执行下一个请求，curl_multi_* 系列函数使并发访问成功可能，PHP 文档对这个函数的介绍不太详细，用法如下： $requests = array('http://zz.dongsheng.org', 'http://www.google.com'); $main = curl_multi_init(); $results = array(); $errors [...]]]></description>
			<content:encoded><![CDATA[<p>通常情况下 PHP 中的 cURL 是阻塞运行的，就是说创建一个 cURL 请求以后必须等它执行成功或者超时才会执行下一个请求，curl_multi_* 系列函数使并发访问成功可能，PHP 文档对这个函数的介绍不太详细，用法如下：</p>
<pre>$requests = array('http://zz.dongsheng.org', 'http://www.google.com');
$main    = curl_multi_init();
$results = array();
$errors  = array();
$info    = array();
$count   = count($requests);
for($i = 0; $i < $count; $i++) {
  $handles[$i] = curl_init($requests[$i]);
  var_dump($requests[$i]);
  curl_setopt($handles[$i], CURLOPT_URL, $requests[$i]);
  curl_setopt($handles[$i], CURLOPT_RETURNTRANSFER, 1);
  curl_multi_add_handle($main, $handles[$i]);
}
$running = 0;

do {
  curl_multi_exec($main, $running);
} while($running > 0);

for($i = 0; $i < $count; $i++)
{
  $results[] = curl_multi_getcontent($handles[$i]);
  $errors[]  = curl_error($handles[$i]);
  $info[]    = curl_getinfo($handles[$i]);
  curl_multi_remove_handle($main, $handles[$i]);
}
curl_multi_close($main);
var_dump($results);
var_dump($errors);
var_dump($info);
</pre>
<p>通过这种方式就可以实现多任务并发执行，更详细的用法看我写的这个类：<a href="http://code.anbutu.com/n-342">curl class</a></p>
]]></content:encoded>
			<wfw:commentRss>http://log.dongsheng.org/2008/07/16/curl-multiple-handlers/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Page Caching using apc
Database Caching 3/28 queries in 0.017 seconds using apc
Object Caching 451/510 objects using apc

Served from: log.dongsheng.org @ 2012-05-20 16:21:42 -->
