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)
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.
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:
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.
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.
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.
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…
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.
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.
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.
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’);
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.
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??
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.
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.
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.
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.