I'm sorry if this seems like a newbie question but I am a newbie - so please be explicit in answers. I also hope that the terminology that I’m using is correct and doesn’t confuse the issue.
I need to parse a json file but I have no control over the content / structure and when the content changes the first I’ll know about it will probably be when I try to read it. My approach will be to work through the data and validate that I recognise each data item and that it’s context is as I expect, so that it’s handled correctly as I insert it into the database.
It seems to me (in my blissful ignorance) that there’s little difference in walking through an array produced by e.g. by PHP json_decode () or walking through the json, with my own specialised parser that will validate the json data as I parse it.
So my question is am I missing something here e.g. a big gotcha that's going to make parsing the json more complex than I expect or maybe I’m missing the point in some other way?
I recommend using json_decode. It will handle turning valid JSON into a PHP data structure. Then it should be much more natural to work with. Not to mention there are probably a lot of things you could get wrong in the parser that you won't have to worry about because json_decode will handle it.
Once you've got a PHP data structure, then work with that. Do your validation etc. there.
I'm a bit confused as to what your question is. I would recommend sticking with json_decode() and not to write your own parser as your's would probably end up simulating json_decode() anyways.
You can validate your json values as you move down each propery however you like.
Related
I am pretty new to coding and am still learning the ropes. I got an XML feed that I'm processing where, amongst other common data that is easy to process, I can get complex elements such as :
<para>Text before the Link more text after the link.</para>.
I get the data out of XML by simplexml_load_string($xml), perhapes this is where I'm going wrong. What is the best way to process an XML file where there may or may not be 'inline child' elements so that the output looks as it should. I can get the data out but what I end up with is two entries, "Text before the Link more text after the link." and the link itself.
This seems simple and I'm sure others may have run into this but I'm not sure how to preserve the original markup. I have been trying to find an answer to this with the good old google but I must be not using the right terms. Any help or pointers to a resource would be much appreciated. I have gone through a few tutorials online, but I'm unsure what I'm doing wrong.
Thanks.
It seems that SimpleXml can work on simple xmls ;)
Here may be solution for you Use DOMDocument
I just inherited a codeigniter application and looking trough the source code all the controllers have scrambled code. Example:
<?php $_X='-8588104972344438462';vMASFkAm('MTExMTAxMDAwMTAxMDEwMDAwMDAwEMMTA=');*
Is there any way to get back the original code?
Is this something codeigniter specific?
I don't have much experience with codeigniter. Please help.
If you have time and will, you can debug all that. See what each function outputs while making sure you replace any evals with echos, just to be on the safe side. The string parameter for that function looks like base64 encoded string, so you might want to decode that as well (or track where it's decoded in the code, and see the output). If it's not base64, it uses some more sophisticated algorithm where x and w seem like separators...
Unfortunately, unless you do it manually, variable by variable, function by function, etc... there's no way to get it back to the original state.
And, like the other answer said, this is in no way CodeIgniter-specific.
This is not CodeIgniter related. Looks like obfuscated code -- likely due to an exploit of some kind. I'd be concerned this server has been compromised.
https://www.google.com/search?q=is+my+server+compromised
To search a little deeper, I'd start by grepping through the codebase looking for
function vMASFkAm(
to see what's there.
I have what I think is a ByteArray. When using urlencode on the string, I get this data...
%0A%82%03%01%11SaleDate%0DToYear%0DYardId%15BuyNowOnly%0BState%0BModel%0FStockId%11FromYear%11FullText%13Inventory%11Category%09Make%0FOrderBy%1DOrderDirection%0FPageNum%11PageSize%01%01%06%05-1%02%06%01%01%01%01%06%0F1024187%06%09Sale%01%01%06%13RunNumber%06%07Asc%04%01%04d
I am looking at the php function unpack, and trying unpack('c4char/Xstr/...') where X is a, A, h, H to try to pull the "SaleDate" string out, but I can't get it to work.
Probably the completely wrong approach. Do I need to write a custom unpacker of some sort?
I'm not even sure what I'm dealing with here, it's the output from a flash file, so I think it's an Amf "ByteArray" but I'm at a loss as to how to parse it.
Looking for even just a direction to start hunting for how to manipulate/parse/rebuild something like this. Ultimately I'd love to be able to run it through a function, get an associative array, change a few values, repack it, and send it on it's merry way.
I think it's AMF3 and this is the data in the messages part.
I guess if there's one "main" question, how do I manipulate that string of data simply and safely?
Thanks for any leads.
There are a lot of AMF deserializers out there that are open source, for simplicity sake, would probably make a lot of sense to not roll your own here, even though AMF3 is fairly simple.
I came across this link on github which looks promising:
https://github.com/silexlabs/amfphp-2.0
You probably don't want to roll the whole framework, but can probably roll this:
/core/amf/Deserializer.php
/core/amf/Serializer.php
As far as I can tell, this appears to be a proprietary encoding method specific to the vendor. They use a "Byte Array" to allow greater flexibility of the payload. There isn't an easy way to work with this, without completely reverse engineering their encoding and decoding algorithms.
I am wanting to call a RAILS script within my php code base to avoid having to duplicate complicated business logic. I see that in PHP I can use passthru() but I'm not sure that is the best bet when returning more than a string back to the PHP function. I need to return back a hash/object of key/value pairs.
I'll be using script/runner on the RAILS side so that I don't have to change anything in the RAILS code. I was hoping there would be a simpler way to do this but I'm finding very little documentation on the webs that relates to this. Thanks in advanced.
Peace,
James
You can make your Rails script output a CSV data and then use PHP's fgetcsv to parse it into array. Of course, you'll have to modify your Rails code to output CSV, but it doesn't sound like a big change - just a couple of lines to convert your data into CSV. FasterCSV gem could help with that.
I need to send full objects from Javascript to PHP. It seemed pretty obvious to do JSON.stringify() and then json_decode() on the PHP end, but will this allow for strings with ":" and ","? Do I need to run an escape() function on big user input strings that may cause an issue? What would that escape function be? I don't think escape works for my purposes.
Are there any downsides to JSON.stringify() I need to know about?
Thanks
Yes, it's reliable in any decent implementation (like Crockford's), and no, you don't have to run it through escape first (if you do that, PHP will be pretty confused at the other end). Browsers are starting to get their own implementations of JSON stuff (now that it's in the 5th edition spec), but for now, you may be best off using Crockford's or similar.
There is a pretty good description of what JSON.stringify() does here:
http://www.json.org/js.html
The source code is also available if you want to be certain and/or make changes.
I have been using it for months without problem.
Also,
I'm not sure if you have seen the man page for json_decode, lots of good info there aswell:
http://ie2.php.net/manual/en/function.json-decode.php
HTH