1

I have created the custom magento SOAP/XML-RPC API successfully to fallow the Marius and Sylvain instructions. And my php file invoking the those two methods which is create and info.

my v1 code is:

    <?php
class Course_Mca_Model_Mca_Api extends Mage_Api_Model_Resource_Abstract
{

    /**
     * init mca students
     * @access protected
     * @param $mcaId
     * @return Course_Mca_Model_Mca
     */
    protected function _initMca($mcaId){
        $mca = Mage::getModel('mca/mca')->load($mcaId);
        if (!$mca->getId()) {
            $this->_fault('mca_not_exists');
        }
        return $mca;
    }

    /**
     * Add Students
     * @access public
     * @param array $data
     * @return array
     */
    public function create($data){
        try {
            if (is_null($data)){
                throw new Exception(Mage::helper('mca')->__("Data cannot be null"));
            }
            $mca = Mage::getModel('mca/mca')->setData((array)$data)->save();
        }
        catch (Mage_Core_Exception $e) {
            $this->_fault('data_invalid', $e->getMessage());
        }
        return $mca->getId();
    }

    /**
     * get info
     * @access public
     * @param int $mcaId
     * @return array
     */
    public function info($mcaId){
        $result = array();
        $mca = $this->_initMca($mcaId);
        $result = $mca->getData();
        return $result;
    }

    /**
     * get students
     * @access public
     * @param mixed $filters
     * @return array
     */
    public function items($filters = null){
        $collection = Mage::getModel('mca/mca')->getCollection();
        $apiHelper = Mage::helper('api');
        $filters = $apiHelper->parseFilters($filters);
        try {
            foreach ($filters as $field => $value) {
                $collection->addFieldToFilter($field, $value);
            }
        }
        catch (Mage_Core_Exception $e) {
            $this->_fault('filters_invalid', $e->getMessage());
        }
        $result = array();
        foreach ($collection as $mca) {
            $result[] = $mca->getData();
        }
        return $result;
    }

    /**
     * Change existing student information
     * @access public
     * @param int $mcaId
     * @param array $data
     * @return bool
     */
    public function update($mcaId, $data){
        $project = $this->_initMca($mcaId);
        try {
            $project->addData((array)$data);
            $project->save();
        }
        catch (Mage_Core_Exception $e) {
            $this->_fault('save_error', $e->getMessage());
        }

        return true;
    }

    /**
     * remove student
     * @access public
     * @param int $mcaId
     * @return bool
     */
    public function remove($mcaId){
        $mca = $this->_initMca($mcaId);
        try {
            $mca->delete();
        }
        catch (Mage_Core_Exception $e) {
            $this->_fault('remove_error', $e->getMessage());
        }
        return true;
    }

}

v2 code is:

     <?php
class Course_Mca_Model_Mca_Api_V2 extends Course_Mca_Model_Mca_Api
{

    /**
     * Add Students
     * @access public
     * @param array $data
     * @return array
     */
    public function create($data){
        try {
            if (is_null($data)){
                throw new Exception(Mage::helper('mca')->__("Data cannot be null"));
            }
            $mca = parent::create($data);
        }
        catch (Mage_Core_Exception $e) {
            $this->_fault('data_invalid', $e->getMessage());
        }
        return $mca->getId();
    }

    /**
     * Student info
     * @access public
     * @param int $mcaId
     * @return object
     */
    public function info($mcaId){
        try
        {
            if (is_null($mcaId)){
                throw new Exception(Mage::helper('mca')->__("Data cannot be null"));
            }
            $result = parent::info($mcaId);
            $result = Mage::helper('api')->wsiArrayPacker($result);
        }
        catch (Mage_Core_Exception $e) {
            $this->_fault('data_invalid', $e->getMessage());
        }
        return $result;
    }

    /**
     * Change existing student information
     * @access public
     * @param int $mcaId
     * @param array $data
     * @return bool
     */
    public function update($mcaId, $data){
        try {
            if (is_null($mcaId)){
                throw new Exception(Mage::helper('mca')->__("Data cannot be null"));
            }
            $mca = parent::update($mcaId);
        }
        catch (Mage_Core_Exception $e) {
            $this->_fault('save_error', $e->getMessage());
        }
        return true;
    }
    /**
     * get students
     * @access public
     * @param mixed $filters
     * @return array
     */
    public function items($filters = null){
        try {
            $collection =parent::items($filters);
        }
        catch (Mage_Core_Exception $e) {
            $this->_fault('filters_invalid', $e->getMessage());
        }
        return $collection;
    }

    /**
     * remove student
     * @access public
     * @param int $mcaId
     * @return bool
     */
    public function remove($mcaId){
        try {
            $mca = parent::remove($mcaId);
        }
        catch (Mage_Core_Exception $e) {
            $this->_fault('remove_error', $e->getMessage());
        }
        return true;
    }

}

When I accessing info method v1: & v2: is returning result.

my code is:

<?php 
$proxy = new SoapClient('http://localhost/ics/index.php/api/soap?wsdl'); //edit the address and put the url to your magento here
//$proxy = new SoapClient('http://localhost/ics/index.php/api/?wsdl');
try
{

//$proxy = new SoapClient('http://localhost/ics/index.php/api/v2_soap?wsdl');
$sessionId = $proxy->login("magento","magento@123"); // put in the info for your user here
echo "Login ID : $sessionId";

echo '<br/>';

//v1
$result = $proxy->call($sessionId, 'mca.info', array('1'));

//v2
//$result = $proxy->mcaMcaInfo($sessionId, '1');
//$result = $proxy->mcaMcaInfo($sessionId, '1');

print_r($result); 


$proxy->endSession($sessionId);
} catch (Exception $e) {
    echo '<h1>Error</h1>';
    echo '<p>' . $e->getMessage() . '</p>';
}

enter image description here

But when I accessing my create method in v1: returning inserted record id, but values are null.

my code is:

<?php 
$proxy = new SoapClient('http://localhost/ics/index.php/api/soap?wsdl'); //edit the address and put the url to your magento here
//$proxy = new SoapClient('http://localhost/ics/index.php/api/?wsdl');
try
{

//$proxy = new SoapClient('http://localhost/ics/index.php/api/v2_soap?wsdl');
$sessionId = $proxy->login("magento","magento@123"); // put in the info for your user here
echo "Login ID : $sessionId";

echo '<br/>';

//v1
$result = $proxy->call($sessionId, 'mca.create', array('php','dotnet','php_dotnet'));
//$result = $proxy->call($sessionId, 'mca.create', 'php','dotnet','php_dotnet');



print_r($result); 


$proxy->endSession($sessionId);
} catch (Exception $e) {
    echo '<h1>Error</h1>';
    echo '<p>' . $e->getMessage() . '</p>';
}

enter image description here enter image description here

Finally when I accessing my create method in v2: returning error Call to a member function getId() on a non-object.

my code is:

<?php 

try
{

$proxy = new SoapClient('http://localhost/ics/index.php/api/v2_soap?wsdl');
$sessionId = $proxy->login("magento","magento@123"); // put in the info for your user here
echo "Login ID : $sessionId";

echo '<br/>';



//v2
$result = $proxy->mcaMcacreate($sessionId, 'php','dotnet','php_dotnet');
//$result = $proxy->mcaMcacreate($sessionId, 'php','dotnet','php_dotnet');

print_r($result); 


$proxy->endSession($sessionId);
} catch (Exception $e) {
    echo '<h1>Error</h1>';
    echo '<p>' . $e->getMessage() . '</p>';
}

enter image description here

Can any one tell me where I went wrong?

And also take look into my create,update,remove,info v1 & v2 methods are correct or not? tell me how to pass filters?

thanks in advance.

6
  • 1
    See how the $data variable looks like before doing the save. before $mca = Mage::getModel('mca/mca')->setData((array)$data)->save(); put this line: Mage::log((array)$data); then check the file var/log/system.log. If that doesn't ring a bell post it in the question.
    – Marius
    Commented Oct 18, 2013 at 7:41
  • @Marius, it's very use full when I sending data. thank you Commented Oct 18, 2013 at 9:00
  • @Marius, I fallow the your instruction v1 is inserting, but v2: is returning error can you tell me where I went wrong? magento.stackexchange.com/questions/9638/… Commented Oct 18, 2013 at 9:49
  • Use again Mage::log() to log the data that is received by the api method. and see what's wrong.
    – Marius
    Commented Oct 18, 2013 at 11:26
  • @Marius, I fallow the your instruction to solved and both are working fine except v1: update method can you see my thread? magento.stackexchange.com/questions/9643/invalid-api-path Commented Oct 18, 2013 at 11:29

0

Browse other questions tagged or ask your own question.