【问题标题】:display error of magento api soapclientmagento api soapclient的显示错误
【发布时间】:2015-03-30 15:47:57
【问题描述】:

我正在使用 laravel 4 框架和 magento api soap。这是我的登录方法:

    public function APIauthentication( $apiUser, $apiKey ) {

        $error = array();

        if( empty( $apiUser ) ) {
                $error[] = 'Unknown api user';
        }

        if( empty( $apiKey ) ) {
                $error[] = 'Invalid api key';
        }

        if( empty( $error ) ) {

                $client = $this->_getClient();
                $token = $client->login( $apiUser, $apiKey );
                $this->_setToken( $token );

                return $this->_apiJsonResult( $token );
        } else {
                return $this->_apiJsonResult( $error );
        }

}

现在我进入 laravel 屏幕 SoapFault 访问被拒绝。

如果 url 不正确或 API 用户/密钥不正确,我需要返回错误字符串。

像这样:

return Redirect::to('user/stores/magento/')->with('status', 'apie user or key is incorrect');

如何做到这一点?有故障代码,但我不知道如何记录 http://www.magentocommerce.com/api/soap/introduction.html#Introduction-GlobalAPIFaults

【问题讨论】:

    标签: php api magento laravel soap


    【解决方案1】:

    SoapFault 是需要捕获的异常。可以通过 Exception 访问故障代码和错误字符串。另外,请确保实例化 SoapClient 时将 'exceptions' 选项设置为 true,否则我相信 PHP 只会引发致命错误。

    if( empty( $error ) ) {
        $client = $this->_getClient();
        try {
            $token = $client->login( $apiUser, $apiKey );
        } catch (SoapFault $e) {
            // login failed logic
            $faultcode = $e->faultcode; // ex: 2
            $message = $e->faultstring; // ex: Access denied.
            // return redirect, etc...
        }
        // login successful logic
        $this->_setToken( $token );
    
        return $this->_apiJsonResult( $token );
    } else {
        return $this->_apiJsonResult( $error );
    }
    

    【讨论】:

    • 它的工作,但比我在 php 文件上测试。当我在 laravel 中测试时出现这样的错误prntscr.com/6nlnzc
    • @HaroldasKerušauskas 该文件是否已命名空间?尝试将catch (SoapFault $e) 更改为catch (\SoapFault $e)
    • 谢谢你的作品 :) 如果 url 不正确,也许你能帮助如何返回错误。有我的方法:public function __construct( $wsdl ) { $client = new parent( $wsdl ); $this->_setClient( $client ); }
    • error is Fatal error: Uncaught SoapFault exception: [WSDL] SOAP-ERROR: Parsing WSDL: Couldn't load from 'localhost:8888/magentof/api/soap/?wsdl' : failed to load external entity "localhost:8888/magentof/api/soap/?wsdl" in / Applications/MAMP/htdocs/magento/soap.php:31 堆栈跟踪:#0 /Applications/MAMP/htdocs/magento/soap.php(31): SoapClient->SoapClient('localhos...') #1 /Applications/MAMP /htdocs/magento/soap.php(253): Client->__construct('localhos...') #2 {main} 在第 31 行的 /Applications/MAMP/htdocs/magento/soap.php 中抛出
    • @HaroldasKerušauskas 因为这个异常是从构造函数中抛出的,所以无论你在哪里实例化这个对象,你都需要处理它。
    猜你喜欢
    • 1970-01-01
    • 2014-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多