thinkphp3.2 + soap

服务器配置

扩展libxml2下载地址:http://xmlsoft.org/downloads.html

在windows下的php.ini文件里

找到这一行代码(如没有则自行添加)

extension=php_soap.dll

SOAP在php.ini中还有自己的配置部分,如下所示

[soap]
; Enables or disables WSDL caching feature.
soap.wsdl_cache_enabled=1
; Sets the directory name where SOAP extension will put cache files.
soap.wsdl_cache_dir="/tmp"
; (time to live) Sets the number of second while cached file will be used
; instead of original one.
soap.wsdl_cache_ttl=86400

这段配置控制了SOAP扩展的WSDL缓存特性。默认情况下,WSDL描述文件在24小时(86400sec)内都在缓存设置的目录下。

另外还需要修改一下代码段,将always_populate_raw_post_data设置为On,并去掉分号,表示允许去的没经格式化的POST数据。

; Always populate the $HTTP_RAW_POST_DATA variable.
always_populate_raw_post_data = On

然后找到如下代码,设置为0

soap.wsdl_cache_enabled=0

这样,在代码调试时,避免遇到一些莫名其妙的错误,完成web服务开发之后,要记得改为1,即打开WSDL缓存,使代码运行得更快。

为了证明你成功配置好SOAP,请使用phpinfo()函数确认下:

thinkphp3.2 + soap

无WSDL

 /* 官网用户注册  判断是否已存在 */
    public function isUsername($username){
        $User = new UserApi();
        $result = $User->checkUsername($username);
        if($result >=0){
            $data['status'] = true;
        }else{
            $data['status'] = false;
        }
        return json_encode($data);
        exit;
    }

  服务器端:
    public function passportServer(){
        try{
            $server = new SoapServer(null,array("uri"=>"http://www.6ycom.com/soap/","location"=>"http://http://www.6ycom.com/soap/passportServer"));
            //$server -> addFunction(array('a', 'b'));
            $server->setClass(get_class($this));
            $server -> handle();
        }catch(SoapFault $e){
            echo $e->getMessage();
        }

    }
客户端:
    public function passportClient(){
        $client = new SoapClient(null,array("uri"=>"http://www.6ycom.com/soap/","location"=>"http://http://www.6ycom.com/soap/passportServer","trace" => 1));
        $param = ['tongtong'];
        echo $result=$client->__soapCall('isUsername',$param);

    }

WSDL形式:

调用生成wsdl类:SoapDiscovery.class.php

服务器端:

<?php

class MyClass {
  public function isExistUser($param) {
    $count = M('ucenter_member')->where($map)->count();
    return $count.$param;
  }
}


require_once 'SoapDiscovery.class.php';
try {
  $disco = new SoapDiscovery('MyClass','MyClass');
  header("Content-type: text/html; charset=utf-8");
   $disco->getWSDL();
  $server = new SOAPServer('MyClass.wsdl', array('soap_version' => SOAP_1_2));

  $server->setClass('MyClass');
  $server->handle();
}

catch (SOAPFault $f) {
  print $f->faultstring;
}

客服端:

<?php
//$client = new SoapClient(null, array('location' => "http://localhost/server.php",'uri'      => "http://localhost/server.php"));
$client = new SoapClient("http://localhost:8082/Webservice/server.php?wsdl");
$param = ['33666fsdfdrewre666663'];
echo $return = $client->__soapCall("isExistUser",$param);