用nusoap在Cakephp中访问Webservice
[ 2008-05-16 20:49 | by 草山湖 ]
1. 下载 nusoap 库,点这里下载
下载文件 (已下载 208 次)
2. 解压到 /app/vendors/nusoap ,目录结构类似:
/app/vendors/nusoap/lib
/app/vendors/nusoap/samples
3. 封装为一个component,保存到 /app/controllers/components/soap.php,以方便在controller里调用 :
<?php
class SoapComponent extends Component
{
var $defaultClientOptions = array(
'endpoint' => '',
'wsdl' => false,
'proxyhost' => false,
'proxyport' => false,
'proxyusername' => false,
'proxypassword' => false,
'timeout' => 0,
'response_timeout' => 20,
);
var $defaultCallOptions = array(
'serviceUrl' => '',
'operation' => '',
'namespace' => 'http://tempuri.org',
'soapAction' => '',
'params' => array(),
'debug' => false, // set to true to return debug info
'encoding' => 'UTF-8',
'rpcParams' => null,
'style' => 'rpc',
'headers' => false,
'use' => 'encoded',
'autoCheckWSDL' => true, // if serviceUrl end with "?WSDL" then set the client options['wsdl'] to be true
);
function initialize(&$controller)
{
vendor('nusoap/lib/nusoap');
}
/**
* @author RainChen @ Wed Mar 12 17:55:37 CST 2008
* @uses return a soap client object
* @access public
* @param mix $options (see $this->defaultClientOptions)
* @return object
* @version 0.1
*/
function client($options)
{
$defaultClientOptions = $this->defaultClientOptions;
if(!is_array($options))
{
$options = array('endpoint' => $options);
}
$options = am($this->defaultClientOptions, $options);
return (new nusoap_client(
$options['endpoint']
, $options['wsdl']
, $options['proxyhost']
, $options['proxyport']
, $options['proxyusername']
, $options['proxypassword']
, $options['timeout']
, $options['response_timeout']
));
}
/**
* @author RainChen @ Wed Mar 12 17:55:16 CST 2008
* @uses quick call
* @access public
* @param array $options (see $this->defaultCallOptions)
* @return array
* @version 0.1
*/
function call($options)
{
$defaultOptions = $this->defaultCallOptions;
$options = am($defaultOptions, $options);
if($options['soapAction'] == '')
{
$options['soapAction'] = $options['namespace']. '/'. $options['operation'];
}
$clientOptions = $this->defaultClientOptions;
$clientOptions['endpoint'] = $options['serviceUrl'];
if($options['autoCheckWSDL'])
{
if(strpos($options['serviceUrl'], '?WSDL') > 0)
{
$clientOptions['wsdl'] = true;
}
}
$client = $this->client($clientOptions);
$client->soap_defencoding = $options['encoding'];
$client->decode_utf8 = false;
$result = $client->call($options['operation'], $options['params'], $options['namespace'], $options['soapAction'], $options['headers'], $options['rpcParams'], $options['style'], $options['use']);
$return = array(
'fault' => null,
'error' => null,
'result' => null,
);
if($client->fault)
{
$return['fault'] = $result;
}
else
{
$err = $client->getError();
if ($err)
{
$return['error'] = $err;
}
else
{
$return['result'] = $result;
}
}
if($options['debug'])
{
$return['request'] = $client->request;
$return['response'] = $client->response;
$return['debug'] = $client->getDebug();
}
return $return;
}
/**
* @author RainChen @ Wed Mar 12 17:57:29 CST 2008
* @uses quick debug for $this->call() result;
* @access public
* @param array $result
* @return void
* @version 0.1
*/
function debug($result)
{
if ($result['fault']) {
echo '<h2>Fault (Expect - The request contains an invalid SOAP body)</h2><pre>'; print_r($result['fault']); echo '</pre>';
} else {
if ($result['error']) {
echo '<h2>Error</h2><pre>' . $result['error'] . '</pre>';
} else {
echo '<h2>Result</h2><pre>'; print_r($result['result']); echo '</pre>';
}
}
echo '<h2>Request</h2><pre>' . htmlspecialchars($result['request'], ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2><pre>' . htmlspecialchars($result['response'], ENT_QUOTES) . '</pre>';
echo '<h2>Debug</h2><pre>' . htmlspecialchars($result['debug'], ENT_QUOTES) . '</pre>';
}
}
?>
4. 在任意controller里添加测试:
class TestController extends AppController
{
var $components = array(''Soap');
function test()
{
$soapRequest = array(
'serviceUrl' => 'http://webService.asmx?WSDL',//这里一定要是大写字母WSDL
'operation' => 'getUser',
'encoding' => 'UTF-8',
'debug' => true, // set to true to return debug info
'params' => array(
'username' => 'abc',
),
);
$result = $this->Soap->call($soapRequest);
isset($soapRequest['debug']) && $soapRequest['debug'] && $this->Soap->debug($result);
}
}
备注:
1. 在构建服务端时,serviceUrl 须加 ?WSDL 后缀
2. 返回结果在:$soapRequest['result']
3.如果$soapRequest 中有debug选项,$result会包含调试信息,可通过$this->Soap->debug($result) 快速调试
下载文件 (已下载 208 次)2. 解压到 /app/vendors/nusoap ,目录结构类似:
/app/vendors/nusoap/lib
/app/vendors/nusoap/samples
3. 封装为一个component,保存到 /app/controllers/components/soap.php,以方便在controller里调用 :
<?php
class SoapComponent extends Component
{
var $defaultClientOptions = array(
'endpoint' => '',
'wsdl' => false,
'proxyhost' => false,
'proxyport' => false,
'proxyusername' => false,
'proxypassword' => false,
'timeout' => 0,
'response_timeout' => 20,
);
var $defaultCallOptions = array(
'serviceUrl' => '',
'operation' => '',
'namespace' => 'http://tempuri.org',
'soapAction' => '',
'params' => array(),
'debug' => false, // set to true to return debug info
'encoding' => 'UTF-8',
'rpcParams' => null,
'style' => 'rpc',
'headers' => false,
'use' => 'encoded',
'autoCheckWSDL' => true, // if serviceUrl end with "?WSDL" then set the client options['wsdl'] to be true
);
function initialize(&$controller)
{
vendor('nusoap/lib/nusoap');
}
/**
* @author RainChen @ Wed Mar 12 17:55:37 CST 2008
* @uses return a soap client object
* @access public
* @param mix $options (see $this->defaultClientOptions)
* @return object
* @version 0.1
*/
function client($options)
{
$defaultClientOptions = $this->defaultClientOptions;
if(!is_array($options))
{
$options = array('endpoint' => $options);
}
$options = am($this->defaultClientOptions, $options);
return (new nusoap_client(
$options['endpoint']
, $options['wsdl']
, $options['proxyhost']
, $options['proxyport']
, $options['proxyusername']
, $options['proxypassword']
, $options['timeout']
, $options['response_timeout']
));
}
/**
* @author RainChen @ Wed Mar 12 17:55:16 CST 2008
* @uses quick call
* @access public
* @param array $options (see $this->defaultCallOptions)
* @return array
* @version 0.1
*/
function call($options)
{
$defaultOptions = $this->defaultCallOptions;
$options = am($defaultOptions, $options);
if($options['soapAction'] == '')
{
$options['soapAction'] = $options['namespace']. '/'. $options['operation'];
}
$clientOptions = $this->defaultClientOptions;
$clientOptions['endpoint'] = $options['serviceUrl'];
if($options['autoCheckWSDL'])
{
if(strpos($options['serviceUrl'], '?WSDL') > 0)
{
$clientOptions['wsdl'] = true;
}
}
$client = $this->client($clientOptions);
$client->soap_defencoding = $options['encoding'];
$client->decode_utf8 = false;
$result = $client->call($options['operation'], $options['params'], $options['namespace'], $options['soapAction'], $options['headers'], $options['rpcParams'], $options['style'], $options['use']);
$return = array(
'fault' => null,
'error' => null,
'result' => null,
);
if($client->fault)
{
$return['fault'] = $result;
}
else
{
$err = $client->getError();
if ($err)
{
$return['error'] = $err;
}
else
{
$return['result'] = $result;
}
}
if($options['debug'])
{
$return['request'] = $client->request;
$return['response'] = $client->response;
$return['debug'] = $client->getDebug();
}
return $return;
}
/**
* @author RainChen @ Wed Mar 12 17:57:29 CST 2008
* @uses quick debug for $this->call() result;
* @access public
* @param array $result
* @return void
* @version 0.1
*/
function debug($result)
{
if ($result['fault']) {
echo '<h2>Fault (Expect - The request contains an invalid SOAP body)</h2><pre>'; print_r($result['fault']); echo '</pre>';
} else {
if ($result['error']) {
echo '<h2>Error</h2><pre>' . $result['error'] . '</pre>';
} else {
echo '<h2>Result</h2><pre>'; print_r($result['result']); echo '</pre>';
}
}
echo '<h2>Request</h2><pre>' . htmlspecialchars($result['request'], ENT_QUOTES) . '</pre>';
echo '<h2>Response</h2><pre>' . htmlspecialchars($result['response'], ENT_QUOTES) . '</pre>';
echo '<h2>Debug</h2><pre>' . htmlspecialchars($result['debug'], ENT_QUOTES) . '</pre>';
}
}
?>
4. 在任意controller里添加测试:
class TestController extends AppController
{
var $components = array(''Soap');
function test()
{
$soapRequest = array(
'serviceUrl' => 'http://webService.asmx?WSDL',//这里一定要是大写字母WSDL
'operation' => 'getUser',
'encoding' => 'UTF-8',
'debug' => true, // set to true to return debug info
'params' => array(
'username' => 'abc',
),
);
$result = $this->Soap->call($soapRequest);
isset($soapRequest['debug']) && $soapRequest['debug'] && $this->Soap->debug($result);
}
}
备注:
1. 在构建服务端时,serviceUrl 须加 ?WSDL 后缀
2. 返回结果在:$soapRequest['result']
3.如果$soapRequest 中有debug选项,$result会包含调试信息,可通过$this->Soap->debug($result) 快速调试
fred
2008-05-27 16:38
强人
admin 回复于 2008-05-27 21:23
欢迎访问草山狐随笔www.xcopy.net.cn
分页: 1/1
1
1

PHP调用Java webservic 实现方法,实用session 共享
php与Webservices--NuSOAP的使用说明(转载)




