Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 3
Custom API XML-RPC & SOAP Hello World Example
 
miliscent
Member
 
Avatar
Total Posts:  45
Joined:  2009-03-11
Pittsburgh, Pennsylvania USA
 

I’ve spent some time this afternoon looking for a good Hello World API example.  One that can be called from either the XML RPC or SOAP API.  I found a few here and there but none of them actually worked for me.  Found some custom module examples too but they weren’t what I was looking for.  I wanted something to be called externally and not to extend Magento internally.  And that Wiki Custom API entry is a total disaster… the author should be drug out into the street and beaten… but I digress.

Here’s my attempt to at least help people get started.  Hopefully someone with more knowledge can build upon this example in the thread and who knows, maybe we can build this up into a real Wiki entry on the topic ;o)

Module directory structure:

magento/
  |-- 
app/
      |-- 
code/
      |   |-- 
local/
      |       |-- 
Companyname/
      |           |-- 
Modulename/
      |               |-- 
Model/
      |               |    |-- 
Objectmodel/
      |               |        |-- 
Api.php
      
|               |-- etc/
      |                    |-- 
api.xml
      
|                    |-- config.xml
      
|-- etc/
           |-- 
modules/
                |-- 
Companyname_Modulename.xml

magento / etc / modules / Companyname_Modulename.xml

<?xml version="1.0"?>
<config>
    <
modules>
        <
Companyname_Modulename>
            <
active>true</active>
            <
codePool>local</codePool>
        </
Companyname_Modulename>
    </
modules>
</
config>

magento / app / code / local / Companyname / Modulename / etc / api.xml

<?xml version="1.0"?>
<config>
    <
api>
        <
resources>
            <
resource_name translate="title" module="companyname_modulename">
            <
title>Title Of My Resource</title>
            <
model>modulename/objectmodel_api</model>
            <
methods>
                <
methodName translate="title" module="companyname_modulename">
                    <
title>Title Of My Method</title>
                </
methodName>
            </
methods>
            </
resource_name>
        </
resources>
    </
api>
</
config>

magento / app / code / local / Companyname / Modulename / etc / config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <
modules>
        <
Companyname_Modulename>
            <
version>0.1.0</version>
        </
Companyname_Modulename>
    </
modules>
    <global>
        <
models>
            <
modulename>
                <class>
Companyname_Modulename_Model</class>
            </
modulename>
        </
models>
    </global>
</
config>

magento / app / code / local / Companyname / Modulename / Model / Objectmodel / Api.php

<?php
class Companyname_Modulename_Model_ObjectModel_Api extends Mage_Api_Model_Resource_Abstract
{
    
public function methodName($arg)
    
{
        
return "Hello World! My argument is : " $arg;
    
}
}
?>

SOAP API Example

$proxy = new SoapClient('http://www.yourhost.com/magento/api/?wsdl');
$sessionId $proxy->login('apiUser''apiKey');
echo 
"Login ID : $sessionId";
$result $proxy->call($sessionId, array('resource_name.methodName'), array('param1' => 'the argument passed...'));
echo 
$result;

Notes:

Beware of caching!  Either refresh your cache or turn it off while you are working on this as it will need refreshed with each change.

You define your own API external names!  In api.xml, the node <resource_name> is what is used to call your custom api in the SOAP sample.  This is how you define what the external name is and you can change the name of the node to be what you want (avoiding naming conflicts of course).  It is NOT called by your companyname_modulename.methodName as I thought at first.

No ACL’s!  This example does not make use of any ACL’s.  That is not it’s intention so please be aware that it any user who can login can call this sample method.

The app directory containing these 4 files and directory structure is attached for download.

File Attachments
app.zip  (File Size: 3KB - Downloads: 1682)
 
Magento Community Magento Community
Magento Community
Magento Community
 
Lavender
Jr. Member
 
Total Posts:  4
Joined:  2009-05-04
 

First of all, I’d like to thank miliscent for this, as it was immensely helpful in developing my custom API. The problem is, that this custom API needs to be accessible from .NET 2008 (C#), so I have to use SOAP 2 to access it.

The above example works for SOAP 1 only.

Now I did some changes, based on what I saw in the api for the Directory core module.
In the Objectmodel folder, I defined a folder Api, and inside it a file V2.php

V2.php

<?php
class Company_Modulename_Model_ObjectModel_Api_V2 extends Companyname_Modulename_Model_ObjectModel_Api{
    
}
?>

Also in the etc folder, I defined a wsdl.xml file as follows:

<?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="resourceNameEntity">
                <
all>                    
                    <
element name="name" type="xsd:string" />
                </
all>
            </
complexType>
            <
complexType name="resourceNameEntityArray">
                <
complexContent>
                    <
restriction base="soapenc:Array">
                        <
attribute ref="soapenc:arrayType" wsdl:arrayType="typens:resourceNameEntity[]" />
                    </
restriction>
                </
complexContent>
            </
complexType>            
        </
schema>
    </
types>
    <
message name="resourceNameMethodNameRequest">
        <
part name="sessionId" type="xsd:string" />
    </
message>
    <
message name="resourceNameMethodNameResponse">
        <
part name="result" type="xsd:string" />
    </
message>  
    <
portType name="{{var wsdl.handler}}PortType">
        <
operation name="resourceNameMethodName">
            <
documentation>Test method</documentation>
            <
input message="typens:resourceNameMethodNameRequest" />
            <
output message="typens:resourceNameMethodNameResponse" />
        </
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="resourceNameMethodName">
            <
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>

So far I got resourceNameEntity and resourceNameMethodName to show up in magento’s wsdl. However I can’t access the functions.
When I call $proxy->__getFunctions(), only the core API functions show up.

I even tried to call $proxy->resourceNameMethodName($sessionId) but I get an error (says that resourceNameMethodName is not a valid function name).

I’d really really appreciate if anyone could help me. I can’t find ANY documentation/tutorial/article on this.

EDIT:
I’m using Magento 1.3.1
I tried to get things to work on Magento 1.3.2.3 but not even using soap 1 could I get it to show anything. Plus the resources array came up empty.

 
Magento Community Magento Community
Magento Community
Magento Community
 
applesauce49
Jr. Member
 
Total Posts:  8
Joined:  2009-05-05
 

Hey, just wanted to let you know that I’ve got this figured out and will be posting a detailed description of what I did later this evening.  I’ve got one last bug, but I’m really close!

The two keys I’ve found is check your case on folder names and also don’t use “_“‘s when you name things, that tells magento to break to a new folder. 

I’ll explain it all later.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Lavender
Jr. Member
 
Total Posts:  4
Joined:  2009-05-04
 

Yes please, that would be really useful, thanks.
I didn’t use underscores, and the casing tends to confuse me rasberry

 
Magento Community Magento Community
Magento Community
Magento Community
 
applesauce49
Jr. Member
 
Total Posts:  8
Joined:  2009-05-05
 

Here is a copy of the code I’ve got mostly working right now.  Just expand the .zip an drop that folder in .../magento/app/code/local and drop the CompanyName_Modulename.xml file in .../magento/app/etc/modules

The problem I’m having though is my in argument is getting lost and nothing is being returned out through the wsdl.  If I make a Soap V1 call to this, it works fine.  It’s when I try to make a V2 call that goes sideways.

Here a sample of what I’m trying to do:

<?php
//THIS WORKS!!!!
$WSDLUrl1 "http://localhost/magento/index.php/api/?wsdl";
$proxy1 = new SoapClient($WSDLUrl);
$sessionId $proxy1->login('admin''rat3pack');

try 
{
    $result 
$proxy1->call($sessionId'resource_name.methodName''1');
}

catch (Exception $e{
    
echo $e->getMessage();
}

echo $result;

//------------------------------------------------

//THIS DOESN"T WORK!!!!
$WSDLUrl2 "http://localhost/magento/index.php/api/v2_soap/?wsdl";
$proxy2 = new SoapClient($WSDLUrl);
$sessionId2 $proxy->login('admin''rat3pack');

try 
{
    $result 
$proxy2->resourceNameMethodName($sessionId"TestArg");
}

catch (Exception $e{
    
echo $e->getMessage();
    return;
}

echo $result;
?>

Hope this helps you, and if you have any insight into why the args are getting lost I’d really appreciate learning what you know!

And hey Varien staff!  Any chance you could step in here and give a hand!?  I think we’ve given the required pound of flesh on this one to get just a little help…

File Attachments
Companyname.zip  (File Size: 4KB - Downloads: 1104)
Companyname_Modulename.xml  (File Size: 1KB - Downloads: 906)
 
Magento Community Magento Community
Magento Community
Magento Community
 
applesauce49
Jr. Member
 
Total Posts:  8
Joined:  2009-05-05
 

I forgot to also mention, make sure you refresh your web reference in Visual Studio after you add this new module so it can pick up the new WSDL information.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Lavender
Jr. Member
 
Total Posts:  4
Joined:  2009-05-04
 

Yeah, connecting through SOAP V1 (as well as XML-RPC) works fine for me too… in PHP.
Unfortunately, it’s the .NET (C#) part that fails. It can’t connect through that URL. It crashes. From what I’ve read, .NET doesn’t work with soap v1.

 
Magento Community Magento Community
Magento Community
Magento Community
 
fakdora
Jr. Member
 
Total Posts:  4
Joined:  2009-08-04
 
applesauce49 - 05 August 2009 06:58 AM

Here is a copy of the code I’ve got mostly working right now.  Just expand the .zip an drop that folder in .../magento/app/code/local and drop the CompanyName_Modulename.xml file in .../magento/app/etc/modules

The problem I’m having though is my in argument is getting lost and nothing is being returned out through the wsdl.  If I make a Soap V1 call to this, it works fine.  It’s when I try to make a V2 call that goes sideways.

Here a sample of what I’m trying to do:

<?php
//THIS WORKS!!!!
$WSDLUrl1 "http://localhost/magento/index.php/api/?wsdl";
$proxy1 = new SoapClient($WSDLUrl);
$sessionId $proxy1->login('admin''rat3pack');

try 
{
    $result 
$proxy1->call($sessionId'resource_name.methodName''1');
}

catch (Exception $e{
    
echo $e->getMessage();
}

echo $result;

//------------------------------------------------

//THIS DOESN"T WORK!!!!
$WSDLUrl2 "http://localhost/magento/index.php/api/v2_soap/?wsdl";
$proxy2 = new SoapClient($WSDLUrl);
$sessionId2 $proxy->login('admin''rat3pack');

try 
{
    $result 
$proxy2->resourceNameMethodName($sessionId"TestArg");
}

catch (Exception $e{
    
echo $e->getMessage();
    return;
}

echo $result;
?>

Hope this helps you, and if you have any insight into why the args are getting lost I’d really appreciate learning what you know!

And hey Varien staff!  Any chance you could step in here and give a hand!?  I think we’ve given the required pound of flesh on this one to get just a little help…

Your code does work for both cases if you just fix three typo errors in the code.

$proxy2 = new SoapClient($WSDLUrl);
$sessionId2 = $proxy->login(’admin’, ‘rat3pack’);

try {
$result = $proxy2->resourceNameMethodName($sessionId, “TestArg");
}

Change $WSDLUrl to $WSDLUrl2 in “$proxy2 = SoapClient($WSDLUrl);”
change $proxy to $proxy2 in “$proxy->login(’admin’, ‘rat3pack’);”
Also, change $sessionId to $sessionId2 in “$result = $proxy2->resourceNameMethodName($sessionId, “TestArg");"

That’s it. and it works flawlessly. Thanks for the code and tutorials guys, it has helped me a great deal.

 
Magento Community Magento Community
Magento Community
Magento Community
 
sim9
Jr. Member
 
Total Posts:  5
Joined:  2009-06-23
 

Hi,
I am a novice in Magento development and totally new to customizing API.I needed to create an API to update an additional EAV attribute called eg_order_id by order_id.On exploring through various materials I came across your post and decided to start by a simple workable example.I created all the necessary files within a custom module.But frankly speaking I really dont know where to look for the output.The SOAP API example part in the first post, where do I write this? Are the ‘apiUser’ and ‘apiKey’ defined somewhere and are these the magento admin login credentials or something else?And for SOAP 2 to access it in the file called V2.php, what code do we place there?Is that where the API function is written but then isn’t Api.php the file that contains API function??

Somebody please guide me on this........

 
Magento Community Magento Community
Magento Community
Magento Community
 
culshaw
Jr. Member
 
Total Posts:  1
Joined:  2009-11-09
 

Is it just me or does this not work in 1.3.2.4,

SOAP V2 that is…

My SOAP1 output works fine.... but not v2

I made my own from scratch and also used this example and keep getting this error:

syntax errorunexpected T_CLASSexpecting T_STRING

I’m thinking this is because the V2 file doesn’t get loaded into the registry?

If it worked fine when the previous example was published this should work in 1.3.2.2 but im still to verify this…

Has anyone encountered this and/or fixed this??

Thanks in need
Ian

 
Magento Community Magento Community
Magento Community
Magento Community
 
neurox
Jr. Member
 
Total Posts:  6
Joined:  2009-01-29
 

Sweet post - I printed out the first post and actually colored the different entities (company name, module name etc.) different colors to fully comprehend how this works. Even with nearly 20 years of programming experience, Magento still puzzles me.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mirage
Member
 
Avatar
Total Posts:  40
Joined:  2008-06-09
Milwaukee, WI
 

Armed with the information above and elsewhere on the internets, I’ve been at it all day trying to extend the Customer API. Simply extending it works actually fine.

The goal is to add an [optional] second parameter ‘sendEmail’ to the customer.create() call.

And I need it to work with V2. I already have it to the point where I overloaded the V2 create() call in my own module. Now I need some sort of conditional way to send a greeting email or not. Figured an additional parameter was the proper way to go.

Hope someone see this.

Thanks in advance,
Juergen

 
Magento Community Magento Community
Magento Community
Magento Community
 
Saaslink
Jr. Member
 
Avatar
Total Posts:  10
Joined:  2009-05-15
Sydney, Australia
 

Hey there, good work on the v2 soap example!

However upon installation in Magento 1.3.2.3 on PHP5.2.5 this component does not work as intended. Has anyone else encountered the same problem? The method is listed correctly in the wsdl service query but I’m getting :

“Uncaught SoapFault exception: [Client] Function ("resourceNameMethodName") is not a valid method for this service” error

Having read many of the online help it sounds like Magento’s v2_soap still have a number of issues and much more documentation than is available.

BT

 
Magento Community Magento Community
Magento Community
Magento Community
 
cfelder
Jr. Member
 
Total Posts:  6
Joined:  2010-06-20
 

Thanks for the great work. This example gave me a good insight on what’s actually happening.

I actually had the same problem with the v2 API. If I do the API call with PHP’s SoapClient I got an error. Other clients behaved differently, but it did not work in any case.

Function ("resourceNameMethodName"is not a valid method for this service

The problem is that PHP and other clients cache the wsdl. So you might be working with a cached wsdl instead of the one with your newest changes. On my mac, the files are cached in /tmp/wsdl*. Deleting those files will force the client to download the wsdl again and everything works fine.

sudo rm -rf /tmp/wsdl*
 
Magento Community Magento Community
Magento Community
Magento Community
 
BjoneMan
Member
 
Total Posts:  45
Joined:  2009-03-04
 

Hello at all,
has anyone find a working solution to extend the v2 soap webservice!
I tried the above mention but get an error!

Is there any example code out there!

I read a lot of tutorials but did\’nt find any working solution as an beginning to customze for my needs!

Working with 1.4.1.0, and the trunk to get the webservice work!
Thanks everybody.

 
Magento Community Magento Community
Magento Community
Magento Community
 
jlenz
Jr. Member
 
Total Posts:  8
Joined:  2010-05-12
 

Invalid api path.  I copied everything PERFECTLY.. SEVERAL times.... cleared cache/refreshed reference in VS… not working.  So frustrating.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 3