Odd behavior of json_decode() - php

Take the following JSON string (generated by some ExtJS code - but that's irrelevant):
[{"action":"Setting","method":"toggle","data":["welcome-home"],"type":"rpc","tid":2},{"action":"ContentExtFeFillout","method":"todo","data":[true,0,8,false],"type":"rpc","tid":3}]
being sent to a server as a POST request and retrieved via $GLOBALS['HTTP_RAW_POST_DATA'].
Running
json_decode($GLOBALS['HTTP_RAW_POST_DATA']);
on our development machine (5.2.10-2ubuntu6.4 with Suhosin Patch 0.9.7) gives a correct print_r() output of:
Array
(
[0] => stdClass Object
(
[action] => Setting
[method] => toggle
[data] => Array
(
[0] => welcome-home
)
[type] => rpc
[tid] => 2
)
[1] => stdClass Object
(
[action] => ContentExtFeFillout
[method] => todo
[data] => Array
(
[0] => 1
[1] => 0
[2] => 8
[3] =>
)
[type] => rpc
[tid] => 3
)
)
Running the same code on a client's production machine (5.2.5 with Suhosin Patch 0.9.6.2 and Zend Optimizer; SUSE Linux by the way) gives the following print_r() output:
Array
(
[0] => stdClass Object
(
[action] => Setting
[method] => toggle
[data] => Array
(
[0] => welcome-home
)
[type] => rpc
)
[1] => 2
[2] => stdClass Object
(
[action] => ContentExtFeFillout
[method] => todo
[data] => Array
(
[0] => 1
[1] => 0
[2] => 8
[3] =>
)
[type] => rpc
)
[3] => 3
)
Note the missing tid property which obviously has been moved into the main array as an own value - this naturally breaks all the following code.
We also downloaded a Windows PHP version 5.2.5 to check if there's a bug in json_decode() but we get the correct output here.
Are there any known issues with json_decode() at all that could cause this odd behavior?
We're currently totally clueless...
Thanks to all of you!
Best regards
Stefan

OK guys - problem solved. Lacking any more options we persuaded the client to update the installed PHP version and guess what: it works.
There seems to have been a subtle bug in their PHP installation (PHP, Zend Optimizer and/or Suhosin) which has been fixed with the update. Still, a quite weird thing.
Thanks to all of you!
Best regards
Stefan

Have you tried swapping the tid and type keys? Also how about using a hardcoded variable to check if the problem could be with $GLOBALS['HTTP_RAW_POST_DATA']?
Try the following:
$t1='[{"action":"Setting","method":"toggle","data":["welcome-home"],"type":"rpc","tid":2},{"action":"ContentExtFeFillout","method":"todo","data":[true,0,8,false],"tid":2,"type":"rpc"}]';
print_r(json_decode($t1));

Related

json decode transforms Facebook payload

Facebook POST request from lead ads test tool is getting transformed on my end:
Facebook lead ad testing tool:
https://developers.facebook.com/tools/lead-ads-testing
Code to accept payload:
$input = json_decode(file_get_contents('php://input'), true);
Framework: CakePHP 2.0
Debug:
2017-07-07 12:12:27 Debug: Array
(
[entry] => Array
(
[0] => Array
(
[changes] => Array
(
[0] => Array
(
[field] => leadgen
[value] => Array
(
[ad_id] => 0
[form_id] => 7.2426956441826E+14
[leadgen_id] => 7.9242177426971E+14
[created_time] => 1499447542
[page_id] => 4.8242822517084E+14
[adgroup_id] => 0
)
)
)
[id] => 482428225170841
[time] => 1499447543
)
)
[object] => page
)
PHP versions: 5.5.9
The problem:
[form_id] => 7.2426956441826E+14
[leadgen_id] => 7.9242177426971E+14
When it should be:
[form_id] => 724269564418262
[leadgen_id] => 792421774269707
So it seems to be converting those numbers for some reason.
I have a clone of this app on an older version of PHP ( 5.4 ) and dont seems to be having this issue there.
I suspect you may need to set the 'options' of json_decode to use JSON_BIGINT_AS_STRING.
Check out http://php.net/manual/en/function.json-decode.php
And this post led me to the possible solution PHP JSON large integers

Getting sugarcrm array output via REST api sorted with PHP correctly

First, I'm using sugarcrm pro 6.5 and accessing via rest v4, so I have this array that's being returned from printing $results that is working fine:
stdClass Object
(
[result_count] => 2000
[total_count] => 3390
[next_offset] => 2000
[entry_list] => Array
(
[0] => stdClass Object
(
[id] => 77da301b-83dd-4fe6-e38f-53ba151fb084
[module_name] => Leads
[name_value_list] => stdClass Object
(
[id] => stdClass Object
(
[name] => id
[value] => 77da301b-83dd-4fe6-e38f-53ba151fb084
)
[name] => stdClass Object
(
[name] => name
[value] => Jim Beam
)
[status] => stdClass Object
(
[name] => status
[value] => Dead
)
[website] => stdClass Object
(
[name] => website
[value] => website.com
)
[phone_cr] => stdClass Object
(
[name] => phone_cr
[value] => 1-888-888-8888
)
)
)
[1] => stdClass Object
(
[id] => d0ecc069-d556-98f3-41f2-53ba1468327a
[module_name] => Leads
[name_value_list] => stdClass Object
(
[id] => stdClass Object
(
[name] => id
[value] => d0ecc069-d556-98f3-41f2-53ba1468327a
)
[name] => stdClass Object
(
[name] => name
[value] => John Doe
)
[status] => stdClass Object
(
[name] => status
[value] => New
)
[website] => stdClass Object
(
[name] => website
[value] => web.com
)
[phone_cr] => stdClass Object
(
[name] => phone_cr
[value] => 1-888-888-8888
)
)
)
I'm using a query from the api to filter the results for the user I'm targeting:
'query' => "leads.assigned_user_id='user_ID-here'",
'order_by' => "date_entered DESC",
This works fine. So I've ran a foreach () statement to retrieve only one field on a button click, which also works just fine. What I really need to accomplish is before this statement, a foreach() command (or something else?) to filter out and retrieve ONLY the "New" results in the status value, and from that group output an array showing only the website field. Seen in the "desired end result section of this question."
This is the code I'm filtering the field I'm targeting and having a new array created with if that helps bridge the gap:
$results = call('get_entry_list', $params, $url);
$eresult = array();
foreach ($results->entry_list as $index=>$value_list) {
$listed = $value_list->name_value_list->website->value;
$eresult[] = $listed;}
So the desired end result based on this data should be:
Array
(
[1] => web.com
)
I'm unsure what I need to do to filter the "Status" field to only then be ran with the $eresult array I created to achieve this. To be clear, everything is working as it should, and my print from $eresult is outputting exactly as it should by returning all results in the website value area, I just need some help to get it sorted before going to that step by sorting it by the "new" status first without all the extra 'stuff,' then sorting out the array in my desired format with the foreach() statement above. I tried to cut out all the other code, as it's a pretty long project, so this should be all the relevant information for the particular goal I need to accomplish in this segment. Any help is greatly appreciated! Thank you!
I've decided to create a second script for this as a temp solution by adding:
'query' => "(leads.assigned_user_id='user_ID-here') AND (status='New')"
So I guess that works, I was trying to avoid calling another script for just one separate function, but it is working fine.

json_encode does not work with strings as indexes

When I have an array like this :
Array (
[0] => Array ( [0] => 1 [1] => 12 [2] => Essener [3] => 1 )
[1] => Array ( [0] => 2 [1] => 12 [2] => Dinkel Spezial [3] => 0.2 )
[2] => Array ( [0] => 1 [1] => 1 [2] => Essener [3] => 1 )
)
and I use json_encode and echo it, I get this:
[["1","12","Essener","1"],["2","12","Dinkel Spezial","0.2"],["1","1","Essener","1"]]
which is good for me.
Now I have an array with stdClass Objects, which I wasn't able to transform into JSON with json_encode. When I echo it, it just doesn't show anything.
Then I transformed this array with objects to an array like this (with get_object_vars()):
Array (
[0] => Array (
[item_id] => 1
[item_name] => Essener
)
[1] => Array (
[item_id] => 2
[item_name] => Dinkel Spezial
)
[2] => Array (
[item_id] => 3
[item_name] => Saatenbrot
)
)
and when I use json_encode and echo it still doesn't show anything. Can anyone tell me what I am doing wrong or what I need to do to get a JSON array?
I need this json array to send data to an IOS App.
From the documentation:
Note:
In the event of a failure to encode, json_last_error() can be used to determine the exact nature of the error.
So you can try to detect the exact error by yourself. From your information I can't see any error.
Furthermore I don't think, it doesn't return anything. Try to var_dump() the result of json_encode(). I assume it returns false, which means an error occured.
So if anybody wondered what was wrong,
the problem was that i had "ä,ü,ö,ß" in my array and i needed to convert these to unicode and then everything worked just fine.

Error when using custom query in cakephp?

I have using cakephp for my website. And i use sql server 2012 with it.
I have confused that when i use:
$this->set('types',$this->Manager->query('select * from product_types'));
to get the array of all my product types
the return array is:
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[name] => hoa my pham
)
)
[1] => Array
(
[0] => Array
(
[id] => 2
[name] => hoa my
)
)
)
Why has the [0] instead of [product_types]????
Please follow the documentation and tutorials available.
Then you will be able to just use
$this->set('managers', $this->Manager->find('all'));
for the very same thing - using a clean approach with wrapper functions and a sql server datasource.
For SqlServer there should be a datasource available, for example:
https://github.com/cakephp/datasources/blob/2.0/Model/Datasource/Database/Sqlsrv.php
This is from the CakePHP documentation:
To use the model name as the array key, and get a result consistent with that returned by the Find methods, the query can be rewritten:
$this->Picture->query("SELECT * FROM pictures AS Picture LIMIT 2;");
which returns:
Array
(
[0] => Array
(
[Picture] => Array
(
[id] => 1304
[user_id] => 759
)
)
[1] => Array
(
[Picture] => Array
(
[id] => 1305
[user_id] => 759
)
)
)
So you need:
$this->set('types',$this->Manager->query('select * from product_types as Product_Types'));
Source: http://book.cakephp.org/2.0/en/models/retrieving-your-data.html
P.S.:
This syntax and the corresponding array structure is valid for MySQL
only. Cake does not provide any data abstraction when running queries
manually, so exact results will vary between databases.

PHP json_encode unknown error

I am returning results from a MySql database in PHP that I would like to use the json_encode function to turn into a string to pass back for another page's usage to display options within a SELECT element. However, the json_encode method fails and my page is not rendering properly. By that I mean I receive a generic IIS error from the cgi-bin process. I have error capturing setup within my script but the error generated is not boiling up far enough to be shown and I am not sure why.
The basis of my question is how can I make json_encode like the information below or do I need to roll my own encoder to deal with it?
Array
(
[0] => stdClass Object
(
[productID] => 2
[optionID] => 1
)
[1] => stdClass Object
(
[productID] => 2
[optionID] => 2
)
[2] => stdClass Object
(
[productID] => 2
[optionID] => 3
)
[3] => stdClass Object
(
[productID] => 2
[optionID] => 4
)
)
Spell json_encode correctly and it works MUCH better... dummy me.

Categories