Get PHP JSON Data in Android - php

I do alot of development in PHP. And I have recently started working with JSON data for various APIs for my websites to interact with each other. I would like to extend this a little further and create a Android app with my web applications.
Please keep in mind I am fairly new at Android and have little experience. I have worked with JSON data in JavaScript, and it is fairly easy. However that is using jquerys easy to use Ajax .post.
So my question is, what is the easiest, most simple way to get JSON data from a website and turn it into a working variable in Android?
I would prefer something with no error checking or anything. Just the basics.
Thanks!

If you're programming object oriented, there is not any easier way then to do it with JSON, so this is a good choice.
You can use Google's Gson to parse the Json strings your PHP back-end sends to your Android app.
As long as you use Objects, you can parse the string into an object with 1 line of code. Just be sure to type all fields correctly on your PHP side.

Take a look at this tutorial, I used this a while back and it makes it pretty clear :)
As #alexander7567 mentioned, GSON is also very good to use. But may be a bit more complex for you to pick up at first!

Related

AngularJS unserialize PHP string

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.

connect to php API from php website

I'm quite new to PHP. As a learning project, I'm currently building a website on which users can order products.
I don't want my website to connect to the database I have directly, but I want that process to go through an API I've made. The API has a quite simple structure: the index.php receives the call and the variables (through post) and then, depending on the type of data received, runs one of the functions in one of it's controllers.
So, the question:
How do I set up a connection between my website and my php api (on the same server) to access my database?
I have searched the web and SO for API connections but most of the questions are about the FB API and oAuth etcetera. If I have missed a similar question please inform me because then I'll delete this question.
Any help would be much appreciated, Thank you in advance!
Sounds like you want to implement a REST API. There are a boatload of tutorials and helpful links that you can find very easily to read up on this subject. (Here is a decent starting point). There are also many, many frameworks that you can use that handle RESTful interactions automatically.
EDIT:
Once you have a REST API setup, the best way to connect and interact with your API in PHP is using the cURL module. This is a good intro to the subject of using cURL in PHP.
The current preferred structure for passing data from API -> client is JSON. PHP makes it trivial to work with JSON. Within your API use json_encode to convert a PHP variable into it's JSON equivalent string. Inside your client, convert the JSON response from your API into a PHP object using the inverse function: json_decode
This is a very well known/widely used technique and there are many more nuances to consider, but this should be a sufficient intro for testing purposes. Once you understand the ideas I strongly recommend doing some google/stackoverflow searches and reading more on the subject.

iOS app getting data from server

I will be writing an IOS app that will query data from a server. What is the proper method to request and retrieve data in that manner. None of the books I'm reading seem to be covering it. I imagine the server would be using something like PHP and it would have access to the database. Would the iOS app do something like XML-RPC and i write php functions to listen, retrieve, and respond? Is XML-RPC even still the hip solution or is there something more modern I've not used yet?
The iOS app will be ultimately trying to get information from a Firebird database.
Well, if you're creating an iOS 5 app, then you're on luck, you can use restful webservices very easily:
NSString* response = [NSString stringWithContentsOfURL:<url pointing to your server> error:&error];
Then you can use NSJsonSerialization to convert the response into a property list. Also there are convenience methods for parsing XML files.
I would highly suggest looking into ASIHTTPRequest.
It is amazingly documented, Extremely powerful and easy to pick up.
Download the files drag and drop them into your project. Import ASIHTTPRequest.h.
Then you can do all of this
http://allseeing-i.com/ASIHTTPRequest/How-to-use
I figure I should put the disclaimer that the developer no longer supports the project, However, it works amazing and since it is so widely used i'm sure if there is ever a major issue with it in the future someone will take the liberty of fixing it and putting it up on github for all.
You can use various methods like this:
Simple Get or Post Request.
XML SOAP Services.
REST JSON Services.
This depends the data you want to retrieve.

PHP RESTful Web Service for an iPhone

I'm developing an iPhone APP and need to implement also an Web Service.
First of all I'm not a Developer and never made something big in PHP, Objective-C, xCode.
My PHP knowledge isn't also good. But let's start with my Environment.
iPhone APP (xCode 4.2, iOS5), PHP Web Service, MySQL DB
I was researching the WEB and most People tend more to REST than SOAP. I think i see also the advantages of REST (using of simple HTTP Verbs (get, post, delete etc...), but that's not the main point here...
I think I understand the main goal of the REST Architecture and tried to make a little concept with an URI and Verb Mapping. Here just a simple example of the mapping:
/location/{location_id}/product
/location/{location_id}/product/{product_id}
Both are GET operations who should get me ether a single product or all products of a location.
How would a simple PHP REST Web Server look like with these functions?
Another part should implement a User Authentication from the iPhone. Somehow i need to store the user session, right now I don't have any idea how to make that. The goald is that if only a user is logged in, he could review the product.
Now I've researched also the Web but couldn't find an easy step-by-step Tutorial.
Do you know any good Tutorials which will help me achieve my goal? :)
A lot of people prefer using PHP Frameworks like ZEND. This seems very interesting, but it seems like a big package with a lot of modules.
Does someone know exactly which Modules are needed to get my Web Service working?
This is quite a good tutorial, it uses the codeigniter framework which makes the learning curve a bit steeper but makes it a lot more powerful in the long run.
http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2/
If you want to build this the custom way it's actually very easy to do if you want to return information in the JSON format especially for php5 which is generally well supported amongst hosts these days.
Essentially the steps are like this:
Pass in product id via url and retrieve using GET i.e. service.php?product_id=10
Query database and return data for product id that was passed in
Store returned data in an array
Set header content-type to application/json
json_encode the result (json_encode)
That way when you call that url in a browser you will get a nice JSON formatted array result in a key:value pair manner. And as of iOS5 json parser comes with the framework (for earlier versions SBJson is a good framework to use (SB JSON))

passing variables between flex and php

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

Categories