PHP调用webservice接口笔记

1、【开启php_soap.dll扩展】

找到php.ini文件
;extension=php_soap.dll

删除掉";" ,重启apache服务器

 

2、【下载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, '');  
        $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";  
    }  
}  
  
?>  

 

 

 

3、【生成wsdl文件】

创建文件server.php,代码如下:

getWSDL();
}
 
//SOAP开启并接收Client传入的参数响应 
$server = new SoapServer(WSDL_URL);
$server->setClass('Test');
$server->handle();
 
/* 测试类 */
class Test {
    public function hello($name = '') 
    {
        return 'Hello '.$name.'.';
    }
}

运行server.php文件,即可生成Test.wsdl文件,如下:




hello">
helloRequest" />
helloResponse" />





domainName#Test#hello" />












helloRequest">
name" type="xsd:string" />

helloResponse">
hello" type="xsd:string" />

Test.wsdl文件中比较关键的地方用颜色标出来了,建议大家自己生成下,不要直接复制。

 

4、【调用接口】

创建client.php,代码如下:

 1));/* 创建soapclient对象 */
 
try {
    $result = $client->hello('man');/* 调用方法 */
    var_dump($result);
} catch (SoapFault $f){
    echo '__getFunctions = ';
    var_dump($client->__getFunctions());
    echo '
-----------------------
'; echo '__getLastRequest = '.$client->__getLastRequest(); echo '
-----------------------
'; echo '__getLastResponse = '.$client->__getLastResponse(); echo '
-----------------------
'; echo "Error Message: {$f->getMessage()}"; } ?>

完成。
 

你可能感兴趣的:(php)