Creating a web service in PHP

后端 未结 7 886
日久生厌
日久生厌 2021-01-06 12:28

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 s

相关标签:
7条回答
  • 2021-01-06 13:05

    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.)

    Get it here.

    0 讨论(0)
  • 2021-01-06 13:08

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

    0 讨论(0)
  • 2021-01-06 13:08

    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

    0 讨论(0)
  • 2021-01-06 13:14

    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.

    0 讨论(0)
  • 2021-01-06 13:24

    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.

    0 讨论(0)
  • 2021-01-06 13:27

    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

    0 讨论(0)
提交回复
热议问题