Magento – How to add parameter with SOAP request

apimagento-1.9php-5.2.x.xsoap

I'm new to Magento I try to call category level using Magento SOAP API with parent category id. I use following code.

<?php
$proxy = new SoapClient('http://domain/index.php/api/soap/?wsdl');
$session = $proxy->login('user', 'password');
$result = $proxy->call($session,'catalog_category.level');
echo json_encode($result);
?>

Here for the above code, "Default Category" details are coming. now I try to call some other category by using following codes.

$result = $proxy->call($session,'catalog_category.level',12);

That is not working

$arguments = array( 'parentCategory' => 12);
$result = $proxy->call($session,'catalog_category.level',$arguments);

This is also not working.

Then I try to call category tree using following code.

 <?php
$proxy = new SoapClient('http://domain/index.php/api/soap/?wsdl');
$session = $proxy->login('user', 'password');
$result = $proxy->call($session,'catalog_category.tree');
echo json_encode($result);
?>

it showing all the category tree. it means API is working but whenever I try to pass an argument it showing server not found an error.

Can any one please tell me how to pass arguments with the request.

Best Answer

<?php
/* code to check SOAP enabled in your server or not */
echo (extension_loaded('soap'))?'SOAP Enabled':'SOAP Disabled';echo '<br />';
echo (class_exists('SOAPClient'))?'SOAP Client Present':'SOAP Client Absent';echo '<br />';
echo (class_exists('SOAPServer'))?'SOAP Server Present':'SOAP Server Absent';echo '<br />';



/** Code to authenticate SOAP credentials and authorize the request URL **/
$apiUrl = 'http://localhost/magento/index.php/api/soap/?wsdl';
$apiUser = 'yourSoapApiUser'; 
$apiKey = 'yourSoapApiKey';

ini_set("soap.wsdl_cache_enabled", "0");
try{
    $client = new SoapClient($apiUrl, array('cache_wsdl' => WSDL_CACHE_NONE));
} catch (SoapFault  $e) {
    echo 'Error in Soap Connection : '.$e->getMessage();
}
try {
    $session = $client->login($apiUser, $apiKey);
    if($session){
        $params = array('storeId'=>$storeId,'categoryId'=>$categoryId);
        try {
            $result = $client->call($session, 'catalog_category.tree',$params);
            echo json_encode($result);
        } catch(Exception $e) {
            $result = json_encode(array('isFault' => 1, 'faultMessage'=>$e->getMessage()));
            $error = true;
        }
    }else echo 'SOAP Connection Failed.';
} catch (SoapFault  $e) {
    echo 'Wrong Soap credentials : '.$e->getMessage();
}

The above code worked fine for me. If it won't for you then check your corresponding Api.php method.

Gist : Multiple request params are always sent in an array.