Reputation: 1469
I would like to create a web service in PHP which can be consumed by different consumers (Web page, Android device, iOS device).
I come from a Microsoft background so am confortable in how I would do it in C# etc. Ideally I would like to be able to provide a REST service which can send JSON.
Can you let me know how I can achieve this in PHP?
Thanks
Tariq
Upvotes: 3
Views: 19540
Reputation: 766
You can use any existing PHP framework like CodeIgniter or Symfony or CakePHP to build the webservices.
You can also use plain PHP like disscussed in this example
Upvotes: 0
Reputation: 1572
You can also try PHP REST Data Services https://github.com/chaturadilan/PHP-Data-Services
Upvotes: 0
Reputation: 5137
I developed a class that is the PHP native SoapServer class' REST equivalent.
You just include the RestServer.php file and then use it as follows.
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 params are provided in the query string. Also, the param key names as well as the method name are case-insensitive.)
Upvotes: 7
Reputation: 2447
You can check out this nice RESTful server written for Codeigniter, RESTful server. It does support XML, JSON, etc. responses, so I think this is your library. There is even a nice tutorial for this on the Tutsplus network - Working with RESTful Services in CodeIgniter
Upvotes: 0
Reputation: 2140
PHP does have native support for a SOAP server ( The SoapServer class manual shows it) and I've found it pretty simple to use.
Creating a REST style API is pretty easy if you use a framework. I don't want to get into a debate about which framework is better but CakePHP also supports output as XML and I'm pretty sure others will as well.
If you're coming from a Microsoft background just be careful about thinking about "datasets". They are a very specific Microsoft thing and have been a curse of mine in the past. It's probably not going to be an issue for you, but you may want to just see the differences between Microsoft and open implementations.
And of course PHP has a native json_encode() function.
Upvotes: 0
Reputation: 10907
Easiest way in PHP is to use GET/POST as data-in and echo
as data-out.
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
.
Upvotes: 0
Reputation: 7419
I would suggest you go for Yii
it is worth of learning. You can easily establish it in this.
Web Service. Yii provides CWebService
and CWebServiceAction
to simplify the work of implementing Web service in a Web application. Web service relies on SOAP
as its foundation layer of the communication protocol stack.
Upvotes: 0