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.
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.
(There are further alternative implementation floating around; but often object-style and not as fast and designed to emulate the core function.)
You can set them up with auto_prepend_file= even. If you just want the JSON extension enabled, there are other sources of course.
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.
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 :)
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.
In fact, 5.1.6 supports json_decode and json_encode, but they can be a bit weird. As in, if you feed it invalid JSON, such as if you have a blank key, it will die without any warnings or errors. But I use json_encode and json_decode every day in a 5.1.6 environment. It's totally possible.
Related
My website has been running for a long time.
Currently upgraded to php 5.6 and maria db 10.1.44.
The utf8_encode function was used in a lot of php code.
But now I have to remove it so the characters are output correctly.
However, I can't edit a lot of php code ... Is there a way to invalidate the utf8_encode function?
Or is it possible to override the php core function?
Yes you could do so by making some changes in the php.ini file as
Disable PHP Functions
You could override utf8_encode() with function_override() but I can not recommend it.
Instead, you better fix the existing code yourself. When you use an IDE like Eclipse PDT, you can do a global search in all files and folders, and remove the calls to utf8_encode() rather quickly.
While PHP 5.6's default encoding has been changed to UTF-8, that version is already end-of-life: https://www.php.net/supported-versions.php. After moving up to 5.6, consider migrating your code to PHP 7.2 at least.
Because PHP 5.1.6. doesn't include the json_decode function, I've downloaded an alternative and included it myself. But for some reason it's really really slow.
This one for example I tried:
http://mike.teczno.com/JSON/JSON.phps
I have tried a few other alternatives.
Does somebody know a better alternative to decode json faster or is it impossible in PHP < 5.2
I am running PHP version 5.1.6 currently, but I would have an application which makes pretty heavy use of the json_encode and json_decode functions, as such, I would like to add these functions to my server's install of PHP (as these functions only ship with PHP versions PHP 5.2 +)
What is the best way of doing this. I'm not too down with the whole Terminal approach so if there was another way that would be great
Many thanks in advance
If you're unable to upgrade, use PEAR's Services_JSON. It works as of PHP 4.3 so you should be fine.
Maybe you should put somewhere in the root of your application a call to a checker function that does function_exists() for a couple of your needed function and if not stops the execution to prevent unwanted results.
I'm curious about how some built in functions are implemented,but it's very time consuming to look it up directly in the source,is there a tool that can automate this?
EDIT
Or is there a tool that can debug into the c code that's actually executed?
Most (all?) of the functions that can be accessed from PHP are defined under the ext/ directory in the PHP source code. If you have a recursive search tool, search for PHP_FUNCTION - if you saved the results of that search into a text file, it would be a pretty good "index" for figuring out where a PHP builtin is defined.
The really core stuff is in ext/standard.
Some rare "functions" are implemented directly as opcodes in the Zend virtual machine that PHP compiles to, so there isn't a well defined C function as such. I think strlen is such a function, for instance.
About the debugging the C code that's executed, I suppose it's possible to use something like dbg ; you'll first have to recompile PHP with the --enable-debug mode, though.
For more informations, you can take a look at :
Building PHP for extension development
Generating a gdb backtrace
I've never used this to debug PHP itself, but I've used those two pages to generate some backtraces of a crash I had with an extension, and it worked OK, from what I remember.
As a sidenote : using a PHP compiled with --enable-debug, you might have to recompile some of the extensions you're using and change the way they're loaded (it's the case for Xdebug, for instance) ; and some other might just not work at all anymore.
I believe that you should take a look at this.
Facebook has developed a tool to convert PHP code into c++.
So I guess it can handle C as well to some extent.
I am passing a json encoded string from javascript to a php file on the server via ajax. on my online server this works fine. but locally, it does not.
There are a few differences in the php installs in the 2 places. minor ones. both are php4. the install locally is actually a newer php4.
I'm trying to track down why this is happening. It looks like the json parsing on the server side with the pear servies_json (json.php), isn't parsing correctly. It manages the first brackets, but then it stops there, and all the internal data is lost?
Is there a php plug in or something that I need to install that keeps this from happening. A setting switch? Thanks for any help.
json_decode() should be used on the server-side to decode the JSON object, it should work fairly consistently. Its sister function json_encode() is equally good for encoding an array/object to a JSON string which can be echoed in your javascript.
Both functions require php 5, for php 4 you can check out this code.