I want to create a REST service with PHP 5. I'd appreciate it if people would recommend some guides/tutorials on the subject. I'd like tutorials that cover the whole process, including the creation, securing and deployment of the service. Thanks.
I've been planning on building a full API/REST interface as of yet I've only implemented some features. The key concept that you need to get around is that it is simply a XML/JSON (etc) response to a predefined url.
You can quite easily get set up using .htaccess & mod rewrite to allow domain.com/method/var/id or some similar structure to redirect to domain.com/script.php?method=method&?var=var&?id=id . Once this is done, you can use these in your script, create your response and return it(print/echo) to script.php .
When a user sends a request, they will received the result that you have specified.
Json is very easy to use and implement thanks to http://php.net/manual/en/function.json-encode.php
You want to look into mod rewrite for apache.
As for authentication, it shouldnt differ to any other login/authentication have a look at sessions for php. http://php.net/manual/en/features.sessions.php
Hope this helps.
You could check out the wiki page on it. There is some abstract explaination on the matter.
For more concrete implementation take a look at the Zend package wich I use to build REST services.
I have always used the Zend_Rest libraries when trying to accomplish this task. They are pretty easy to implement and pretty well documented.
Zend_Rest
Related
I want to build a REST API using PHP, but without any framework. By the following requirments:
The code should be as simple as possible with OOP development principles in mind, easy to read and expand
Data should be kept in MySQL and to be returned as JSON in the given format
DO NOT use ANY Framework or ANY already written code, but to have structure
User input data validation
There should be no security issues
At first, I thought I should build complete MVC project, but I realized that actually I will probably don't need any views and I will use Services instead of controllers. And also models for both entities (Articles and Users).
I'm still not sure what is the perfect way to do it, so I will just tell you what I`m thinking so far...Sorry if Its a duplicate post but I haven't found much information about this and from the little I found, I got more confused.
I thinking of a simple router.php class that will have a method:
map($httpMethod, $route, $callback)
So, for example, I will call ("POST", "/users/register", registerUser(params)) or ("GET", "/users", registerUser(params)), just like I would do in a MVC web app.
I think I will need a model and a service for each of both entities. The service will execute the SQL for each CRUD operation. I think I know how to create the service, as it won't be much different than a controller.
But I wonder how can I create the model part for both entities. What exactly I will need for the models as a code?
First of all, it would be nice if you agree that this is the right way and if not, I would love to hear a lot of criticism because I'm currently confused and really don't know where to start.
I agree with you.
And suggest you to know about Loopback, it's good, like what you described.
I know your question is how to build your own rest api without framework, but it sounds like you'd actually make good use of at least some components (not necessarily a whole framework), do you really need/want to write a router from beginning?
if so sure, if no maybe some microframework? anyway, symfony has some info on how you would create your own framework (just as an example), they use couple of their own classes (i.e. httpcomponent), but just for the explanation of idea/way how things you want are done.
https://symfony.com/doc/current/create_framework/index.html
I found this library in PHP for get started with REST API's
php-platform/restful
This requires prior knowledge on using Composer
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))
I am new to REST. I understand how to create the functions within the application. BUT I am not sure how to access the JSON request using REST with headers. I am using CakePHP as my framework and in addition to that I am using the CakePHP REST plugin by KVZ (https://github.com/kvz/cakephp-rest-plugin). I am using ACL within my application and therefore I need to login using REST headers or however you access that information. I am pretty sure your suppose to use the amazon S3 authentication method but I don't understand it. I haven't been able to find any clear information on this.
THE QUESTION: How do I access the function (http://localhost:5555/app/data/getAll.json) using a REST JSON Request with PHP? Please include how to send the username and password with HTTP_AUTHORIZATION/header. The more detailed the answered, the better.
Thanks
REST isn't a technology, its just a way of naming your urls. Have a look at http://www.xfront.com/REST-Web-Services.html. You can use any mechanism you like to access the page although Curl with PHP is probably the easiest way for you.
Have a look at the PHP curl docs, there are examples there to guide you.
I am planning to write an API using PHP and and I am very interested in HTTP protocol type of API that exists but I don't know what people call that type of API. I think you can point me towards the best guide if I let you know how I want the developers to use it.
Assuming there are following functions.
Login
SignUp
GetRequests
Now the Login should take 2 parameters Username and Password of the user that exist in the database. It should then return a token which will be used to request other resources like "GetRequests" function. So once the user has the token, s/he can call "GetRequests" passing the token and will get the information.
The SignUp function works the same way as login but the input parameters are different. It also returns a token and can be used to make other requests for resources.
There are many other functions but I believe these are enough to get an idea of what type of API I am talking about. Can you please guide me as which Tools or Frameworks I can use to develop this sort of API quickly and easily.
You don't need any specific tools or frameworks to write such a thing or to put it another way, you can use any framework you want. A typical web API "function" works just like an ordinary web page, the only difference is that is doesn't accept cookies (and other browser-specific http headers) and usually returns its output as xml or json rather than html.
What you are describing is a general implementation pattern, and isn't specific to any single approach of implementing web services.
Nowadays, many web service API's are implemented either using REST or SOAP. You would be able to implement what you are describing with either of these.
You can get a technical overview through the above Wikipedia links, or, simply google REST vs. SOAP, and you'll get lots of pages giving you the good and the bad of both approaches.
My advice would be to Learn REST, JSON. I think this tutorial Working with RESTful Services in CodeIgniter might be interesting to study.
maybe something like this ?
$command = $_REQUEST['command'];
$param = $_REQUEST['param'];
echo api::$command($param);
class api{
static function saySomething($param){
return $param;
}
}
and try access the page with
http://localhost/test.php?command=SaySomething¶m=what
I have an existing website written in PHP. I would like to add a REST API. I like how easy creating a RESTful API was using Django. Are there any CONS for using Django for the sole purpose of creating an API on a PHP powered website? Thanks in advance.
There are a couple of cons:
your codebase will be larger
every change in the data model on one side must be done on the other side aswell
it will require more resources from your server
you have 2 systems to maintain
But for the rest, I can see why it would be easier to do this with Django than it would be to do with a plain PHP API. I have my doubts that there are no PHP libraries available to do something similar though.
I love Django, but I'm not sure that it would benefit you here. Maybe I don't completely understand how you plan to use it, but it seems like if you already have your data access and logic done in PHP then you would have to re-code that in Python in order to leverage Django.
If what you really want is clean urls and simple url mapping then you could probably use CodeIgniter or CakePHP. That way you don't need to rewrite your existing code in Python or having the same code in 2 different languages.