您的位置:首页 > 编程语言 > PHP开发

thinkphp5 使用soap调用接口

2017-04-12 09:04 375 查看
分享下厚典在上海网站开发中的技术,thinkphp3.2调用soap这里不讲了,直接说一下thinkphp5调用soap,需要对tp5的结构有一定的了解,不然看不懂。

先要开启php_soap,不然会报错

一、服务器端

1、先在common.php下建function

如图:



代码如下:

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

如图



代码发下:

class Web extends Controller {

public function index(){

WebService(url('web/index'),'Web');

}

public function itemType( $type='', $style='' )

{

echo $type.$style;

}

二、客户端

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



代码如下:

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、客户端如果需要调用,可直接调用



class Swiper extends Common

{

public function index(){

//

$client = WebClient();

$res = $client->itemType('swiper_price','1234');

}

到此完事。其实很简单。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  thinkphp thinkphp5 soap