Thursday, May 31, 2012
PHP Benchmarking Tools
2. flood: http://httpd.apache.org/test/flood/
3. Web Capacity Analysis Tool: http://support.microsoft.com/kb/231282
reference: http://phplens.com/lens/php-book/optimizing-debugging-php.php
Sunday, May 13, 2012
Use Event Dispatcher in Symfony2
$eventDispatcher = $this->container->get('event_dispatcher');
Sunday, May 06, 2012
Compile PHP binary with Lua extension on Windolws 7
- download and install Microsoft Windows SDK for Windows 7 and .NET Framework 3.5 SP1
- download PHP source code (php-5.3.11.tar.bz2) from: http://windows.php.net/download/
- download PHP SDK (php-sdk-binary-tools-20110915.zip) from: http://windows.php.net/downloads/php-sdk/
- download PHP Dependable libs (deps-5.3-vc9-x86.7z) from: http://windows.php.net/downloads/php-sdk/
- download PHP PECL Lua extension source code (lua-0.9.4.tgz) from: http://pecl.php.net/package/lua
- download and install Lua 5.1 from http://code.google.com/p/luaforwindows/downloads/list
install Lua at C:\Lua - create a directory at C:\php-sdk, unzip the php-sdk-binary-tools-20110915.zip into this directory
- open Microsoft Windows SDK v7.0 CMD Shell
- enter those commands below:
setenv /x86 /win7 /Release
cd C:\php-sdk
bin\phpsdk_setvars.bat
bin\phpsdk_buildtree.bat php-5.3.11 - unzip php-5.3.11.tar.bz2 into C:\php-sdk\php-5.3.11\vc9\x86\php-5.3.11
- create a directory at C:\php-sdk\php-5.3.11\vc9\x86\deps, unzip deps-5.3-vc9-x86.7z into this directory
- create a directory at C:\php-sdk\php-5.3.11\vc9\x86\pecl, unzip lua-0.9.4.tgz into C:\php-sdk\php-5.3.11\vc9\x86\pecl\lua
- back to Microsoft Windows SDK v7.0 CMD Shell, and enter those commands below:
cd C:\php-sdk\php-5.3.11\vc9\x86\php-5.3.11
buildconf
configure --disable-zts --enable-snapshot-build --with-lua=C:\Lua\5.1
nmake
nmake snap - go to the directory C:\php-sdk\php-5.3.11\vc9\x86\php-5.3.11\Release, you can get the PHP Winodows Binaries: pecl-5.3.11-nts-Win32-VC9-x86.zip and php-5.3.11-nts-Win32-VC9-x86.zip
- enjoy to use the binaries!
troubleshooting:
- "ext\calendar\jewish.c(324) : error C2001: newline in constant": open two files: jewish.c and calendar.c, re-save them with UTF-8 encoding.
- cd Release\php-5.4.7
..\..\..\..\bin\zip.exe -9 -q -r ..\php-5.4.7-nts-Win32-VC9-x86.zip .
The system cannot find the path specified.
cd ..\..
cd Release\pecl-5.4.7
..\..\..\..\bin\zip.exe -9 -q -r ..\pecl-5.4.7-nts-Win32-VC9-x86.zip .
The system cannot find the path specified.
cd ..\..
cd Release\php-test-pack-5.4.7
..\..\..\..\bin\zip.exe -9 -q -r ..\php-test-pack-5.4.7.zip .
The system cannot find the path specified.
cd ..\..
cd Release
..\..\..\..\bin\zip.exe -9 -q php-debug-pack-5.4.7-nts-Win32-VC9-x86.zip *.pdb
The system cannot find the path specified.
cd
Z:\PHP-SDK\_php54\vc9\x86\php-5.4.7-src\Release
cd
Z:\PHP-SDK\_php54\vc9\x86\php-5.4.7-src\Release
..\..\..\..\bin\zip.exe -9 -q -r php-devel-pack-5.4.7-nts-Win32-VC9-x86.zip php-5.4.7-devel-VC9-x86
The system cannot find the path specified.
cd ..\..
open Makefile, added a line as below after "PHP_SRC_DIR =C:\php-sdk\php-5.3.11\vc9\x86\php-5.3.11":
ZIP="$(PHP_SRC_DIR)\..\..\..\..\bin\zip.exe"
- There is no php_lua.dll.
The lua extension is static only.
reference1: Build your own PHP on Windows
reference2: Windows 中编译 PHP5.4 + xdebug
reference3: /dev/php/php-on-windows
pre-build DLLs: http://downloads.php.net/pierre/
Friday, February 17, 2012
Facebook 檢查是否有點擊讚按鈕 (Likes)
$appId = '1234567890'; // Facebook App Id
$res = $this->facebook->api('/me/likes/' . $appId);
回傳值:
{
"data": [
{
"name": "應用程式名稱",
"category": "Application",
"id": "1234567890",
"created_time": "2012-02-17T07:54:54+0000"
}
],
"paging": {
"next": ""
}
}
use FQL:
$appId = 1234567890;
$query = array(
'likes' => "SELECT uid, page_id, type, profile_section,
created_time FROM page_fan
WHERE uid = me() AND
page_id = "' . $appId . '",
);
/**
* 回傳值
* $response = array(
* array(
* 'name' => 'likes',
* 'fql_result_set' => array(
* array(
* "created_time" => "1329468255",
* "page_id" => "1234567890",
* "profile_section" => "other",
* "type" => "APP",
* "uid" => "100000119629520"
* )
* )
* )
* );
*/
$responses = $this->facebook->api(
array(
'method' => 'fql.multiquery',
'queries' => $query
)
);
Facebook Javascript Like button click event:
FB.Event.subscribe('edge.create',
function(response) {
//alert('You liked the URL: ' + response);
}
);
Monday, January 02, 2012
Doctrine2 ORM add query result cache and delete cache
// add query result cache
$query = $this->createQueryBuilder('i')
->where('i.delete = false')
->getQuery();
$query->useResultCache(true, 3600, 'allItems');
$items = $query->getArrayResult();
// delete cache
$em->getConfiguration()
->getResultCacheImpl()->delete('allItems');
Saturday, December 17, 2011
Develop Symfony2 with Eclipse
1. Download Eclipse IDE for Java EE Developers
2. Extract the archive file to where you want to install Eclipse.
3. Open Eclipse, Go to Help->Install New Software...
4. Add Symfony-2-Eclipse-Plugin repository: http://pulse00.github.com/Symfony-2-Eclipse-Plugin/
5. Install All of them. (PDT Extensions, PHP Development Tools, Symfony, YEdit)
6. Add Eclipse Color Theme plugins repository: http://eclipse-color-theme.github.com/update
7. Install Eclipse Color Theme
8. Download Eclipse Symfony2 theme
9. Go to Window->Preferences->General->Appearance->Color Theme , and install the Symfony2 theme.
10. ALL DONE! ENJOY Symfony2!
ex1. Go to PHP->Code Style->Formatter(PDT Tooles) Change Active profile to "Symfony [built in]"
ex2. Edit theme color in Twig: Normal, String
ex3. Edit theme color in YEdit: Scalars, Documents, Default
Tuesday, November 08, 2011
Javascript and PHP AES encrypt decrypt library
This is a Javascript AES encrypt decrypt library
AES Advanced Encryption Standard
And this is a PHP AES encrypt decrypt library
AES in PHP
Friday, November 04, 2011
Symfony theme for NetBeans
A nice guy make a symfony color theme for NetBeans. You can download from his googlecode page: Symfony Color Scheme for NetBeans v1.3
解決 DejaVu Sans Mono 字型在 NetBeans 下中文顯示成方塊的問題
-
找到 NetBeans 安裝路徑, ex: C:\Program Files\NetBeans 7.0.1
進入 etc 資料夾 ex: C:\Program Files\NetBeans 7.0.1\etc
打開 netbeans.conf 並搜尋 netbeans_jdkhome 參數
該參數存放的是 JRE 的路徑 ex:"C:\Program Files\Java\jre6" - 進入 JER 的路徑, 並進到 lib 資料夾 ex: C:\Program Files\Java\jre6\lib
- 複製一份 fontconfig.properties.src 並更改檔名成 fontconfig.properties
-
編輯 fontconfig.properties 檔
新增下列的內容 filename.DejaVu_Sans_Mono=DejaVuSansMono.ttf filename.DejaVu_Sans_Mono_Bold=DejaVuSansMono-Bold.ttf filename.DejaVu_Sans_Mono_Bold_Oblique=DejaVuSansMono-BoldOblique.ttf filename.DejaVu_Sans_Mono_Oblique=DejaVuSansMono-Oblique.ttf 修改下列的內容 sansserif.plain.alphabetic=DejaVu Sans sansserif.bold.alphabetic=DejaVu Sans Bold sansserif.italic.alphabetic=DejaVu Sans Oblique sansserif.bolditalic.alphabetic=DejaVu Sans Bold Oblique monospaced.plain.alphabetic=DejaVu Sans Mono monospaced.bold.alphabetic=DejaVu Sans Mono Bold monospaced.italic.alphabetic=DejaVu Sans Mono Oblique monospaced.bolditalic.alphabetic=DejaVu Sans Mono Bold Oblique sequence.sansserif.x-windows-950= alphabetic,chinese-ms950,dingbats,symbol,chinese-ms950-extb sequence.monospaced.x-windows-950= alphabetic,chinese-ms950,dingbats,symbol,chinese-ms950-extb - 做完上述的修改後, 再進入 NetBeans -> Tools -> Options -> Fonts & Colors -> Syntax -> Default -> Font, 選擇 Monospaced 字型, 這樣就可以正確地顯示 DejaVu Mono 的字型了, 收工!
Monday, October 24, 2011
Monday, September 19, 2011
Benchmarks for mt_rand() and rand()
Benchmarks results as below:
mt_rand() 10000000 times for 4.7026278972626 seconds each one average is 0.00000047 seconds rand() 10000000 times for 4.7438910007477 seconds each one average is 0.00000047 seconds
The test machine is Amazon EC2 High-CPU Instances (20 ECU)
Another test as below:
mt_rand() 10000000 times for 2.4974269866943 seconds each one average is 0.00000025 seconds rand() 10000000 times for 2.4646849632263 seconds each one average is 0.00000025 seconds
The test machine is Amazon EC2 Micro Instances (1 ECU)
Wednesday, August 31, 2011
Install PHPUnit on Windows 7
First you need install a Zend Server in C:\Zend folder
Run commands below
# pear channel-update pear.php.net # pear upgrade pear # pear channel-discover pear.phpunit.de # pear channel-discover pear.symfony-project.com # pear channel-discover components.ez.no # pear clear-cache # pear update-channels # pear install --alldeps phpunit/PHPUnit
DONE!
Friday, August 26, 2011
32bit 系統下 json_decode 遇到 BIGINT 的問題
在 32bit 系統下處理 Facebook 的 uid 時, 一定會遇到 json_decode 時, php 把 uid 轉成 float 的情形, 大部分的情形只要把 uid 轉成 string 就能做其他的事了, 以下是正確的轉型技巧
$json = '{"uid":100000123456789}';
$arr = json_decode($json, true);
// $arr = array(1) {
// ["uid"]=>
// float(1.0000012345678E+14)
// }
$s2 = sprintf ( "%.0f", $arr['uid'] );
// $s2 = string(15) "100000123456789"
Monday, August 08, 2011
jQuery file upload plugin: AjaxFileUpload
We can use AjaxFileUpload make a file upload process.
Basic usage:
function ajaxFileUpload()
{
$.ajaxFileUpload({
url:'upload.php',
secureuri:false,
fileElementId:'fileToUpload',
data:{name:'logan', id:'id'},
success: function (data, status) {
alert($(data).text());
}
});
return false;
}
Official Guide have more detail usage.
Wednesday, July 20, 2011
CentOS install Zend Server CE
Follow the official guide "RPM Installation (RHEL, CentOS, Fedora and OEL)" first.
I install Zend Server CE with "Manually Installing Zend Server" guide. But my Zend Server repository is downloaded from here, enter the Linux tab, download the "Zend Server (DEB/RPM Installer Script)" package. Use the "zend.rpm.repo" repository file.
Then, you can finish the installation with "yum install zend-server-ce-php-5.3"
Link the /usr/bin/php to /usr/local/zend/bin/php
ln -s /usr/local/zend/bin/php /usr/bin/php
Or use "ln -f -s /usr/local/zend/bin/php /usr/bin/php"
Troubleshooting:
1. "Cannot load /usr/local/zend/lib/apache2/libphp5.so into server"
solution: close SELINUX, /etc/selinux/config >> SELINUX="disabled"
Sunday, June 26, 2011
在 Windows 版的 Zend Server CE 安裝 PEAR
在使用 ZendServer\bin\go-pear.bat 的時候, 會出現 phar...does not have a signature 的錯誤, 這時候只需要修改 go-pear.bat 的內容
original: %PHP_BIN% -d output_buffering=0 -d PEAR\go-pear.phar change to: %PHP_BIN% -d output_buffering=0 -d phar.require_hash=0 PEAR\go-pear.phar
這樣就可以解決無法安裝 PEAR 的問題
PEAR 安裝完後, 如果有發生 Structures_Graph 的 library 找不到, 去 Pear 下載 Structures_Graph 的 package 回來放到 PEAR 的資料夾, 再執行下面幾個指令:
pear channel-update pear.php.net pear upgrade pear
最後再 Zend Server CE 內設定 include_path 加入 pear 套件的路徑
如果 go-pear.bat 已經加好了, 就重新啟動 Zend Server 即可使用 pear 套件了
資料來源: http://katsande.com/installing-phpunit-for-php-5-3-on-zendserver
Friday, June 24, 2011
PHP DEBUG 工具 debug_print_backtrace
發現到兩個有用的 debug function
debug_print_backtrace 和
debug_backtrace
debug_print_backtrace() 的功用是將目前程式區塊呼叫的位置開始, 一層一層往上找呼叫的位置, 可以將程式 trace 到最一開始的進入點, 這樣的方式可以有效的找到整個程式是從那一段開始出錯的, Zend_Db 也有實作這樣的功能, 沒想到原來 PHP 裡就已經內建了
debug_print_backtrace() 和 debug_backtrace() 不同的地方在於, debug_backtrace() 是未被格式化的資料, debug_backtrace() 回傳的是一份矩陣的 trace 資料, 而 debug_print_backtrace() 回傳的是已經備格式化過的文字, debug_print_backtrace() 比較方便用來快速檢視, 但若是要寫 LOG 或其他用途, debug_backtrace() 是比較好的選擇
Tuesday, June 07, 2011
camelize and underscore
/**
* Returns given $lower_case_and_underscored_word as a camelCased word.
*
* @param string $lower_case_and_underscored_word Word to camelize
* @return string Camelized word. likeThis.
* @access public
* @static
*/
function camelize($lowerCaseAndUnderscoredWord) {
$replace = str_replace(" ", "", ucwords(str_replace("_", " ", $lowerCaseAndUnderscoredWord)));
return $replace;
}
/**
* Returns an underscore-syntaxed ($like_this_dear_reader) version of the $camel_cased_word.
*
* @param string $camel_cased_word Camel-cased word to be "underscorized"
* @return string Underscore-syntaxed version of the $camel_cased_word
* @access public
* @static
*/
function underscore($camelCasedWord) {
$replace = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $camelCasedWord));
return $replace;
}
Friday, May 20, 2011
資料快取的概念
class test
{
protected $_tmp = array();
public function foo()
{
$id = '123';
$tmp = $this->get_tmp($id);
echo '';
var_dump($tmp);
echo '';
}
public function bar()
{
$id = '223';
$tmp = $this->get_tmp($id);
echo '';
var_dump($tmp);
echo '';
}
public function get_tmp($id)
{
// 已經有資料時直接回傳
if (count($this->_tmp) > 0)
{
if (isset($this->_tmp[$id]))
{
return $this->_tmp[$id];
}
return FALSE;
}
// 取得資料
$tmp = array(
'123' => array('id' => '123', 'name' => 'xdy'),
'223' => array('id' => '223', 'name' => 'xdasdfsafdy'),
'323' => array('id' => '323', 'name' => 'xsdfsdfdy'),
'423' => array('id' => '423', 'name' => 'xsadasdasdffdy'),
);
// 作快取
$this->_tmp = $tmp;
// 檢查是否有需要的資料
if (isset($this->_tmp[$id]))
{
return $this->_tmp[$id];
}
return FALSE;
}
}
$test = new test();
$test->foo(); // 抓取的資料會做第一次的快取
$test->bar(); // 這次就會直接從快取抓資料
$tmp = $this->get_tmp('323'); // 這次也是直接抓快取資料
Monday, March 28, 2011
MVC Frameworks Document (1)
/**
* Example Controller
* 網址範例 http://localhost/api.php?c=api_welcomes&a=index&p[]=p1&p[]=p2
* 參數 p 是一個矩陣, 可以帶到 action 使用
* 目前 index 這個 actin 有兩個引數 ($p1 和 $p2)
* 所以要帶兩個參數 p (p[]=p1&p[]=p2) 來滿足該 action 的引數需求
*/
class STOCK_ApiWelcomesController extends STOCK_AppController
{
public function index($p1, $p2)
{
// using argument example
d($p1);
d($p2);
// load model example
$this->load->model('example');
$this->m_example->info();
$this->load->model('example2');
$this->m_example2->info();
// load config example
$config = $this->load->config('auth_exception');
// set view data
$this->view->set('abc.def', 123456);
$this->view->set('abc.f', 456);
$this->view->set('abc.def.xyz', 6);
// render a element
$this->view->render_element(
'example', // element name
array('test' => 'this is a element test (1).')
);
// set veiw layout name
$this->view->set_layout('');
// render a layout
$this->view->render();
}
}
./models/example.php
class STOCK_Example extends STOCK_AppModel
{
public function __construct()
{
parent::__construct();
}
public function info()
{
$this->load_database();
d($this->db);
$this->load_memcache();
d($this->mc);
// load model is also avaliable!
$this->load_model('user');
d($this->m_user);
}
}
./views/api_welcomes/index.php
<p>
---------- This is work! ---------- line 2 ----------
</p>
<?php echo d($abc); ?>
<?php
$this->render_element(
'example',
array('test' => 'this is a element test (2).')
);
?>
./views/elements/example.php
<div><?php echo $test; ?></div>
在任何地方插入下面的程式, 可以完成事件的處理 (Event Handle), event 的程式放在 ./events/ 下, 檔名即為 event name
include(event('example'));