1

I am working on rest webservices in php. I want to know that can i make rest webservices in php without using any framework(cakephp,zend,Tonic). if anyone have some some idea. Please let me know ?

r_y
  • 51
  • 1
  • 4
  • Is this is a duplicate? Read this question: http://stackoverflow.com/questions/359047/php-detecting-request-type-get-post-put-or-delete – SiliconMind Feb 29 '12 at 12:50

3 Answers3

1

I implemented a RESTful service using cURL. Now the only reason we did not use a framework was because the version of PHP we were using was really old. We also had a manager who dictated that this was how it was to be done. So, my advice is to not re-invent the wheel and go with something that is going to do most of the work for you.

Zoidberg
  • 10,137
  • 2
  • 31
  • 53
  • How exactly would you use `curl` to develop a webservice? You can of course use it to call one but `curl` is just not for query processing... – s1lence Feb 29 '12 at 12:55
  • He wants to know how to make a RESTful webservice not using a framework. cURL is a layer that does all of the HTTP work, and you can send and receive the data in either JSON or XML as you can make your RESTful service recognize either format (so your answer does infact work well with mine). – Zoidberg Feb 29 '12 at 12:58
  • Also, in a RESTful webservice, you use GET to retreive data, and POST (or PUT) to save data and DELETE to remove data which are all standard HTTP request types. Again, there are frameworks out there that do much more for you than cURL, and I had recommended that he use one of those unless it is absolutely neccessary not to – Zoidberg Feb 29 '12 at 12:59
1

You will have to readout the requests yourself, do some processing (whatever your webservice is intended to do) and return some representation of an answer.

Often Webservices use http methods like PUT. See here for explanation about how to do so.

Answers are often in JSON or xml format. PHP has extensions or included support for both formats. See here for JSON, and here for xml. Maybe you even will have to use some of these for input processing.

A important part before you start with your development is however that you exactly know what your webservice should do, how it should be called and what it should return. So mainly define your API before starting to code.

s1lence
  • 2,188
  • 2
  • 16
  • 34
  • cURL is a compliment to this. If done properly (using cURL) you could have your webservice accept both XML and JSON requests/responses. – Zoidberg Feb 29 '12 at 13:00
0

Let's say we have our application directory structure like this

  • api
    • request.php
    • .htaccess

Edit .htaccess file which is under api directory with below code

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)$ request.php?_url=$1 [QSA,NC,L]
</IfModule>

Every request comes with any pattern starting with "/api" will go to our "request.php"

This will handle any rest pattern with /api/*

Our request.php will look like

<?php

session_start();

$_url = $_GET['_url'];


switch ($_url) {
    case "products" :
        echo "All Products";
        break;
    case "product" :
        echo "One Product";
        break;
    case "product/save" :
        $request_body = file_get_contents('php://input');
        $data = json_decode($request_body);
        echo "Data saved to DB";
        break;
    default :
        header($_SERVER["SERVER_PROTOCOL"] . "404 Not Found", true, 404);
}

check out here

prabhatojha
  • 1,925
  • 17
  • 30