浅谈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';
相关文章
- 这篇文章主要介绍了c# 三种方法调用WebService接口的相关资料,文中示例代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下...2020-07-07
C#使用Http Post方式传递Json数据字符串调用Web Service
这篇文章主要为大家详细介绍了C#使用Http Post方式传递Json数据字符串调用Web Service,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25- 这篇文章介绍了c#动态调用Webservice的两种方法实例,有需要的朋友可以参考一下...2020-06-25
- 这篇文章主要给大家介绍了关于c#中的WebService及其调用方式的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
- 本文章来给大家详细介绍在php中如何来调用执行mysql存储过程然后返回由存储过程返回的值了,有需要了解的同学可进入参考。 。调用存储过程的方法。 a。如果存储过...2016-11-25
DTO 实现 service 和 controller 之间值传递的操作
这篇文章主要介绍了DTO 实现 service 和 controller 之间值传递的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-02-22- 方法一:直接调用复制代码 代码如下:<? /******************************************************************************/ /* 文件名 : soapclient.php /* 说 明 : WebService接口客户端例程 /****************...2014-06-07
- 这篇文章主要为大家详细介绍了C# 创建、部署和调用WebService的简单示例,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2020-06-25
- 下面小编就为大家带来一篇C# Soap调WebService的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了c#动态改变webservice的url访问地址,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了Spring boot 无法注入service问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-10-09
- 这篇文章主要介绍了WebService 的简单封装接口调用方法,主要是通过简单的sql语句来查询数据库,从而返回dataset,十分简单实用,有需要的小伙伴可以参考下。...2020-06-25
- 这篇文章主要介绍了JAVA调用SAP WEBSERVICE服务实现流程图解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-10-11
- c#编写webservice服务引用实例分享,大家参考使用吧...2020-06-25
- 首先用c#创建一个web service,主要是利用其WSDL的功能,当然也可以利用php创建一个,道理都是一样的...2020-06-25
- C#调用和实现WebService,纯手工打造! 需要的朋友可以参考一下...2020-06-25
系统找不到指定的文件 No installed service named "Apache2"
我们有时会发现apache会出现,系统找不到指定的文件 No installed service named "Apache2" 安装Apache问题:系统找不到指定的文件 No installed service named "Apa...2016-09-14- 本文主要用详细的图文给大家介绍C#创建、部署、调用WebService的全部过程以及中间需要避免的问题。...2020-06-25
- 在php程序中有事会需要用到C代码,这篇文章着重介绍一下用C写php扩展的方法,而且不需要重新编译php。有需要的同学可以参考一下。 在php程序中需要用到C代码,应该是下...2017-07-06
- webservice的 发布一般都是使用WSDL(web service descriptive language)文件的样式来发布的,在WSDL文件里面,包含这个webservice暴露在外面可供使用的接口。今天我们来详细讨论下如何动态调用以及读取解析返回结果...2020-06-25