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
Related
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.
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.
This may be a inappropriate question for SO, but I thought lets see :)
I'm writing a website in php. Every pageload may have 10-20 DB requests.
Using the result of the DB queries I need to generate a page.
The page would contain a topic (should be image or text) followed by comments. There could be mutiple topics like this.
Currently, I'm creating a string using the DB result and sending it to the browser.
When browser receives the string (as an ajax response), it parses using split functions and creates the HTML dynamically.
I'm basically a C++ programmer; relatively new to web development. So, I do not have fair understanding of the JS objects. How long of a string can JS variable hold? Is it ok to use split and generate HTML at the client.
I'm not generating the complete HTML at the server side to avoid any overhead because of string concatenation. I believe sending less no. of characters to the client (like I'm doing) is better as compared to sending complete HTML code.
Is something (or everything) wrong in my understanding :)
Any help is appreciated.
EDIT:
Well, I'll be highly grateful if I could get opinions in yes/no. What would you recommend. Sending HTML to the client or a string that will be used at the client to generate HTML?
Unless you have a specific reason for doing so, I think you should look into generating the HTML with PHP and sending it directly to the browser. PHP was built specifically for this purpose.
I think you be best off to look at jQuery and more specific to the AJAX method of that library. Also, take a look at JSON and you should be all good to go.
Have you considered using a templating engine like Smarty?
It's pretty simple to use, take a look at the crash course, you might like it! http://www.smarty.net/crash_course
I am writing my own drupal website side API.
I see that some tables containg some odd strings that i need to pharse, I cant fine any logic in it.
for example, I have upload via the admin a file and i have found the relevant row with the data.
a:12:{s:3:"alt";s:10:"aaaaaaaaaa";s:3:"fid";s:2:"22";s:5:"width";i:1024;s:6:"height";i:680;s:8:"duration";i:0;s:12:"audio_format";s:0:"";s:17:"audio_sample_rate";i:0;s:18:"audio_channel_mode";s:0:"";s:13:"audio_bitrate";i:0;s:18:"audio_bitrate_mode";s:0:"";s:4:"tags";a:0:{}s:5:"title";s:0:"";}
I need to pharse out the aaaaaaaaaa string which is the image "alt".
Do you have an idea of how to parse this string, I hope that your rule will be soutable for drupa odd strings in general?
Thanks
These strings are not Drupal-specific. They are serialized PHP data types (objects, arrays, etc.). See the PHP manual for serialize().
The string you've quoted is a PHP serialized array. See the PHP manual page for the serialize() and unserialize() functions for more info.
However I strongly doubt that you actually need to be delving into the data in this way. That data will have been set up in Drupal, and can be modified in Drupal. I can't say where, since the data you've provided is lacking context, but it'll likely be somewhere in your administration area.
I'd suggest you find where it's configured and change it there. Hacking around directly with the serialized strings that Drupal creates is a sure-fire way to damage your Drupal installation if you don't know what you're doing.
That string is serialised. You can unserialize it using http://php.net/manual/en/function.unserialize.php
how can I pass a string from PHP to Flash?
I need to pass this to flash,
$var = 'uid_'.$uid.'_'.'likes_'.$likes;
Any ideas on how I can accomplish this?
Thanx in advance!
So, your best bet in this case is going to be one of two things:
1) If this is a simple easy solution with a little bit of data that isn't going to see a whole ton of traffic, just have your PHP output an XML file and use Flash's URLLoader() to load in that .xml data - then parse it.
Alternately,
2) If this is going to see some heavy traffic, or if you want to do it the "right" way, look into either ZendAMF or AMFPHP. Lee Brimelow has tutorials for working with this stuff at gotoandlearn.com - basically, you can remote into a PHP Web Service which will return data (not just strings - you can even do typed objects!) as binary data directly into your Flash file.
Either way you're not going to have too much trouble with it - it's a pretty straightforward operation. Let me know if you have any questions.
The simplest way to achieve is to use the well-named flashvars to pass it.
Adobe as a KB about flash HTML parameters http://kb2.adobe.com/cps/127/tn_12701.html
Javascript based flash integration libraries (like swfObject) allow to pass any of these parameters as well.