Magento – Magento API : Call to a member function getRole() on a non-object

apimagento-1.7product-list

I am searching products using magento api. created one input box and search button where user will enter the product name and submit the keyword. It will search the products using the key word and name attribute. But it is showing the error:

Call to a member function getRole() on a non-object

I have checked user which i created, user role and user role assigned to user and role resources but still issue the same.. here is my code

  <form action="" method="get"> <input type="text" name="searchWord" /> <input type="submit" value="search" name="search" /> </form> <?php if(isset($_GET['search'])){

    // creating soap client $client = new SoapClient('http://localhost/magento/index.php/api/soap/?wsdl');

    // creating session for login 

    $sessionId = $client->login('myusername','apiKey');

    $searchKeyWord = $_GET['searchWord'];

    $filters = array(
                    'name' => array('like'=>"%$searchKeyWord%")
                     ); 
var_dump($filters);
 try{ $result = $client->call($sessionId, 'catalog_product.list', array($filters)); } catch(Exception $e){ echo $e->getMessage();
        return;

    }

I have tried other method too.. like

$result = $client->call($sessionId, 'catalog_product.info', '1') and its working fine but catalog_product.list is not working.. Any idea

Thanks in advance 🙂

Best Answer

I think the cause is, that you never set the variable $client in this code snippet. It´s commented out. I guess you have initialized the variable somewhere else before this snippet in your code. That´s why it works sometimes.

You should change the line

 // creating soap client $client = new SoapClient('http://localhost/magento/index.php/api/soap/?wsdl');

to the following (two lines!!!):

 // creating soap client
$client = new SoapClient('http://localhost/magento/index.php/api/soap/?wsdl');