views:

342

answers:

4

Im building a codeigniter app which uses json_encode to provide ajax data in many places... today I learned that the server has php 5.1.6 which doesnt support this method (or json_decode).. what can I do?? please help.

A: 

The json_decode is added since (PHP 5 >= 5.2.0, PECL json >= 1.2.0), it is supported in your version too, you should give it a try :)

Web Logic
+4  A: 

There is an emulation of json_encode() in upgradephp. Just include() that script, and you don't need to rewrite anything.
As alternative you can use PEAR::PHP_Compat. IIRC it has an emulation of that too.
(And there is a third alternative implementation floating around; but it's object-style and not as fast.)

mario
Well, I didn't know those existed. Thanks for pointing them out, all I have experience of is Services_JSON and Zend_Json.
Geoff Adams
http://pear.php.net/package/PHP_Compat has the nicer code, and you can cherry-pick only whatever function you need; but it's less complete currently. And actually I think Zend_Json is better for development, because it has error reporting. Where the native PHP json_decode() has none.
mario
+1 never knew about this one
Thorpe Obazee
+2  A: 

You could use Services_JSON if CodeIgniter can use it instead of PHP 5.2's json_* functions - I know Zend_Framework has Zend_Json which uses json_* functions if available, otherwise it uses its own implementation in userland PHP code as a fallback.

Of course, if you have access to the server to compile PHP, you could try the extension or upgrading to PHP 5.2 (a better solution).

EDIT: I would take the route of compatibility layers as mentioned above.

Geoff Adams
A: 

I needed json_encode and json_decode for jquery grid. I tried upgradephp but json_encode didnt seem to work with jquery grid so I deleted that function from the file and added this one. json_decode seems to work just fine though.

NachoF