Creating a web service in PHP

I would like to create a web service in PHP that can be used by various consumers (web page, Android device, iOS device).

I'm coming from a Microsoft background, so I'm pretty sure I'll do it in C #, etc. Ideally, I would like to provide a REST service that can send JSON.

Can you tell me how can I do this in PHP?

thank

Tarik

+3


source to share


7 replies


I have developed a class that is the native RAP class of the SoapServer class.

You just include the RestServer.php file and then use it like this.

class Hello
{
  public static function sayHello($name)
  {
    return "Hello, " . $name;
  }
}

$rest = new RestServer(Hello);
$rest->handle();

      

Then you can make calls from another language like this:



http://myserver.com/path/to/api?method=sayHello&name=World

      

(Note that it doesn't matter what order the parameters are in the query string. Also, the parameter key names as well as the method name are case insensitive.)

Get it here .

+7


source


I would advise you to go for Yii

, it's worth learning. You can easily fix it in this. Web service . Yii provides CWebService

and CWebServiceAction

to make it easier to inject a web service into a web application. The web service relies on SOAP

as the main layer of the communication protocol stack.



0


source


The easiest way in PHP is to use GET / POST for input and echo

output of data. Here's a sample:

<?php if(empty($_GET['method'])) die('no method specified');
switch($_GET['method']){
   case 'add': { 
       if(empty($_GET['a']) || empty($_GET['b'])) die("Please provide two numbers. ");
       if(!is_numeric($_GET['a']) || !is_numeric($_GET['b'])) die("Those aren't numbers, please provide numbers. ");
       die(''.($_GET['a']+$_GET['b']));
       break; 
   }
}

      

Save this as test.php and go to http://localhost/test.php?method=add&a=2&b=3

(or wherever your webserver is) and it should say 5

.

0


source


PHP has built-in SOAP server support ( The SoapServer Class Guide shows this) and I found it pretty easy to use.

Building a REST style API is pretty straightforward if you are using a framework. I don't want to discuss which structure is better, but CakePHP also supports XML output and I'm sure others will.

If you're coming from a Microsoft background, be careful when thinking of "datasets". They are very specific Microsoft stuff and have been my scourge in the past. This probably won't be a problem for you, but you can just see the differences between Microsoft and open source implementations.

And of course PHP has a built-in json_encode () function .

0


source


You can check out this nice RESTful server written for Codeigniter, a RESTful server . It supports XML, JSON, etc. responses, so I think this is your library. There is even a good tutorial on the Tutsplus network - Working with RESTful Services in CodeIgniter

0


source


You can also try PHP REST Data Services https://github.com/chaturadilan/PHP-Data-Services

0


source


You can use any existing PHP framework like CodeIgniter or Symfony or CakePHP to create web services.

You can also use plain PHP like in this

0


source







All Articles