Php Flow » Magento, Php

How to create Web service in Magneto

Well now I am discussing how to create webservice method in magento.There is a lot of existing webservice in magneto Api ,its divide in different section like (Sales,catalog ect).
Here I am extending sales module and add new web method “getTicketPrice ()”on sales api.
Step 1:First we have register our module
Add the following file in magento/app/etc/modules/Msa_Sales.xml

  1. <?xml version=“1.0″?>  
  2. <config>  
  3.     <modules>  
  4.         <Msa_Sales>  
  5.             <active>true</active>  
  6.             <codePool>local</codePool>  
  7.         </Msa_Sales>  
  8.     </modules>  
  9. </config>  

Step 2: Add a new file api.xml, which is contain the all the method name description and module resource alias. Here Method name node have all the custom method name list.
magento/app/local/Msa/Sales/etc/api.xml

  1. <?xml version=“1.0″?>  
  2. <config>  
  3.     <api>  
  4.         <resources>  
  5.             <sales_order translate=“title” module=“sales”>  
  6.                 <methods>  
  7.                      <getTicketPrice translate=“title” module=“sales”>  
  8.                         <title>get ticket</title>  
  9.                         <acl>sales/order/getticket</acl>  
  10.                     </getTicketPrice>     
  11. </resources>   
  12. <resources_alias>  
  13.             <resourcename>sales_order</resourcename>  
  14.         </resources_alias>  
  15. </api>  
  16. </config>  

Step3:Add a fileconfig.xml in etc folder. Here we tells the module name, object model and override class name to magento
magento/app/local/Msa/Sales/etc/config.xml

  1. <?xml version=“1.0″?>  
  2. <config>  
  3.     <modules>  
  4.         <Msa_Sales>  
  5.             <version>0.1.0</version>  
  6.         </Msa_Sales>  
  7.     </modules>  
  8.     <global>  
  9.         <models>  
  10.             <sales>  
  11.                 <rewrite>  
  12.                     <order_api>Msa_Sales_Model_Order_Api</order_api>  
  13.                 </rewrite>  
  14.             </sales>  
  15.         </models>  
  16.     </global>  
  17. </config>  

Step4:finally give the method definition of getTicketPrice.

  1. public function getTicketPrice()  
  2.     {  
  3. //create connection  
  4.         $read = Mage::getSingleton(‘core/resource’)->getConnection(‘core_read’);  
  5. //write sql query         
  6. $sql=“Select  *  from ticket”;  
  7.         //return $sql;  
  8.         $res=$read->fetchAll($sql);  
  9.                  if($res)  
  10.         {  
  11. //if success then return result set  
  12.         return $res;  
  13.         }else  
  14.         {  
  15. If not then return message  
  16.         $msg[]=“No records Found”;  
  17.         return $msg;  
  18.         }  
  19.                   
  20.     }  

Now you will be able use this webservice method with resource name and method name.
$result1=$client->call($session, ‘sales_order.getTicketPrice’, array());

Did you enjoy this article? Share it!

About the Author:

Hi, This is Parvez Alam from India. I am software developer with 4 years’ experience in web development. I have submitted articles on PHP, Mysql, Magento,CSS, HTML, jQuery, web designing and social API. You can subscribe to my blog via RSS/Twitter/Google plus and Facebook. parvez1487(at)gmail(dot)com

Also like

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>