PHP SOAP实现WebService

第一种:WSDL模式 
Client.php客户端请求文件 
http://localhost/soap/Wsdl/wsdl.php?wsdl 修改为自己目录的对应路径 ?wsdl 必须带着否则会报错

 SOAP_1_2));
    try{
        $result = $client->test();
        echo $result; 
    }catch (SoapFault $f){
        echo "Error Message: {$f->getMessage()}";
    }
?>

SoapDiscovery.class.php 核心类

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

    /**
     * 获取WSDL
     * 
     * @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 string
     **/
    public function returnWSDL() {
        return "\n\n\n";
    }
}

?>

wsdl.php 处理文件

setClass('YourCode');
    $servidorSoap->handle();
}else{
    require_once './SoapDiscovery.class.php';
    // 创建WSDL
    $disco = new SoapDiscovery('YourCode','Solsoft_YourCode');
        header("Content-type: text/xml");
    if (isset($_SERVER['QUERY_STRING']) && strcasecmp($_SERVER['QUERY_STRING'],'wsdl')==0) {
        echo $disco->getWSDL();
    }else {
        echo $disco->returnWSDL();
    }
}

?>

yourCode.php 自定义的代码,必须为一个类,类名在wsdl.php 中使用,注意对应修改。

说明:

client.php 客户端请求文件 
http://localhost/soap/Wsdl/wsdl.php?wsdl 修改为自己目录的对应路径 ?wsdl 必须带着否则会报错 
Wsdl/ 
SoapDiscovery.class.php 核心类 
wsdl.php 处理文件 
yourCode.php 自定义的代码,必须为一个类,类名在wsdl.php 中使用,注意对应修改。

在项目中和其他公司java程序员做接口,他们那边只会Web Services来做接口,之后整理该扩展,分享一下。

访问 http://localhost/soap/Wsdl/wsdl.php?wsdl 的部分截图。 

PHP SOAP实现WebService_第1张图片

 

 PHP SOAP实现WebService_第2张图片

 

你可能感兴趣的:(服务器)