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');
}
到此完事。其实很简单。
先要开启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');
}
到此完事。其实很简单。
相关文章推荐
- 使用URL工具类调用webservice接口(soap)与http接口的实现方式
- PHP 使用SOAP调用接口开发,偶尔会报错
- 使用soap调用wsdl的接口实战
- webservice接口常见问题1:客户端使用SOAP方式调用CXF服务时异常
- thinkphp 调用wsdl接口实例化SoapClient抛出异常
- iOS-90-调用后台接口(WCF,soap访问方式,接口后缀.svc形式),使用WSDL2objc工具才能搞定
- 如何使用.NET调用新浪微博接口的实例
- php中使用soap的建立共享接口
- 使用 AJAX 调用SOAP Web 服务,第 2 部分: 扩展 Web 服务客户机
- 如何使用Soap消息调用Web Services
- 使用 Ajax 调用 SOAP Web 服务,第 1 部分: 构建 Web 服务客户机
- 使用Delphi调用WebServices接口的简单应用实例
- 使用c++开发客户端调用XML-RPC 接口
- 使用 AJAX 调用 SOAP Web 服务
- 在Thinkphp中使用Soap
- 在DLL编程中调用模版类时出现的类似"class“XXX”需要有 dll 接口由 class“XXX”的客户端使用"的warning的解决方案
- 使用Delphi调用WebServices接口的简单应用实例
- 使用巧妙的接口指针调用COM对象
- 使用 AJAX 调用SOAP Web 服务,第 2 部分: 扩展 Web 服务客户机
- 使用C#调用QC的接口