【问题标题】:yii - how to create a completely independant extension using nusoap 0.9.5 in yii 1.1.13yii - 如何在 yii 1.1.13 中使用 nusoap 0.9.5 创建一个完全独立的扩展
【发布时间】:2013-07-02 04:22:12
【问题描述】:

感谢@JPR 解决,感谢@PeterM

/* The only dependance is when */ class NuSoap extends CApplicationComponent

v-下面,初始问题-v


我想知道如何在 yii 1.1.13 中使用nusoap 0.9.5 创建一个基本扩展? 我的简单代码如下所示:

<?php 
require("libs/nusoap-0.9.5/lib/nusoap.php");
// namespace
$ns = "https://my-namespace-site";

// client
$client = new soapclient('https://ip-to-webservice-server');

// header
$headers = "<credentials><ns1:username xmlns:ns1=\"$ns\">username</ns1:username>
<ns2:password xmlns:ns2=\"$ns\">password</ns2:password></credentials>";
$client->setHeaders($headers);

// searching 
$params = array(
    'local_user_array' => array(
        'limit' => 10
    )
);
$result = $client->call('local_users_search', $params, $ns );
if( $client->getError() ) {
    echo $client->getError();
}
else {
        foreach( $result['data'] as $offer ) {
            echo "<div>".$offer['firstname']."</div>";
        }
    }
?>

我的代码运行良好。现在,我该怎么做才能在 yii 中使用 $result 才能在视图中显示结果?

最好的答案是一个带有文件结构和代码以及有意义的解释的具体示例。

任何帮助将不胜感激。提前感谢您的帮助。我很期待;-)

PS:请不要引用任何指向 yiiframework 网站的链接,因为它没有多大帮助,因为我也知道如何搜索。

【问题讨论】:

    标签: php yii yii-extensions yii-components yii-modules


    【解决方案1】:

    创建一个从 CApplicationComponent 扩展的类。

    class NuSoap extends CApplicationComponent 
    {
    
        protected $params = array();
        protected $client, $ns;
    
        public function init() {
            require("libs/nusoap-0.9.5/lib/nusoap.php");
            $this->client = new soapclient('https://ip-to-webservice-server');
            $this->ns = "https://my-namespace-site";
        }
    
        public function getResults() {
            $results = $this->client->call(
                'local_users_search', 
                $this->params, 
                $this->ns 
            );
            return $results;
        }
    
        public function setParams(array $params) {
            $this->params = $params;
        } 
    
        // whatever other methods you need for it to work
    }
    

    然后在你的主配置文件中,在组件数组下:

     array(
        'nuSoap' => array(
            'class' => 'application.components.NuSoap' // name your class NuSoap.php
        )
        ......
    )
    

    确保在 main.php 配置文件中也导入了 application/components 或 application/extensions 目录。将你的类文件放在 application/components 或 applcation/extensions 目录下的 NuSoap.php 中。

    您现在可以在 Yii 应用中的任何位置引用您的组件:

    Yii::app()->nuSoap->setParams($params);
    $results = Yii::app()->nuSoap->getResults();
    

    这应该足以让您朝着正确的方向开始。 Yii 文档对于理解应用程序组件是如何工作的非常有帮助,但是由于你不想阅读它,你只需要猜测一些事情。如果你想使用 Yii,避免阅读文档是绝对没有意义的。

    【讨论】:

    • 一些建议:为了使其更便携,使用前缀 - ENuSoap - 正如 Yii 开发人员推荐的那样。使 clientns 在 config.xml 中可配置。您也可以使用 ovveride __call 来调用 ws:Yii::app()-&gt;nuSoap-&gt;local_users_search($params);。让clientns 可配置,您可以在配置中添加新的ws,通过定义一个新组件myWs =&gt; array('class' =&gt; 'ENuSoap', 'ns' =&gt; 'foo', 'client' =&gt; 'http://localhost'),然后调用它Yii::app()-&gt;myWs-&gt;some_service($withParams)
    • 非常感谢@JPR,非常有用的现场信息比 Yii 文档帮助我更好/更快,这是 stackoverflow 存在的原因之一 XD
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    相关资源
    最近更新 更多