iOS app getting data from server - php

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.

Related

Webservice connecting do database with IOS, PHP, REST API, JSON

i have tried to understand how to connect to a database with an IOS application.
i built an application that connects to a database on my localhost. i wrote a web service in PHP that returns a JSON encoded response, i then wrote the rest of the code in obj C with NSURL etc.
that is app-->web service-->database, right?
but im trying to understand things i read on the internet, like someone mentioning using the REST API, i dont understand what to use REST for, and what REST is really (noob i know).
i understand that REST gives JSON encoded responses, but is it something you use to create a web service ?
so is their someone who can explain to me what REST, web services in PHP, JAVA and JSON etc is in relation to mobile development ? please provide examples, thats the way i understand best.
Let me tell you, for a newbie you are on the right path and have the done it right! Once you get a hang of the basic json request / response webservices, you will yearn for more. Like security, uniform interface etc.
REST is nothing different from what you did. REST is an acronym for REpresentational State Transfer. The key part is statelessness.
REST uses HTTP verbs POST,GET, PUT/PATCH & DELETE to do the basic CRUD (Create, Read, Update and Delete).
Two additional verbs are OPTIONS (to retrieve the list of actions that can be performed) and HEAD (to return headers)
Read this to get a fair idea:
http://restcookbook.com/
http://www.drdobbs.com/web-development/restful-web-services-a-tutorial/240169069

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.

Get PHP JSON Data in Android

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!

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))

Should I always use webservice?

HI all,
I am designing an client - server application system. The Server provide 2 features: upload and download images. The clients (maybe Iphone, Android, WP7, ...) use these features to provide to end-users.
At this time, I can design the server as web-pages (such as upload.php, download.php) as well as webservices. Both ways work fine for me.
My question is: which one I use webservices or web-pages technical for my designing? And why?
Thanks.
To be honest with you, using something like SOAP for this seems like a lot of unnecessary effort. You can very easily accomplish this using form encoded data and the right query string parameters without going through the trouble of building SOAP envelopes and writing all of the XML. Granted, the system I described is very close to a REST webservice, so I suppose it depends on what you mean by "webservice." Either way though, given relatively simple system you've outlined, I don't think something like SOAP will give you a very good return on your investment.

Categories