PHP 简单实现 WebService

0x00 准备工作

  • 开启PHP的Soap扩展

  • 准备要提供服务的代码,服务代码如下:

      #file service.php
      class Service {
          public function HelloWorld() {
              return "Hello";
          }
          public function Add($a, $b) {
              return $a + $b;
          }
      }
    
  • wsdl生成类SoapDiscovery.class.php,后附

0x01 创建WSDL文件

使用SoapDiscovery类生成WSDL文件,文件名为Service.wsdl,生成代码如下:
其中初始化SoapDiscovery类的时候,第一个参数为提供服务的类名

#file create.php
getWSDL();

0x02 使用wsdl文件创建WebService

这里PHP必须开启soap扩展,否者报错, 代码如下:

#file interface.php
SOAP_1_2);
    $ser->setClass('Service');
    $ser->handle();

这里我们假设这个访问地址为 http://gl6.cc/wsdl/interface.php

0x03 测试WebService

这里需要使用的WebService的访问地址,代码如下

#test.php
'http://gl6.cc/wsdl/interface.php',
    'uri'=>'abcd',
    'style'=>SOAP_RPC,
    'use'=>SOAP_ENCODED 
];
echo $soap->Add(12,2);

访问,如果输出等于14,恭喜成功。

0x04 其他

有时候,有些软件需要提供wsdl文件, 直接以xml格式输出wsdl文件即可,代码如下:

#wsdl.php

0x05 附录

  • SoapDiscovery.class.php

'

class_name = $class_name;
        $this->service_name = $service_name;
    }

    /**
     * SoapDiscovery::getWSDL() Returns the WSDL of a class if the class is instantiable.
     * 
     * @return string
     * */
    public function getWSDL() {
        if (empty($this->service_name)) {
            throw new Exception('No service name.');
        }
        $headerWSDL = "\n";
        $headerWSDL.= "service_name\" targetNamespace=\"urn:$this->service_name\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tns=\"urn:$this->service_name\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns=\"http://schemas.xmlsoap.org/wsdl/\">\n";
        $headerWSDL.= "\n";

        if (empty($this->class_name)) {
            throw new Exception('No class name.');
        }

        $class = new ReflectionClass($this->class_name);

        if (!$class->isInstantiable()) {
            throw new Exception('Class is not instantiable.');
        }

        $methods = $class->getMethods();

        $portTypeWSDL = '';
        $bindingWSDL = '\n\n";
        $serviceWSDL = '\n\nservice_name . 'Port" binding="tns:' . $this->service_name . "Binding\">\n\n\n";
        $messageWSDL = '';
        foreach ($methods as $method) {
            if ($method->isPublic() && !$method->isConstructor()) {
                $portTypeWSDL.= '\n" . '\ngetName() . "Response\" />\n\n";
                $bindingWSDL.= '\n" . '\nservice_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n\n\nservice_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n\n\n";
                $messageWSDL.= '\n";
                $parameters = $method->getParameters();
                foreach ($parameters as $parameter) {
                    $messageWSDL.= '\n";
                }
                $messageWSDL.= "\n";
                $messageWSDL.= '\n";
                $messageWSDL.= '\n";
                $messageWSDL.= "\n";
            }
        }
        $portTypeWSDL.= "\n";
        $bindingWSDL.= "\n";
        //return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '');
        //生成wsdl文件,将上面的return注释
        $fso = fopen($this->class_name . ".wsdl", "w");
        fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, ''));
    }

    /**
     * SoapDiscovery::getDiscovery() Returns discovery of WSDL.
     * 
     * @return string
     * */
    public function getDiscovery() {
        return "\n\n\n";
    }
}
?>

你可能感兴趣的:(PHP 简单实现 WebService)