I've been on working on this for ages. I am receiving a JSON from a request to an API and unfortunately the key's of each entry is a time uuid which is in binary format. I'm trying to use json_decode() to decode the json into a PHP array. However, when I echo the string I have about 80 entries but after the decode into the array, it trims it down to about 40 entries,
I should mention that I don't need these values that are in the keys, I could just strip them off if possible using array_values() maybe?
Any advice would help Thanks!
Here's some sample data
"\u001c":{"down":"1280069835000","off":"1279893600000","on":"1279886400000","up":"1280077035000"},"=":{"down":"1280163435000","off":"1279893600000","on":"1279886400000","up":"1280167035000"}
I actually contacted the development team, and they converted the binary uuid to string format, and it worked like a charm. Looks like this sort of thing may not be possible besides manually parsing out the JSON. Thanks for your efforts all!
Related
I have a program in labview which is sending UDP packets and using a php program I am receiving those packets. So labview program is sender and php program is receiver.
In labview program, the float array is type casted to string using type cast function block and sent as UDP packets. While receiving those packets using php, I am receiving some data which is not in readable format.
I have tried converting the string array into float array using array_map ('floatval', $array).. But still the values are not coming in the readable format.
Please help me to solve this issue.
The LabVIEW help for Type Cast points you at the document on flattened data which mentions that the representation is big-endian (most significant byte first). The entry on How LabVIEW Stores Data in Memory shows the actual representation of a single-precision floating-point number (SGL):
Now that you know what LabVIEW is sending, your question becomes how to decode this in PHP - if you can't solve this yourself, I suggest asking a new question.
If you can change the LabVIEW code, you could alter the format in which the data is sent so as to make it easier to decode at the other end. Possible options there might include:
If network bandwidth is not an issue, use a standard text-based format such as JSON
If JSON is too big but you can afford eight bytes per value, convert to DBL - using a conversion 'bullet' from the numeric palette - before flattening to string, then reorder the bytes of the string to little-endian at the LabVIEW end. From Ton Plomp's comment, that might be correct for your current PHP code.
If you really can't afford more than four bytes per value, but the range of your data values is not too wide, you could scale them to an integer value (U32 or I32) before flattening; again, that might be easier to decode at the other end.
Note that although the data format you get from Type Cast and/or Flatten to String is documented and historically has been stable, I don't think it's absolutely guaranteed not to change between LabVIEW versions.
Also the unreadable section of data could be header info added by the UDP function. You may be able to parse that data and discard.
Another thing to try is to read the UDP Rx data in Labview and compare to the Tx data to try and identify what is going on.
I'm currently working with data I'd like to temporarily store in my database as encrypted data. I'm not worried about the database getting hacked into, I just want to ensure the people that had entered the data that it is not reachable by any other than themselves. (and me of course)
The data is not meant to be stored permanently in the database since I'm exporting it to a third party application using their API, but since they have a rate limit I need to store the data in our database until the limit is over and I can upload it. (Assuming the rate limit occurs)
The process:
The request I receive from the form is in an array, so to begin with I serialize() the array to get a long string which I will unserialize() later.
Then I want to use a method that lets me convert the string into numbers and back again without losing information.
The reason I want to turn the data into numbers is because I use the HashIds library, which only encodes numbers. To my knowledge it's an extra layer of security I'm happy to add.
Read more on HashIds here: http://hashids.org/
What I have tried:
I tried converting the string into hex numbers, and then the hex numbers into decimals. Unfortunately the number was too large, and i haven't had any luck using biginteger with it.
base64_encode() which is not going to turn the data into numbers, but then base_converting them is. But I couldn't figure out the base converting in php since apparently it's rather odd.
Conclusion:
How can I convert the data I'm receiving from a form request into a short encoded string which can be converted back into the data without too much hassle? I don't quite know all the options PHP offers yet.
UPDATE:
To conclude this thread, I ended up using OpenSSL to encrypt my serialized array. Only problem I ran into was if the request contained a file I wouldn't be able to serialize it and save the object to the database. I do still need a way around this, since the third party application expects the file to be a multipart/formdata object i can't just save the filepath to the database and upload that. But I guess I will have to figure out that one later.
That link http://hashids.org/ provides a pretty clear example. Lets assume that your integer is 15.
$hashids = new Hashids\Hashids('some random string for a salt. Make sure you use the same salt if you want to be able to decode');
$encoded = $hashids->encode(15);
print_r(['hashedId' => $encoded]);
$decoded = $hashids->decode($hashed);
print_r(['decoded' => $decoded]);
So the value of $decoded should equal 15
Update
Sorry - the hashids bit of your question threw me and as such, I misunderstood what you were asking. I will update my answer:
You should really be using https://secure.php.net/openssl_encrypt and https://secure.php.net/manual/en/function.openssl-decrypt.php
I am using json_encode to transform my php multidimensional array to output json. Normally, this function would convert all values to strings. To make sure that integers values are send to javascript as integer values, I am using the numeric check:
$json = json_encode($data, JSON_NUMERIC_CHECK);
This works fine in all but one case for my app. In the php array (which is extracted from the database), there is one field that contains very large integers. I save it to database as a VARCHAR, but unfortunately this is converted to an integer when encoding to json. The problem is that since this is a very large integer, it gets rounded and therefore does not represent the true value. How could I tackle this problem?
Do you want the large number to be transformed to an integer? Your question leads me to believe you don't. If that's the case, remove the JSON_NUMERIC_CHECK option from the call and it shouldn't change the encoding of the field.
Documentation about this (and other) constants is here.
Maybe is to late but i did hit the same problem, and stuck on PHP 5.3 on the server because of legacy code that must be run with that version. The solution that i used is dumb, but did work for me: simple add an space character at the end of the long integer that is varchar readed from the db, and before sending it to json encode with JSON_NUMERIC_CHECK.
I am writing some php code which which will be following the oauth2.0 specification. One of the requirements they specify is that any request in which parameters are repeated results in an error. As such, I have a json which I am parsing using json_decode, and I am trying to figure out how to catch if the json repeats any parameters. The result from json_decode seems to just use the last value for the key in the case of repeated parameters, so it seems like I would need to detect them before decoding. Does anyone know how to do this without writing my own json parser?
Thanks!
The approach that I ended up using was to json_encode the json decoded version of the input string and compare it to the input string. If the two match, there were no repeats. If they don't the json_decode saw the repeat, and removed it automatically, and hence there were repeats.
I have a php script that outputs a json-encoded object with large numbers (greater than PHP_MAX_INT) so to store those numbers internally, I have to store them as strings. However, I need them to be shown as un-quoted numbers to the client.
I've thought of several solutions, many of which haven't worked. Most of the ideas revolve around writing my own JSON encoder, which I have done already, but don't want to take the time to change all the places I have json_encode to instead say my_json_encode.
Since I have no control over the server, I cannot turn remove the JSON library. I cannot undeclare json_encode, nor can I rename it. Is there any easy way to handle all this, or is the best option to just go through each and every file and rename all the method calls?
With javascript being loosely typed, why the need to control the type in the JSON data? What are you doing with this number in javascript, and would parseInt\parseFloat not be able to make the leap from string to number on the client side?
The only option I had was to use my own json_encode method renamed to my_json_encode, and then change everywhere that called that method.