怎么在PHP中对Webservice进行调用

发布时间:2021-03-09 17:00:04 作者:Leah
来源:亿速云 阅读:157

本篇文章给大家分享的是有关怎么在PHP中对Webservice进行调用,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。

方法一:直接调用

复制代码 代码如下:


<? 
/******************************************************************************/
/*  文件名 : soapclient.php
/*  说  明 : WebService接口客户端例程
/******************************************************************************/
include('NuSoap.php'); 

// 创建一个soapclient对象,参数是server的WSDL  
$client = new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl'); 

// 参数转为数组形式传递 
$aryPara = array('strUsername'=>'username', 'strPassword'=>MD5('password')); 

// 调用远程函数 
$aryResult = $client->call('login',$aryPara); 

//echo $client->debug_str; 
/*
if (!$err=$client->getError()) {
  print_r($aryResult); 
} else { 
  print "ERROR: $err"; 
}
*/

$document=$client->document; 
echo <<<SoapDocument 
<?xml version="1.0" encoding="GB2312"?> 
 <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:si="http://soapinterop.org/xsd"> 
   <SOAP-ENV:Body> 
   $document
   </SOAP-ENV:Body> 
 </SOAP-ENV:Envelope> 
SoapDocument; 

?> 

复制代码 代码如下:


<?
/******************************************************************************/
/*  文件名 : soapclient.php
/*  说  明 : WebService接口客户端例程
/******************************************************************************/
include('NuSoap.php');

// 创建一个soapclient对象,参数是server的WSDL
$client = new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');

// 参数转为数组形式传递
$aryPara = array('strUsername'=>'username', 'strPassword'=>MD5('password'));

// 调用远程函数
$aryResult = $client->call('login',$aryPara);

//echo $client->debug_str;
/*
if (!$err=$client->getError()) {
  print_r($aryResult);
} else {
  print "ERROR: $err";
}
*/

$document=$client->document;
echo <<<SoapDocument
<?xml version="1.0" encoding="GB2312"?>
 <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:si="http://soapinterop.org/xsd">
   <SOAP-ENV:Body>
   $document
   </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>
SoapDocument;

?>

方法二:代理方式调用

复制代码 代码如下:


<? 
/******************************************************************************/
/*  文件名 : soapclient.php
/*  说  明 : WebService接口客户端例程
/******************************************************************************/
require('NuSoap.php');  

//创建一个soapclient对象,参数是server的WSDL  
$client=new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');  

//生成proxy类  
$proxy=$client->getProxy();  

//调用远程函数  
$aryResult=$proxy->login('username',MD5('password')); 

//echo $client->debug_str; 
/*
if (!$err=$proxy->getError()) {
  print_r($aryResult); 
} else { 
  print "ERROR: $err"; 
}
*/

$document=$proxy->document; 
echo <<<SoapDocument 
<?xml version="1.0" encoding="GB2312"?> 
 <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:si="http://soapinterop.org/xsd"> 
   <SOAP-ENV:Body> 
   $document
   </SOAP-ENV:Body> 
 </SOAP-ENV:Envelope> 
SoapDocument; 

?> 

复制代码 代码如下:


<?
/******************************************************************************/
/*  文件名 : soapclient.php
/*  说  明 : WebService接口客户端例程
/******************************************************************************/
require('NuSoap.php');

//创建一个soapclient对象,参数是server的WSDL
$client=new soapclient('http://localhost/Webservices/Service.asmx?WSDL', 'wsdl');

//生成proxy类
$proxy=$client->getProxy();

//调用远程函数
$aryResult=$proxy->login('username',MD5('password'));

//echo $client->debug_str;
/*
if (!$err=$proxy->getError()) {
  print_r($aryResult);
} else {
  print "ERROR: $err";
}
*/

$document=$proxy->document;
echo <<<SoapDocument
<?xml version="1.0" encoding="GB2312"?>
 <SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:si="http://soapinterop.org/xsd">
   <SOAP-ENV:Body>
   $document
   </SOAP-ENV:Body>
 </SOAP-ENV:Envelope>
SoapDocument;
?>

  许多使用NuSoap 调用.NET WebService或J2EE  WebService的朋友可能都遇到过中文乱码问题,下面介绍这一问题的出现的原因和相应的解决方法。
  NuSoap调用WebService出现乱码的原因:
  通常我们进行WebService开发时都是用的UTF-8编码,这时我们需要设置:

复制代码 代码如下:


$client->soap_defencoding = 'utf-8';
$client->soap_defencoding = 'utf-8';

  同时,需要让xml以同样的编码方式传递:

复制代码 代码如下:


$client->xml_encoding = 'utf-8';
$client->xml_encoding = 'utf-8';


  至此应该是一切正常了才对,但是我们在输出结果的时候,却发现返回的是乱码。
  NuSoap调用WebService出现乱码的解决方法:
  实际上,开启了调试功能的朋友,相信会发现$client->response返回的是正确的结果,为什么$result = $client->call($action, array('parameters' => $param)); 却是乱码呢?
  研究过NuSoap代码后我们会发现,当xml_encoding设置为UTF-8时,NuSoap会检测decode_utf8的设置,如果为true,会执行 PHP 里面的utf8_decode函数,而NuSoap默认为true,因此,我们需要设置:

复制代码 代码如下:


$client->soap_defencoding = 'utf-8'; 
$client->decode_utf8 = false; 
$client->xml_encoding = 'utf-8';
$client->soap_defencoding = 'utf-8';
$client->decode_utf8 = false;
$client->xml_encoding = 'utf-8';

以上就是怎么在PHP中对Webservice进行调用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。

推荐阅读:
  1. php 调用 .net webservice
  2. 怎么在fastadmin中对JavaScript进行调用

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php webservice

上一篇:如何在PHP中使用SOAP调用WebService数据

下一篇:怎么在PHP中调用C#开发的dll库

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》