thinkphp5 使用soap调用接口

来源:互联网 发布:外立面效果图制作软件 编辑:程序博客网 时间:2024/06/06 07:34
分享下厚典在上海网站开发中的技术,thinkphp3.2调用soap这里不讲了,直接说一下thinkphp5调用soap,需要对tp5的结构有一定的了解,不然看不懂。

先要开启php_soap,不然会报错

一、服务器端

1、先在common.php下建function

如图:

thinkphp5 使用soap调用接口


代码如下:

function WebService($uri,$class_name='',$namespace='controller',$persistence = false){
    $class = 'index\\'. $namespace .'\\'. $class_name;
    $class = 'app\index\controller\Web';
    $serv = new \SoapServer(null,array("uri"=>$uri));
    $serv->setClass($class);
    if($persistence)
        $serv->setPersistence(SOAP_PERSISTENCE_SESSION);//默认是SOAP_PERSISTENCE_REQUEST
    $serv->handle();
    return $serv;
    
}

2、在服务器端建调用的function  

如图

thinkphp5 使用soap调用接口

代码发下:

class Web extends Controller {
    public function index(){
        WebService(url('web/index'),'Web');
    }
    public function itemType( $type='', $style='' )
    {
       echo $type.$style;
    }



二、客户端

同样是在common,php下建个function 注意:我这个客户端跟服务端并不在同一个项目下,是分开得两个访问地址

thinkphp5 使用soap调用接口


代码如下:

function WebClient($url='',array $options=array()){
  if(stripos($url,'?wsdl')!== false)
  {
    return new \SoapClient($url,array_merge(array('encoding'=>'utf-8'),$options));//WSDL
  }
  else
  {
    $location = "http://yb.houapi.cn/";
    $uri = "index/web/index";
    $options = array_merge(array('location'=>$location,'uri'=>$uri,'encoding'=>'utf-8'),$options);
    return new \SoapClient(null,$options);//non-WSDL
  }
}

2、客户端如果需要调用,可直接调用

thinkphp5 使用soap调用接口



class Swiper extends Common
{
    public function index(){
        //
        $client = WebClient();
        $res = $client->itemType('swiper_price','1234');
    }

到此完事。其实很简单。
0 0