【问题标题】:Loading model function inside a webservice function在 web 服务函数中加载模型函数
【发布时间】:2014-05-23 03:35:03
【问题描述】:

我在一个基于 codeigniter 的项目中工作,该项目集成了 SOAP Web 服务,但我无法在已注册的 Web 服务函数中加载模型函数。

我在 SOAP webservice 中有这 2 个函数:hello 和 addcontact。

function hello($name) {
            return 'Hello, ' . $name;
    }

function addcontact($nombre, $apellido, $ciudad) {            
        $resultado=$this->modelo_turismo->addcontact($nombre, $apellido, $ciudad);

        if($resultado){
            return "Bienvenido $nombre $apellido. Tu eres de $ciudad.";
        }else{
            return "No se pudo agregar contacto.";
        }            
    }

函数 hello 很简单,并且在客户端使用服务时工作正常,不像函数 addcontact 在尝试使用时显示此消息:

Response not of type text/xml: text/html

如您所见,我在模型中加载了一个函数,该函数将联系人插入数据库,但我什至没有返回任何数据库数据来回显或打印。

我还尝试了其他一些尝试加载模型的方法,但我无法摆脱该消息,所以我尝试了这个(我知道在 CodeIgniter 中使用这样的函数插入很奇怪,但我正在努力学习为什么该消息会出现):

function addcontact($nombre, $apellido, $ciudad) {
        $conexion = new mysql ("localhost","root","","turismo");
        if ($conexion->connect_errno){
            return "Failed to connect to MySQL: " . $conexion->connect_error;
        }
        $query = "INSERT INTO contactos (nombre, apellido, ciudad) VALUES ('$nombre', '$apellido', '$ciudad')";
        $resultado = $conexion->query($query);
        if($resultado){
            return "Bienvenido $nombre $apellido. Tu eres de $ciudad.";
        }else{
            return "No se pudo agregar contacto.";
        }

    }

使用该功能我再次收到此错误:

响应不是 text/xml 类型的:text/html

但是如果我像这样将连接行中的“mysql”更改为“mysqli”:

$conexion = new mysqli ("localhost","root","","turismo");

我在加载客户端时得到了预期的结果:

比恩维尼多约翰特拉沃尔塔。加利福尼亚州。

然后我怀疑我加载模型的错误是因为在我的数据库配置文件中我有这行:

$db['default']['dbdriver'] = 'mysql';

所以我尝试将驱动程序更改为“mysqli”,但没有好的结果。我不断收到同样的错误:

响应不是 text/xml 类型的:text/html

顺便说一句,这是我注册“addcontact”功能的方式:

$this->nusoap_server->register('addcontact',                // method name
        array('nombre' => 'xsd:string',
            'apellido' => 'xsd:string',
            'ciudad' => 'xsd:string'),        // input parameters
        array('return' => 'xsd:string'),      // output parameters
        'urn:Turismo_WSDL',                      // namespace
        'urn:Turismo_WSDL#addcontact',                // soapaction
        'rpc',                                // style
        'encoded',                            // use
        'Agregar reservacion'            // documentation
    );

这是客户端函数,它使用上面的函数:

function addcontact() {

    $wsdl = site_url('Webservice/wsdl');
    $client = new nusoap_client($wsdl, true);
    $client-> soap_defencoding='UTF-8'; 
    $client->decode_utf8 = true;

    $err = $client->getError();
    if ($err) {
        echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';

    }

    $result = $client->call('addcontact', array('nombre' => 'John', 'apellido'=>'Travolta', 'ciudad'=>'California'));

    // Check for a fault
    if ($client->fault) {
        echo '<h2>Fault</h2><pre>';
        print_r($result);
        echo '</pre>';
    } else {
        // Check for errors
        $err = $client->getError();
        if ($err) {
            // Display the error
            echo '<h2>Error</h2><pre>' . $err . '</pre>';
        } else {
            // Display the result
            echo '<h2>Result</h2><pre>';
            print_r($result);
        echo '</pre>';
        }
    }
}

所以我的问题是,我做错了什么?我可以使用如上所述的手动连接来完成这项工作,但我想像在 CodeIgniter 中一样使用模型。

【问题讨论】:

    标签: php web-services codeigniter soap model


    【解决方案1】:

    我们可以使用实例变量调用codeigniter模型

    $ci =&amp; get_instance();

    Class soap
    {
        private $ci;
    
        // set the CI classes to  $CI
        function __constuct(){
           $this->ci = & get_instance ();
        }
    
        function callone(){
          $resultado=$this->ci->modelo_turismo->addcontact($nombre, $apellido, $ciudad);
        }
    
    
    }
    

    ci-&gt; 将提供来自 CI 的模型

    有关 codeigniter 实例变量的更多信息,请参阅此处 Codeigniter: Get Instance

    【讨论】:

    • 我在 CI 控制器中配置了我的 nusoap_server,'class Webservice extends CI_Controller{' 我在 __construct() 函数中有这样的代码:'$this->nusoap_server = new nusoap_server();',我真的不知道为什么即使使用 get_instance() 也会收到该消息;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 1970-01-01
    • 2011-09-13
    • 2023-03-08
    • 1970-01-01
    • 2014-11-08
    相关资源
    最近更新 更多