Search code examples
phplaravelmagentomagento-soap-api

display error of magento api soapclient


im working with laravel 4 framework and magento api soap. this is my login method:

    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 );
        }

}

now im getting on laravel screen SoapFault Access denied.

i need to return error string if url is incorrect or API user/key is incorrect.

like this:

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

how to do this? there is fault code but i dont know how to log that http://www.magentocommerce.com/api/soap/introduction.html#Introduction-GlobalAPIFaults


Solution

  • A SoapFault is an Exception that needs to be caught. The fault code and error string can be accessed via the Exception. Also, make sure the SoapClient is instantiated with the 'exceptions' option set to true, otherwise I believe PHP just throws a fatal error.

    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 );
    }