Search code examples
magentoundefined

Magento Custom API Code can't pass in parameters (Undefined variable error)


I am trying to create a custom API handler and while I can get the function to run, passing in variables for some reason does not work and I get an empty parameter called in. System.log returns the error:

DEBUG (7): Undefined variable: inputvar.

Please help!

First things first, my directory structure at app/code/local is as follows:

Company
  -Customapi
     -etc
       -api.xml
       -config.xml
       -wsdl.xml
     -Model
        -Order
           -Api.php
           -Api
             -V2.php

Now for the code.

Api.php

class Company_Customapi_Model_Order_Api extends Mage_Api_Model_Resource_Abstract
{      

public function test($inputvar){

    echo $inputvar;
    $result = $inputvar;

    return $result;
}  

}

V2.php

class Company_Customapi_Model_Order_Api_V2 extends Company_Customapi_Model_Order_Api {}

wsdl.xml:

<?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/"/>    
        </schema>
    </types>
    <message name="getTestRequest">
        <part name="inputvar" type="xsd:string"/>
     </message>
     <message name="getTestResponse">
        <part name="result" type="xsd:string"/>
     </message>
    <portType name="{{var wsdl.handler}}PortType">
        <operation name="customapiOrderTest">
            <documentation>Test the API.</documentation>
            <input message="typens:getTestRequest"/>
            <output message="typens:getTestResponse"/>
        </operation>
   </portType>
   <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="customapiOrderTest">
            <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>
   <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>

api.xml:

<?xml version="1.0"?>
<config>
    <api>
        <resources>
            <customapi_order translate="title" module="company_customapi">
                <model>company_customapi_model_order_api</model>
                <title>company Order API</title>
                <acl>order</acl>
                <methods>
                    <test translate="title" module="company_customapi">
                        <title>A test function.</title>
                        <acl>test</acl>
                    </test>
                </methods>
            </customapi_order>
        </resources>
        <v2>
            <resources_function_prefix>
                <customapi_order>customapiOrder</customapi_order>
                <!-- prefix+functionname = customapiOrderTest -->
            </resources_function_prefix>
        </v2>
    </api>
</config>

config.xml:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Company_Customapi>
            <version>0.1.0</version>
        </Company_Customapi>
    </modules>
   <global>
        <models>
            <Company_Customapi>
                <class>Company_Customapi_Model</class>
            </Company_Customapi>
        </models>
    </global>
</config>

test.php:

$session = $client->login(SOAP_USER, SOAP_PASS);
$result = $client->customapiOrderTest($session, "inputvaluehere");
             var_dump ( $result);  

Solution

  • Found the answer. In Wsdl.xml you have to specify the session parameter as one of your inputs (your function on the server can't see it but you do pass it in from the client)

    <message name="getTestRequest">
       <part name="sessionId" type="xsd:string"/>
       <part name="inputvar" type="xsd:string"/>
    </message>
    

    Only took me 2 days to find the answer. Don't you just love Magento!