前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP调用Webservice实例[通俗易懂]

PHP调用Webservice实例[通俗易懂]

作者头像
全栈程序员站长
发布2022-09-13 21:08:17
2.6K0
发布2022-09-13 21:08:17
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

NuSoap是PHP环境下的WebService编程工具,用于创建或调用WebService。它是一个开源软件,是完全采用PHP语言编写的、通过HTTP收发SOAP消息的一系列PHP类,由NuSphere Corporation(http://dietrich.ganx4.com/nusoap/ )开发。NuSOAP的一个优势是不需要扩展库的支持,这种特性使得NuSoap可以用于所有的PHP环境,不受服务器安全设置的影响。  

方法一:直接调用 view plaincopy to clipboardprint? <?   /******************************************************************************/  /*  文件名 : 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;

?> 方法二:代理方式调用 view plaincopy to clipboardprint? <?   /******************************************************************************/  /*  文件名 : 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编码,这时我们需要设置: view plaincopy to clipboardprint? $client->soap_defencoding = ‘utf-8’;  $client->soap_defencoding = ‘utf-8’;

  同时,需要让xml以同样的编码方式传递: view plaincopy to clipboardprint? $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,因此,我们需要设置: view plaincopy to clipboardprint? $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’;

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/162642.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
主机安全
主机安全(Cloud Workload Protection,CWP)基于腾讯安全积累的海量威胁数据,利用机器学习为用户提供资产管理、木马文件查杀、黑客入侵防御、漏洞风险预警及安全基线等安全防护服务,帮助企业构建服务器安全防护体系。现支持用户非腾讯云服务器统一进行安全防护,轻松共享腾讯云端安全情报,让私有数据中心拥有云上同等级别的安全体验。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档