最近在維護一個舊網站,該網站用的是PHP4,因為使用一些新的技術,其中資料傳遞都是用JSON這種格式,但在PHP4是不援原生的json_encode() 跟 json_decode(),必須自己寫。
首下須要下載JSON的lib:點此下載
解壓解後可以看到有一個「JSON.php」檔,這就是我們需要的lib檔,首先把他include進來:
1 |
include("JSON.php"); |
接著寫下判斷,如果沒有原生json_encode() 跟 json_decode(),就到lib中去找:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
if( !function_exists('json_encode') ) { function json_encode($data) { $json = new Services_JSON(); return( $json->encode($data) ); } } if( !function_exists('json_decode') ) { function json_decode($data) { $json = new Services_JSON(); return( $json->decode($data) ); } } |
如此就可以使用json_encode() 跟 json_decode()囉