CI集成nusoap

因为要用php访问一些 Web service的接口
需要用到nusoap
关于nusoap的使用
参见 http://blog.csdn.net/keyunq/archive/2006/12/06/1431725.aspx
而网站用了ci的架构 所以得想办法集成进来
网上搜了下
找到如下解决方法

1.首先下载nusoap 最新版貌似是0.7.2
将lib目录里的文件放到 ci的libraries目录 如: ci/application/libraries/nusoap-0.7.3


2.编写Nusoap_lib.php 自己的类库文件 放在ci/application/libraries/Nusoap_lib.php

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Nusoap_lib
{
   function __construct()
   {
       require_once('nusoap-0.7.3/nusoap.php');
   }
}
?>

3.然后就可以用$this->load->library("Nusoap_lib") 来调用了

如下
<?php
class Client extends Controller
{
    function __construct()
    {
        parent::Controller();
        $this->load->library("Nusoap_lib");
    }

    function index()
    {
        $this->nusoap_client = new nusoap_client("http://xxxxxxxx/xxxxxxxx.asmx?wsdl","wsdl");
        if($this->nusoap_client->fault)
        {
            $str = 'Error: '.$this->nusoap_client->fault;
        }
        else
        {
            $parameters = array('param1'=>param1,'param2'=>param2,'param3'=>$param3,'param4'=>$param4);
            if ($this->nusoap_client->getError())
            {
                $str = 'Error: '.$this->nusoap_client->getError();
            }
            else
            {
                $arr_result = $this->nusoap_client->call('CheckUserName',$parameters);
                $str = $arr_result['CheckUserNameResult'];
            }
        }
        echo $str;   
    }
}
?>

整个过程和集成smarty貌似差不多

以上只是调用webservice 的代码,编写webservice的代码,可以参考下面的网址:
http://codeigniter.com/forums/viewthread/59710/


//add by Q 2008.12.1

NuSOAP 调用 Web Service 出现乱码的原因:


通常我们进行  Web Service 开发时都是用的 UTF-8 编码,这时我们需要设置 :
Code:

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


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

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


集成到ci里 就是
$this->nusoap_client->soap_defencoding = 'utf-8';
$this->nusoap_client->xml_encoding = 'utf-8';

到现在,应该是一切正常了才对,但是我们在输出结果的时候,却发现,返回的是 乱码


NuSOAP 调用 Web Service 出现乱码的解决方法:


实际上,开启了调试功能的朋友,相信会发现 $client->response 返回的是正确的结果,为什 么 $result = $client->call($action, array('parameters' =& gt; $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';
集成到ci里 就是 $this->nusoap_client->soap_defencoding = 'utf-8'; $this->nusoap_client->decode_utf8 = false; $this->nusoap_client->xml_encoding = 'utf-8';

相关文章

1.使用ajax调用varxhr;functioninvoke(){if(window.ActiveXO...
               好不容易把WebService服务器...
1新建一个工程项目用来做服务端增加一个MyService1类文件pac...
packagecom.transsion.util;importjava.io.BufferedReader;i...
再生产wsdl文件时重写描述文件1usingSystem;2usingSystem.Co...
一般情况下,使用eclipse自带的jax-ws生成webservice会自动生...