Adding REST API to existing PHP website - php

I'm using some open source code to launch a game. The game is old, around 11yrs. It is a tick based web based game. It's written in php and using MySQL. Basically I want to port this game to ios, instead of using uiwebview I want to build native controls.
I know that it's bad practice to communicate directly with MySQL from an ios app. So what I want to do is add a RESTful API. I'm new to this and have scoured google for answers, does anyone have any pointers of where to start when adding an API to an existing site? It's quite a complex structure.
Thanks.
Paul.

My suggestion is using a framework that is guided towards building an API. I use Slim Framework for my API back end and it's pretty easy to use. If you create a separate URL for this API or include directly into the PHP project as a class it could work.
Some tutorials to get you started are
Say Hello World with Slim
RESTful services with jQuery, PHP and the Slim Framework
Writing a RESTful Web Service with Slim
Hope this helps,
Wes

Related

Creating a multi user Chat application in core php

Hi Im building a User relationship management application in php, here in this application we would require a multi user chat application in php, since im new to chat based application if any one have a chat application in core php please do share with me if would be very helpfull
Probably not an exact answer to your question, but could be useful.
Building a multi-user chat system from scratch on bare PHP, is a lot of work. You might want to look into php frameworks that save you at least half of the work.
Have a look at Laravel Framework: https://laravel.com/docs/5.6
It can integrate 3rd party services, example Pusher(https://pusher.com/) to run a multiple channel chat.
There are a lot of tutorials on youtube.com or https://laracasts.com/ (Laravel dedicated resource center and forum) that can help you get a chat up and running within an hour or two.
Why don't you integrate socket.io in your application.

Laravel 5.5 and angular 4 integration

I am working on a ERP system that is 6 years old and it is developed in raw PHP maintaining procedural coding structure without OOP or any Framework.
Finally our management decided to re-write the application with upgraded technologies. We will write the application in Laravel Framework. Database will be Oracle and Mysql both. In the front end we will use Angular 4.
The confusion is should i use Angular 4 for front end request handling and data binding? Or i should go with Javascript/Jquery. Last few days i am seeking some suggestions on the net. I found some also. My question is, how should i integrate Laravel and angular? should i go for API based approach maintaining two different project for Laravel and Angular or angular inside Laravel in a folder? If i go for API based approach, will my ajax request increased? The database of the application will be too large.
will API based approach make data loading slower?
All the request in the application will be ajax based and there will be no page refresh approach, but url will change on request.
Please suggest me the possible right solution to help me taking the right decision.
Angular 2/4+ has been designed to build SPA (Single Page Application). If you deep dive into angular then it will be clear to you that all the structures and conventions followed in the angular 2+ architecture are optimized for SPA.
So, if you want to use Angular with Laravel 5+ then I think the best way would be to use angular to build a standalone frontend application and use Laravel 5+ as a backend. That means laravel will act as an API server and a backend of the application.
Using laravel as an API server doesn't necessarily mean your database will be bigger. It is actually upto you how you design your application.
But, there is an another good solution.
You can use VueJs. There is a build in support for VueJs within Laravel 5+.
The benefit of using VueJs is that you can use it just like Jquery. That means you don't have to create separate standalone application for VueJs like Angular.
But, you can use it, page by page basis. It is loosely coupled.
You will get most of the benefit of Angular from it.
You can also use React.
You can use Angular 4 to rewrite your application. Fontend as Single Page Application and Backend exposing REST API will help you to build a good performance application.
I prefer to keep Angular and REST API separate as they should be independent to each other. REST APIs should always be generic so that they can be consumed by other applications too.
Yes, there will be lot of API calls to server. You can scale your REST APIs according to your number of requests. It is better building micro-services to ensure most of the services will not be impacted in case of lot of heavy load (at least part of application will be running). It will help you in future maintenance also.

How to create an API-centric application (more specifically: for HMVC in CodeIgniter)

Background:
I am working on a web application, that I plan to launch with iPhone and Android versions in the near future, and I have some questions about developing an API for use by the different front-ends.
I Started with This:
I was reading this TutsPlus tutorial, which talks about creating an API-centric web application, meaning that all of your browser calls and smartphone app calls are routed through the API.
What especially got me excited, was this statement in the tutorial from Twitter:
One of the most important architectural changes is that Twitter.com is now a client of our own API. It fetches data from the same endpoints that the mobile site, our apps for iPhone, iPad, Android, and every third-party application use.
The above statement reflects, what I would ideally like to create.
Then I Saw This:
I found this TutsPlus tutorial, which talks about, how to build a REST API for CodeIgniter. This was good, because I am developing my application in CodeIgniter, using the Modular Extensions plugin - HMVC, and I thought it would be perfect for my purposes.
The only thing, that I'm somewhat lost in, is the way Phil Sturgeon seems to suggest to create the API. He suggests to have all of your front-end code modules completed and then create an API for each of the modules in their respective directories (not necessarily in that order). This would be fine if I didn't want it to be API-centric.
And I Want to Know How to Do This:
How should I proceed to get a blend of both tutorials:
API-centric application that can be used by a browser application as well as separate mobile applications
REST API for CodeIgniter (with HMVC modules)
Should I:
Just follow the API-centric tutorial and try to tailor it to my needs?
Follow the CodeIgniter-specific tutorial and use Phil Sturgeon's advice for how to implement it with HVMC?
A combination of both?
Neither?
Take a look at Eating your own Dogfood by Nick Jackson. These are slides from CICONF 2012 where he details how this all works.
Building your API first is a great idea, then your application can use the API to fetch the data itself. This is of course a little slower as you're adding a HTTP request on data, but locally its not too bad and if you use HMVC to make the request it's even quicker.
In the tutorial I didn't get into HMVC because it's an extra subject, but it can be awesome for adding modular API's. In some simple applications I just make a /api folder like the tutorial suggested but PyroCMS Professional has an API module and a sexy route to allow modular API stuff:
$route['api/([a-zA-Z0-9_-]+)/(:any)'] = '$1/api/$2';
$route['api/([a-zA-Z0-9_-]+)'] = '$1/api/index';
This means I can have a "api.php" controller in every module and still use the /api/controller/method syntax or even /api/controller if you have function index_get().
I would strongly suggest you grab PyroCMS Professional for this feature. Yep you gotta pay, but it handles user key generation, authorisation, logging, etc all out of the box and handles your website too.

Android app 101: how do I sync web contents to the app? just starting building applications

I'm trying to build my own android application, and I was wondering if someone can direct me or help me find a tutorial on what to do to get contents between the two. I've been teaching myself php and web building and now I'm trying to build an android app.
But I have no idea as to how to sync the two pieces together? Say if I wanted to update some contents on my php page, how would it be updated to my android app? I'm also beginner at java, but I do have ambition to learning these things.
Thank you in advance fellow members.
ps I've been messing around with eclipse and the android platform.
I don't know if you wanted to write it from scratch or find some sort of library. I've also just started android development and I stumbled across a project called openmobster.
http://code.google.com/p/openmobster/
It's a library for integrating mobile applications with web based services. They have tutorials and documentation in the wiki section. For example: http://code.google.com/p/openmobster/wiki/AndroidSyncApp
If you were interested in writing something from scratch you should be able to browse the source to see some of the things they are doing.

How to write a REST API?

I'm writing an iPhone app as a hobby project and it will need a web service to provide it with data. It's not very different from what I do at work, but at work I only write views and controllers. Someone else is responsible for writing the model and usually the clients provide the web service.
I have done some web programming before, back when everyone were using MySQL and PHP, so my skills are a bit outdated, but I'm confident that I would be able to pull it of using the techniques I already know. However, I don't want to waste my time using obsolete tools. I've figured out that the state of the art would be to write a REST API. I was thinking that there should be some pretty good frameworks out there that pretty much just gives you a REST API with CRUD functionality as soon as you've defined a model.
I guess my question is: What would be the fastest way to get a REST API up and running? I really just want to focus on writing the iPhone app and not spend too much time on this API. It would be great if I could get web administration and revision history too. I should also add that the API isn't supposed to be public, so support for authentication would be great as well.
Just to be clear. I wouldn't mind a PHP framework. In fact it could possibly be better since I know that my current hosting supports it.
EDIT:
The links below which apparently were good for 3 years are no longer working so I went and found a couple of new tutorials that I think are going to stick around for a while. These are on the Ray Wenderlich site, a very well respected ios dev tutorial site. The first article actually references the broken links below but it is complete within itself:
How To Write A Simple PHP/MySQL Web Service for an iOS App
and the second one has a little twist to it. It used parse.com on the backend and AFNetworking. Both of which are quite excellent.
How To Synchronize Core Data with a Web Service – Part 1
I have fixed the broken links below by finding the articles in the way back machine. People seem to like the links so I will keep them. The links above should provide more food for thought.
I am doing exactly the same thing with my iphone app. I found this article on building a RESTful API in PHP:
https://web.archive.org/web/20130910164802/http://www.gen-x-design.com/archives/create-a-rest-api-with-php/
and there is also a followup article here:
https://web.archive.org/web/20130323001500/http://www.gen-x-design.com/archives/making-restful-requests-in-php/
with a link to source code at the bottom of the article.
I have programmed a REST API in ZEND Framework using the Zend_Rest_Controller, on the iPhone I used ASIHTTPRequest. My experience with both where good. At the beginning I had some trouble setting up ZEND and connecting it to mySQL, but once I figured out how to do it I was able to write the API very quickly. I can share more information with you if you have any further questions.
EDIT: There seems to be no official documentation on Zend_Rest_Controller. This link describes how to use it to create your API. You simply have to disable rendering in the init() of your subclass and implement the methods for each REST call.
Just to let you know:
I ended up using Ruby on Rails.
EDIT: Since this answer has been downvoted for not providing the reason behind choosing Ruby on Rails and also no instructions on how to write a REST API with it, I thought I would give you my motivation and some simple instructions.
I started reading a book about Ruby on Rails and realized that all I needed to do was to use scaffolding and I got a JSON REST API for free.
Here's a good guide to get you started: http://guides.rubyonrails.org/getting_started.html
When you have your Ruby on Rails environment up and running, creating your REST API isn't harder than running:
$ rails generate scaffold Post name:string title:string content:text
(Example from the above link.) I also found that Rails is very easy and free to deploy to heroku, which meant that I didn't have to pay for hosting for my very basic, low traffic, REST API. There are many other reasons why I am very happy to work with Ruby on Rails, but that's beyond the context of this question.
I followed a quite simple tutorial for creating RESTful APIs with PHP:
Corey Maynard - Creating a RESTful API with PHP
The main concept includes:
one abstract class that handles the parsing of the URI and returning the response, and
one concrete class that consists of just the endpoints for the API.
What about Python?
I'd use Python, Django and Piston.
I'd generate Django models from your
existent DB using inspectdb.
Add the Django admin to your models.
Add Django Piston to your app.
Profit.
With no experience with Python or Django probably it'll take you a day to develop this solution and all code is unit tested and proved to work.
If you want to use PHP I recommend using the CodeIgniter framework with Phil Sturgeon's REST server:
http://net.tutsplus.com/tutorials/php/working-with-restful-services-in-codeigniter-2
https://github.com/philsturgeon/codeigniter-restserver
Checkout the following PHP class that follows MVC.
http://www.phpclasses.org/package/5080-PHP-Implement-REST-Web-services-servers.html
Hope this helps.
If you already know PHP, there's nothing wrong with a PHP/MySQL backend. You can send all responses in iPhone-compatible plist xml format, and instantly turn the response into a NSDictionary/NSArray/NSNumber data structure with this short snippet of code:
NSString *response = [request responseString];
NSData* plistData = [response dataUsingEncoding:NSUTF8StringEncoding];
NSPropertyListFormat format;
NSString *errorStr;
NSDictionary* plist = [NSPropertyListSerialization propertyListFromData:plistData
mutabilityOption:NSPropertyListImmutable
format:&format
errorDescription:&errorStr];
I also use the ASIHTTP package for forming URLs, sending asynchronous requets, and receiving the responses, I highly recommend it:
http://allseeing-i.com/ASIHTTPRequest/
You should use whatever languages you are comfortable with for the web service. Any language that can formulate REST responses to requests is fine.
That said, if you want to get something running quickly, I suggest using Python on Google App Engine. It's free and you can use Java instead of Python if you so desire. App Engine supports authentication using OpenID and/or Google Accounts (not sure if they're mutually exclusive) so that should make things easier to code.
As far as making the requests on the iOS device, I suggest using ASIHTTPRequest.
Another option is restSQL, an ultra-lightweight persistence framework. See http://restsql.org. It supports MySQL and PostgreSQL and runs in a standard Java EE container, e.g. Apache Tomcat.
restSQL is a very unconventional data access layer. restSQL is not an object-oriented view of the database. It presents flat or hierarchical "views" of relational database tables. These views are query-able and updatable through a simple REST-based HTTP or Java API. The HTTP interface is based on REST principles, which use HTTP’s built-in features, rather than abstracting away from them.
You want a 'REST API with CRUD functionality' and that's exactly restSQL's sweet spot. You could do this with no code. Simply define your SQL Resources via XML files and start doing HTTP calls against them with full CRUD capability.

Categories