Using ExtJS4 + PHP with RESTful web services - php

I want to build an app using ExtJS 4 and PHP.
I will follow the MVC pattern with ExtJS 4.
However, I want to use RESTful webservices with PHP.
Yet I am not sure how to proceed.
How can I call for Web Services that are PHP functions?
How am I going to give the directory like structure to my links?

You can obviously use Ext.Ajax.request() to craft any RESTful URls that you want. However, be sure to check out the REST proxy that's built into Ext JS's data package. By connecting this to your store/model, you can dynamically generate RESTful URLs which can be used for the vast majority of your generic CRUD operations.
RE: the PHP side, that's a big question. Fortunately, there are literally a billion turtorials on creating RESTful services in PHP, so I'd dust off your Google-fu and start there. Here's a very simple introduction that just happens to use PHP: http://code.tutsplus.com/tutorials/a-beginners-guide-to-http-and-rest--net-16340

You are most likely looking for Ext.Ajax.request.
There are some examples of Ext.Ajax.request from this stack overflow article.

Related

Using App Engine Python application as proxy to PHP application

During the past two years I have built an App Engine application in Python. Soon it will be possible to use PHP on App Engine. I would like to use off-the-shelf PHP applications such as Wordpress, Mediawiki and phpBB together with my Python application. To the user it should be transparent which of the two applications (Python or PHP) she is using for a particular page. I consider the Python application to be the main application where I will do most of the programming. This is because I have more experience with Python and also because I already have written a lot of reusable code for App Engine.
Currently my approach is to build a proxy in Python that maps HTTP requests like this:
http://www.yellow.com/blog/* to http://phpapp.appspot.com/wordpress/client1/*
http://www.yellow.com/community/* to http://phpapp.appspot.com/phpbb/client1/*
yellow.com is a domain mapped to my Python application.
http://www.blue.com/wiki/* to http://phpapp.appspot.com/mediawiki/client2/*
http://www.blue.com/* to http://phpapp.appspot.com/wordpress/client2/*
blue.com is a domain mapped to my Python application.
Besides the blog, community or wiki, there are a lot of URL's that don't require PHP. These URL's are handled by the Python application. For example: http://www.yellow.com/admin/*.
I'm still struggling with the proxy to get the passing of cookies between the two applications right, but I think it's possible to do this.
It would be awesome if I could get it to work this way. However, it seems to me this is not the most elegant way to handle this. I know I could use subdomains to serve the PHP applications, but I would rather just use URL patterns. Also, with the proxy approach, I can tweak the returned HTML by the PHP application before serving it to the user. Another advantage of this approach is the ability to cache the pages from the PHP applications in memcache.
I would like to hear what you think of my approach to use Google App Engine (custom) Python and (off-the-shelf) PHP applications together. Will I run into problems with the proxy (Javascript, cookies, ...)? Would it be better to build everything in Wordpress, for example, with custom plugins written in PHP (the plugins could fetch data from the Python application)? Other suggestions?
Your use case is a good example of what Appengine's Modules are intended for. Take also a look at the dispatch mechanism.

php frameworks , when and why do i need them? [duplicate]

This question already has answers here:
To Use a PHP Framework or Not? [closed]
(12 answers)
Closed 9 years ago.
I need to create the server part of my mobile application.
The server part consists of registering a user to the server by making a simple http post request (json) from device to server and saving the users credentials to database (mysql).
Some http post requests from the devices , stating to the server in which kind of events they want to be subscribed (my server keeps data of some events).
Send push notifications , to my devices from the server when a new event is generated (already have the php scripts for that).
For a kind of server likes this , do i need to use a php framework? In this page i found a list of different frameworks. I found this page while reading php tutorials and some of them suggested using a php framework.
But what are these frameworks for? Do i need to learn/use one? When are they needed. Are they for front-end sites?
Thank you for reading my post :)
Frameworks are created to make your work easy.
Advantages of using framework
There are a lot of functions that are already written
There are some good programic patterns. You have good code and file organisation.
There are a lot of libraries and plugins.
There is MVC pattern already implemented.
They give you advantage of faster develompent.
There is community support
They are mostly fast and secure.
Why use a framework with PHP?
Why do I need to use a popular framework?
Using framework in the begining may seem to be hard, but when you learn it, it makes coding a lot faster!
Disadvantages of using framework:
You will learn a lot by writing your own.
If you need it for small project like portfolio, there is no need for giving yourself more work to set the framework to work.
There is a lot of things in framework that you don't need and they take its space on server.
https://softwareengineering.stackexchange.com/questions/49488/when-not-to-use-a-framework
It's not about frontend project, if you have big project you should use framework if it's little it's not worth. That is my opinion.
I'd suggest you Zend Framework or Symfony if you decide to use Framework.
I recently have the similar project requirement for a REST based application on PHP. We choose Zend Framework 2 which have REST supported architecture. I check out same support on Symfony 2. So One thing i get it that Framework gave you standrized way of code/architecture + set of tools. If I decided for core php application, then obviously I need to write a REST based API Server architecture. Which is already available in case of Frameworks.

Building a REST API quickly from a Database model

I've designed my database diagram(11 entites) for my prototype application. I need to have an REST API on top of this that will allow me to build a prototype android tablet application that talks to it.
Considering this is only for prototype purposes. What would be the quickest way to get a API up and running that would allow me to get, put, delete etc..
Security at this point isn't an issue so even if all add edit delete were open that would be fine.
My initial idea is to build this with PHP and MySQL maybe using the Yii framework to help but I feel there might be something that might be faster for my purpose.
Any thoughts or recommendations or advice?
It is worth looking into API rapid-prototyping tools like Apify (tutorial) since you're starting from scratch. If you're planning to have both an API and a Web site accessing the same data, consider using one framework for both as it helps prevent duplicating logic. CakePHP and Yii are good examples of multipurpose frameworks that are suited for this type of development.
If Python is an option, it is trivial to use a light-weight http framework to build a REST API that accesses a database.
Here's a snippet for a fully functioning http server using the itty framework: https://github.com/toastdriven/itty/blob/master/examples/web_service.py and https://github.com/toastdriven/itty/blob/master/examples/posting_data.py
Look at this project which allows you to create an API REST in seconds.
https://github.com/GeekyTheory/Automatic-API-REST
Features
Creation a powerful API REST of your MySQL Data Base in Seconds.
Management of the API in situ, it is not neccesary an extra data base.
Private tables and fields.
Custom queries.
Installation
Clone the repo.
git clone https://github.com/GeekyTheory/Automatic-API-REST/
Place it in your web folder /var/www/YourWebPage/
Open the file config.php and complete all the fields with the server credencials.
Go to domain.com/AutomaticaApiRest

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.

How can I access a REST data source from a PHP framework?

We are looking to build a PHP web application to manage data that is available only through a 3rd party REST like interface.
We would like to use an existing PHP framework to do this, we're considering cakephp and codeigniter, but are open to other options.
We expect the biggest problem will be creating models that utilise the REST API as a data source instead of a local database.
I've written a base ReST datasource plugin for CakePHP and am using it in lots of other plugins that interact with ReSTful APIs. Plugins I've released so far are YahooGeoPlanet and bit.ly and I'm also currently working on ones for Google Apis (YouTube and Analytics), Twitter, Twitpic and Facebook. They use the CakePHP's excellent HttpSocket class that deceze mentions.
Have a look at those CakePHP plugins and you should be able to see how simple but powerful they are.
You might look at Zend_Rest_Client, which I've never used, but appears worth a look.
Failing that, Zend_Http_Client is pretty solid for abstracting away all the curl ugliness.
Either one can be used independently of the rest of the Zend Framework stuff, and should interoperate just fine with other frameworks.
You can implement it as a Datasource in Cake, which to the Model and therefore to the rest of the application behaves exactly as a database would. Internally the Datasource can dynamically fetch the data using cURL or the Cake HttpSocket. Have a look at the Twitter API example.

Categories