How to encode an array in JSON without json_encode ()?

I am very new to PHP, but now I have to help my friend solve the problem with PHP.

He bought a PHP-based website, after he uploaded it to the host, he discovered that there was an error:

Fatal error: Call to undefined function json_encode() in xxx.php 

The code is very simple:

 echo json_encode(array( 'result' => true, )); 

It sends json to the client.

I know that json_encode added after php 5.2, but the PHP version of its host is PHP 5.1.2, so the reason it reports an error.

But we do not have the right to update the PHP version of the host, so I need to change the code. How to allow json return to client without json_encode ?

The web page in the client browser will run this javascript code to get and verify that json is:

 jQuery.ajax({ 'url': '...', ..., 'success':function(json) { if(json != null && json.result == true) { // do something } } } 

Thanks a lot!


UPDATE

Since I do not have the right to update anything or install new libraries on this host, the only thing I can do is change the code. But I know almost nothing about php, that I do not know how to use third-party libraries. Finally, I fix this as:

In php:

 echo '{"result":"true"}'; 

In javascript:

 if(json!=null && json.result=="true") { // do something } 
+4
source share
8 answers

First of all: you should strongly consider upgrading since PHP 5.1.x and 5.2.x are no longer supported. You indicated that you do not have rights to this, but if you are a paid customer, this should count on something. PHP 5.2 is deprecated from the latest version of PHP 5.3.6 .

A user at PHP.net has provided a custom function that does the same. You can rename it to json_decode() and wrap it in an if (!function_exists('json_encode')) :

 if (!function_exists('json_encode')) { function json_encode() { // do stuff } } 
+6
source

It has an older version of PHP where json_encode is missing. This can be fixed using various third-party libraries.

+3
source

Here's the implementation you can use , posted in the comments on PHP docs for json_encode .

+1
source

You can use a third-party JSON library, such as one from the PEAR project (they provide all kinds of useful PHP libraries of various quality):

http://pear.php.net/pepr/pepr-proposal-show.php?id=198

I am sure there is more (I would try a Google search for PHP JSON libraries).

+1
source

Why not use a regular PHP json library that you can include in every setting?

I googled this one here for you.

0
source

Read the comments for json_encode at http://www.php.net/manual/en/function.json-encode.php , where you will also find a comment containing a manual implementation for php <5.2: http: //www.php .net / manual / de / function.json-encode.php # 100835

In addition, there are other implementations available through google.

0
source

jsonwrapper implements the json_encode function if it is absent, and leaves it alone if it is already present. So it is well compatible with the future.

0
source

I had to install JSON Libs the other day on my server, try the following:

Run the following commands:

  • pecl install json
  • touch /etc/php.d/json.ini
  • echo "extension=json.so" > /etc/php.d/json.ini
  • service httpd restart

The first will install json libraries on your computer, the second line will create json.ini fiel in the php.d directory, and the last line will add the extension=json.so line to the extension=json.so file.

Hope this helps!

0
source

Source: https://habr.com/ru/post/1345218/


All Articles