Question

I am new to magento. I have created the custom Magento Api to follow the Marius instructions.

my code is:

app/code/local/Course/Mca/etc/api.xml - the api declaration file.

 <?xml version="1.0"?>
<config>
    <api>
        <resources>
            <mca_mca translate="title" module="mca">
                <title>Mca API</title>
                <model>mca/mca_api</model>
                <acl>mca/mca</acl><!-- acl resource alias -->
                <methods><!-- definne the methods -->

                    <create translate="title" module="mca"><!-- add Mca -->
                        <title>Add Mca</title>
                        <acl>mca/mca/create</acl>
                    </create>

                    <info translate="title" module="mca"><!-- project details -->
                        <title>Retrieve project info</title>
                        <acl>mca/mca/info</acl>
                    </info>

                </methods>
                <faults module="mca"><!-- errors that might appear-->
                    <mca_not_exists>
                        <code>101</code>
                        <message>Requested Mca does not exist.</message>
                    </mca_not_exists>
                    <invalid_data>
                        <code>102</code>
                        <message>Provided data is invalid.</message>
                    </invalid_data>
                    <save_error>
                        <code>103</code>
                        <message>Error while saving Mca. Details in error message.</message>
                    </save_error>
                    <remove_error>
                        <code>104</code>
                        <message>Error while removing Mca. Details in error message.</message>
                    </remove_error>
                </faults>
            </mca_mca>
        </resources>
        <resources_alias>
            <mca>mca_mca</mca>
        </resources_alias>
        <v2>
            <resources_function_prefix>
                <mca>mcaMca</mca>
            </resources_function_prefix>
        </v2>
        <acl><!-- acl definition -->
            <resources>
                <mca translate="title" module="mca">
                    <title>mca</title>
                    <mca translate="title" module="mca">
                        <title>Students</title>
                        <sort_order>110</sort_order>

                        <create translate="title" module="mca">
                            <title>Create</title>
                        </create>

                        <info translate="title" module="mca">
                            <title>Info</title>
                        </info>

                    </mca>
                </mca>
            </resources>
        </acl>
    </api>
</config>

app/code/local/Course/Mca/etc/wsdl.xml - wsdl part for V2

 <?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns:typens="urn:{{var wsdl.name}}" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
             xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/"
             name="{{var wsdl.name}}" targetNamespace="urn:{{var wsdl.name}}">
    <types>
        <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:Magento">
            <import namespace="http://schemas.xmlsoap.org/soap/encoding/" schemaLocation="http://schemas.xmlsoap.org/soap/encoding/" />

            <complexType name="mcaMcaCreateEntity"><!-- define type for add project -->
                <all>
                    <element name="firstname" type="xsd:string" minOccurs="1" />
                    <element name="lastname" type="xsd:string" minOccurs="0" />
                    <element name="telephone" type="xsd:string" minOccurs="0" />

                </all>
            </complexType>

            <complexType name="mcaMcaInfoEntity"><!-- define type for retrieve info -->
                <all>
                    <element name="mca_id" type="xsd:string" minOccurs="1" />
                    <element name="firstname" type="xsd:string" minOccurs="1" />
                    <element name="lastname" type="xsd:string" minOccurs="0" />
                    <element name="telephone" type="xsd:string" minOccurs="0" />

                    <element name="created_time" type="xsd:string" minOccurs="1" />
                    <element name="updated_time" type="xsd:string" minOccurs="1" />
                </all>
            </complexType>

        </schema>
    </types>
    <!--[+] define messages -->

    <message name="mcaMcaCreateRequest">
        <part name="sessionId" type="xsd:string" />
        <part name="data" type="xsd:string"  />
    </message>
    <message name="mcaMcaCreateResponse">
        <part name="result" type="xsd:string"/>
    </message>

    <message name="mcaMcaInfoRequest">
        <part name="sessionId" type="xsd:string" />
        <part name="projectId" type="xsd:string" />
    </message>
    <message name="mcaMcaInfoResponse">
        <part name="result" type="typens:mcaMcaInfoEntity" />
    </message>
    <!--[-] define messages -->
    <!--[+] define portTypes -->
    <portType name="{{var wsdl.handler}}PortType">
        <operation name="mcaMcaCreate">
            <documentation>Create the Mca</documentation>
            <input message="typens:mcaMcaCreateRequest" />
            <output message="typens:mcaMcaCreateResponse" />
        </operation>

        <operation name="mcaMcaInfo">
            <documentation>Retrieve MCA info</documentation>
            <input message="typens:mcaMcaInfoRequest" />
            <output message="typens:mcaMcaInfoResponse" />
        </operation>

    </portType>
    <!--[-] define portTypes -->
    <!--[+] define binding -->
    <binding name="{{var wsdl.handler}}Binding" type="typens:{{var wsdl.handler}}PortType">
        <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />

        <operation name="mcaMcaCreate">
            <soap:operation soapAction="urn:{{var wsdl.handler}}Action" />
            <input>
                <soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
            </input>
            <output>
                <soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
            </output>
        </operation>

        <operation name="mcaMcaInfo">
            <soap:operation soapAction="urn:{{var wsdl.handler}}Action" />
            <input>
                <soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
            </input>
            <output>
                <soap:body namespace="urn:{{var wsdl.name}}" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
            </output>
        </operation>

    </binding>
    <!--[-] define portTypes -->
    <service name="{{var wsdl.name}}Service">
        <port name="{{var wsdl.handler}}Port" binding="typens:{{var wsdl.handler}}Binding">
            <soap:address location="{{var wsdl.url}}" />
        </port>
    </service>
</definitions>

app/code/local/Course/Mca/etc/wsi.xml - it's similar to wsdl.xml but it's used for WS-I complience

 <?xml version="1.0" encoding="UTF-8"?>
<wsdl:definitions xmlns:typens="urn:{{var wsdl.name}}"
                  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
                  xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
                  xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"
                  xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
                  name="{{var wsdl.name}}"
                  targetNamespace="urn:{{var wsdl.name}}">
    <wsdl:types>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:{{var wsdl.name}}">

            <xsd:complexType name="mcaMcaCreateEntity">
                <xsd:sequence>
                    <xsd:element name="firstname" type="xsd:string" />
                    <xsd:element name="lastname" type="xsd:string" />
                    <xsd:element name="telephone" type="xsd:string" />
                </xsd:sequence>
            </xsd:complexType>

            <xsd:element name="mcaMcaCreateRequestParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="sessionId" type="xsd:string" />
                        <xsd:element minOccurs="1" maxOccurs="1" name="data" type="xsd:string" />
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>

            <xsd:element name="mcaMcaCreateResponseParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="result" type="xsd:string" />
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>

            <xsd:complexType name="mcaMcaInfoEntity">
                <xsd:sequence>
                    <xsd:element name="mca_id" type="xsd:string" />
                    <xsd:element name="firstname" type="xsd:string" />
                    <xsd:element name="lastname" type="xsd:string" />
                    <xsd:element name="telephone" type="xsd:string" />

                    <xsd:element name="created_time" type="xsd:string" />
                    <xsd:element name="updated_time" type="xsd:string" />
                </xsd:sequence>
            </xsd:complexType>

            <xsd:element name="mcaMcaInfoRequestParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="sessionId" type="xsd:string" />
                        <xsd:element minOccurs="1" maxOccurs="1" name="projectId" type="xsd:string" />
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>
            <xsd:element name="mcaMcaInfoResponseParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="result" type="typens:mcaMcaInfoEntity" />
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>

        </xsd:schema>
    </wsdl:types>

    <wsdl:message name="mcaMcaCreateRequest">
        <wsdl:part name="parameters" element="typens:mcaMcaCreateRequestParam" />
    </wsdl:message>
    <wsdl:message name="mcaMcaCreateResponse">
        <wsdl:part name="parameters" element="typens:mcaMcaCreateResponseParam"/>
    </wsdl:message>

    <wsdl:portType name="{{var wsdl.handler}}PortType">

        <wsdl:operation name="mcaMcaCreate">
            <wsdl:documentation>Add Mca</wsdl:documentation>
            <wsdl:input message="typens:mcaMcaCreateRequest" />
            <wsdl:output message="typens:mcaMcaCreateResponse" />
        </wsdl:operation>

        <wsdl:message name="mcaMcaInfoRequest">
            <wsdl:part name="parameters" element="typens:mcaMcaInfoRequestParam" />
        </wsdl:message>
        <wsdl:message name="mcaMcaInfoResponse">
            <wsdl:part name="parameters" element="typens:mcaMcaInfoResponseParam" />
        </wsdl:message>

    </wsdl:portType>
    <wsdl:binding name="{{var wsdl.handler}}Binding" type="typens:{{var wsdl.handler}}PortType">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />

        <wsdl:operation name="mcaMcaCreate">
            <soap:operation soapAction="" />
            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>

        <wsdl:operation name="mcaMcaInfo">
            <soap:operation soapAction="" />
            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>

    </wsdl:binding>
    <wsdl:service name="{{var wsdl.name}}Service">
        <wsdl:port name="{{var wsdl.handler}}Port" binding="typens:{{var wsdl.handler}}Binding">
            <soap:address location="{{var wsdl.url}}" />
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

app/code/local/Course/Mca/Model/Mca/Api.php - model for handling the Api v1 requests (and some V2 requests)

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

    public function items($filters)
    {
    }

    public function update($stuId, $stuData)
    {
    }

    public function delete($stuId)
    {
    }
}

app/code/local/Course/Mca/Model/Mca/Api/V2.php - model for handling the Api v2 requests

 <?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);
            $mca = Mage::helper('api')->wsiArrayPacker($mca);
        }
        catch (Mage_Core_Exception $e) {
            $this->_fault('data_invalid', $e->getMessage());
        }
        return $mca->getId();
    }

    /**
     * Project info
     * @access public
     * @param int $mcaId
     * @return object
     */
    public function info($mcaId){
        $result = parent::info($mcaId);
        $result = Mage::helper('api')->wsiArrayPacker($result);
        return $result;
    }

}

It is showing the SOAP/XML-RPC list as well WSDL and I created username and api to given permission to access the Mca Api.

enter image description here

enter image description here

Finally I invoking the my php file it's returning an error.

my php 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/>';

//$params = array('param1'=>'Dotnet','param2'=>'Php','param3'=>'9676588589');

//v1
//$result = $proxy->call($sessionId, 'mca.create',$params);

//v2
$result = $proxy->mcaMcaCreate($sessionId, 'Dotnet','Php','9676588589');

print_r($result); 


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

Error is: enter image description here

thanks in advance.

Was it helpful?

Solution

It's a kind of error which shows that Magento doesn't find the method of your API. So you have an issue how you define this method in the api.xml. By the way I saw several potential issues. See below

  • You don't need to create again the public function create in your class Course_Mca_Model_Mca_Api_V2 because you extend Course_Mca_Model_Mca_Api and this one has the same method.
  • Your method Course_Mca_Model_Mca_Api::create expects a string as a parameter and a string as a returned value, in your WSDL definition, you define a kind of array (which is anyway badly defined). For your test purpose and to allow you a step by step coding, do:

Replace in wsdl.xml

<message name="mcaMcaCreateRequest">
    <part name="sessionId" type="xsd:string" />
    <part name="data" type="typens:mcaMcaCreateEntity" />
</message>
<message name="mcaMcaCreateResponse">
    <part name="result" type="xsd:int"/>
</message>

With

<message name="mcaMcaCreateRequest">
    <part name="sessionId" type="xsd:string" />
    <part name="data" type="xsd:string" />
</message>
<message name="mcaMcaCreateResponse">
    <part name="result" type="xsd:string"/>
</message>
  • You will have to update also the wsi.xml file like you did for the wsdl.xml file
  • Into api.xml, you have maybe some issue too, do the following:

Replace

    <resources_alias>
        <mca>mca_mca</mca>
    </resources_alias>
    <v2>
        <resources_function_prefix>
            <mca>mcamca</mca>
        </resources_function_prefix>
    </v2>

with

    <resources_alias>
        <mca>mca_mca</mca>
    </resources_alias>
    <v2>
        <resources_function_prefix>
            <mca>mcaMca</mca>
        </resources_function_prefix>
    </v2>

Then in your test code, use the V2 Soap Version

$proxy = new SoapClient('http://localhost/ics/index.php/api/v2_soap?wsdl');
$sessionId = $proxy->login('magento', 'magento@123');
$result = $proxy->mcaMcaCreate($sessionId, 'my text test');
print_r($result);

OTHER TIPS

This is your problem.
In api.xml you have this:

<resources>
    <mca translate="title" module="mca">
        <title>Mca API</title>
        ....

This should be

<resources>
    <mca_mca translate="title" module="mca">
        <title>Mca API</title>
        ....

If you look closely at the example I gave here, there was this in api.xml

<api>
    <resources>
        <portfolio_project translate="title" module="portfolio">

So the tag under the <resources> should be <[module]_[entity]>. In your case the module and the entity have the same name mca, so it should be <mca_mca>

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top