I have a PHP project which provides a api endpoint exposing JSON data. Part of that data are serialized strings generated in PHP.
In a nodejs application I can unserialize that data using the php-unserialize package. However, I am a complete beginner at Angular and can't find a similar package for AngularJS.
I would appreciate some direction on how to unserialize a string into JSON data in AngularJS
and, where I can find AngularJS packages?
The AngularJS framework does not have a service that decodes array and objects serialized by PHP serialize.
Instead encode using PHP json_encode.
I seriously recommend that PHP APIs avoid using serialize to communicate data.
From Anonymous:1
Please! please! please! DO NOT serialize data and place it into your database. Serialize can be used that way, but that's missing the point of a relational database and the datatypes inherent in your database engine. Doing this makes data in your database non-portable, difficult to read, and can complicate queries. If you want your application to be portable to other languages, like let's say you find that you want to use Java for some portion of your app that it makes sense to use Java in, serialization will become a pain in the buttocks. You should always be able to query and modify data in the database without using a third party intermediary tool to manipulate data to be inserted.
I've encountered this too many times in my career, it makes for difficult to maintain code, code with portability issues, and data that is it more difficult to migrate to other RDMS systems, new schema, etc. It also has the added disadvantage of making it messy to search your database based on one of the fields that you've serialized.
That's not to say serialize() is useless. It's not... A good place to use it may be a cache file that contains the result of a data intensive operation, for instance. There are tons of others... Just don't abuse serialize because the next guy who comes along will have a maintenance or migration nightmare.
The JSON Standard is well supported by most languages and widely used for data interchange on the Web. The AngularJS $http service supports it automatically by default.
Related
Just came across serialize() in php. Many databases store the data in serialized form so curious to know what is more usefun json or serialize and why?
Json Data->["Math","Language","Science","History"]
serialized Data->a:4:{i:0;s:4:"Math";i:1;s:8:"Language";i:2;s:7:"Science";i:3;s:7:"History";}
Serialize() is a native PHP method of storing data in text strings. It can even store objects. And it has a special notation for objects. JSON, it its turn, is a JavaScript's Object Notation. Basically, same thing. But they are used in different domains.
JSON is usually used for online communication - sending data to/from AJAX scripts, numerous API's, etc. It appeared to be much lighter and readable than XML, thus it competes with it for the most commonly used data transfer standard for APIs. It is also widely used in config files, say, for Composter - where readability is one of the top requirements.
Visual readability and easy structure makes JSON the best way to communicate between different platforms and programming languages. This, last phrase is important.
PHP Serializing is used in cases where you save data in PHP and you restore data back in PHP as well. Say, you can save arrays, or $user object into a PHP session, and then restore them back. Or store a multi-dimensional array in a MySQL field, and then restore it exactly as it was in an $array variable. In most cases Serialized data is not much shorter/lighter than JSON and doesn't usually take less space - it makes it lose readability though. But there are quite powerful tricks with it PHP has - it preserves data types, classes names, visibility options, etc. Also refer to __sleep() and __wakeup() magic class methods allowing you to save objects on the fly and create special constructors to revive them back, again, on the fly.
In your particular case, if you store data in MySQL database with PHP, and then read it back to PHP - then PHP serialization would be the best way to use. Unless you want to access this data with other languages, or edit manually/visually.
to complete Oleg's answer I would like to suggest you to visit this link with objetive comparisons between json_encode and serialize PHP methods:
http://www.shozab.com/php-serialization-vs-json-encoding-for-an-array
It shows how serialize is usually slower but not recommended for all situations.
And finally here you have another discussion with many contributions in stackoverflow:
Serialize or json in PHP?
Hope it will be interesting for you.
I'm starting a new project that uses couchbase (a noSQL database that stores objects in json format), together with php.
The thing is that it would be really easy to work with them both if I could have something that maps json into one of my own php classes (and vice versa).
Do you know any library for that?
One way to start is to look (or use) the "Basement" Library that is available here:
https://github.com/Basement/Basement
This library uses json_decode/encode.
Hope that will help you.
You may use our JSONmapper to map from JSON to your PHP classes.
It unfortunately does not support mapping back (yet).
Tug already mentioned Basement, which will provide that functionality of "models" in the near future like you know it from ORM systems.
Aside from that, mapping your plain old php objects to JSON is very easy, thanks to the nature of json_encode/decode. Since you can pass it an arbitary object and it will store it as JSON, thats basically the only thing you need at hand. If you need more infos about JSON and PHP, my blog post is a good start: http://nitschinger.at/Handling-JSON-like-a-boss-in-PHP
If you use Basement, it makes it a little bit easier for you since it allows you to transform PHP types into JSON behind the scenes automatically (or write your own mapper if needed).
If you have a specific example that you want to build, let me know and I'd be happy to provide an example!
I was wondering what would be the best way to store JSON/XML responses. I'm currently building an application that supports heavily on the SoundCloud API to fetch songs/playlists etc.
Here are some ideas I've come up with.
Storing the results in a Relational Database and then using PHP to convert them to classes to make easy use of them throughout my application.
Doing the above, only this time using my framework's built-in ORM.
Using a Document-Oriented Database. (ie. MongoDB, couchDB, ...)
Storing the JSON responses in a cache. (using my framework's cache classes)
Can anyone care to shed some light on some of the advantages/disadvantages of using any of these methods?
Which one do you prefer?
If you have a solid schema, that you wont think it will change, you might want to use relational database. You will need to parse the json and make objects out of the JSON response and using your framework you can persist it to database.
If you think your schema will change use NoSQL.
It also depends what will you do with this data. Are you going to search the nodes within JSON?
You can also do a object to mongo mapping, you can either parse the JSON and store it as an object or you can store the JSON the way it is.
Nice thing about NOSQL is that they support JSON pretty well in which they use BSON (Binary JSON).
In terms of cache, IME, it should be used only for lookups, and actually, you cant search the cache. It s just for getting objects faster than going to database and getting it.
Take a look at this:
http://www.mongodb.org/display/DOCS/Inserting#Inserting-JSON
If you can tolerate hosting your music and playlist data on Google's AppEngine, Ubud-db can be something for you: https://bitbucket.org/f94os/ubud-db/wiki
Ubud-db is a document store on AppEngine with a REST-JSON API. Spring/Jackson maps from JSON to a Map, and then Ubud's service maps from the Map to Entity, persisted by the Datastore.
The REST-JSON API makes it easy to integrate with a website using AJAX to access and display dynamic data.
If I need keep the data for longer than a cache provider, I would store them in a database as-is and then just json_decode them when I retrieve them from the DB. If it's just temporary storage, cache is a great idea, still leaving it encoded as json to reduce the size.
I'm writing a web application in Symfony for the first time, so I have a question regarding the use of Doctrine vs. Stored Procedures.
My background is in Java server-side, so I wanted to know what the pros and cons are for using stored procedures, versus using simple Doctrine code to get things done. At the most basic level, let's say that my Symfony web application is used for management, while there's another engine (which might or might not be written as a Symfony component, or indeed not in PHP at all) which retrieves configurations from the database for distribution or whatnot. Here I can see where stored procedures might be handy: both code bases use them to query and access data, while neither are preoccupied with the actual schema. If there's a change to the schema (adding a column in some table, for instance), then I'd only have to change the SRPOC, and nothing else, whereas if I had been using code in both engines to access data, I'd have to change them both to match the new schema.
Any comments? Or did I take it too far?
Thanks!
-DBG
If you don't use doctrine, you loose database abstraction, and object mapping. And this is a major loss. If you need third party apps integrations, you shouldn't let them interact with your database. Rather, provide a web service for them to read/write data via JSON, for example. This way you can change your database schema and have control over third-party apps.
i am new to flex development my question is How do i pass variables between flex 3 and a mysql database using php? i was told the best way is to AMFPHP but that seems like an over kill or may be not am not sure.. any ideas?
I'd use json over xml since json will transfer less bytes and php's json_encode($object) is quick and easy.
I'd use json over amfphp because json is general purpose. For example, I can create a web service for flex or javascript by returning json.
You might have a look at this article for comparison between json, amfphp, and xml. Also, here is a nice tutorial on flex, php, and json.
AMFPHP is not really being actively developed. The best alternative right now is to use Zend_Amf http://wadearnold.com/blog/?page_id=155, which is supported by both Zend and Adobe.
Don't be scared by the need for Zend Framework components. The framework is modular, and you can use your own custom php classes for accessing data without having to incur the Zend Framework learning curve.
The great thing about using AMF is that since it is a binary data transfer, it's very fast.
Also, working with XML or even JSON, is an annoying extra step if you just want your flex app to get results data an api call. If, for some reason, you need to also handle outputting data to xml or json, that can easily be added to your app by extending or creating new controllers/services that translate the data from arrays and objects to xml or json
There's a few choices open to you. Essentially, it boils down to how you'd like to deal with the data on the PHP side of the fence.
The two I'd spend time investigating would be simple XML (my first preference) and AMFPHP.
XML:
Flex can work very easily with XML data, even mapping it automatically to/from ActionScript objects (generically, or with something like the xobj project on GoogleCode, to typed instances). Similarly, there's plenty of support available for working with XML in PHP code.
AMFPHP:
AMFPHP gives you way to pass typed ActionScript objects over the wire to your PHP code. There's tooling included in the AMFPHP project that makes working with MySQL on the PHP side easy too.
If you are doing a simple query, I would pass your data as plain POST data. You can do this using HTTPService component in Flex. On the PHP side I would respond with an XML string and set the HTTPService resultFormat to "e4x" (Ecmascript for XML), for an easy object-like manipulation of the result data.
Here is the livedocs reference for HTTPService: http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_2.html
For more complicated queries/operations, there is also the option of using the Flex RemoteObject with AMF as the data protocol. On the PHP side you can then use the zend framework AMF component to communicate.
Here is the livedocs reference for RemoteObject: http://livedocs.adobe.com/flex/3/html/help.html?content=data_access_4.html
And the download page for Zend AMF: http://framework.zend.com/download/amf