create magento custom api

来源:互联网 发布:克拉科夫理工大学 知乎 编辑:程序博客网 时间:2024/04/29 23:27


i.e. customer_account.login api

dir path

PACKAGENAME           

     ---Customer 

         ---etc

             ---api.xml

             ---config.xml

             ---wsdl.xml

        ---Model

            ---Session.php

            ---Account

               ---Api

                   ---V2.php

               ---Api.php

Code of config.xml

<?xml version="1.0"?><config>    <modules>        <PACKAGENAME_Customer>            <version>0.1.0</version>        </PACKAGENAME_Customer>    </modules>    <global>        <models>            <customer>            <rewrite>                            <session>PACKAGENAME_Customer_Model_Session</session>                    <account_api>PACKAGENAME_Customer_Model_Account_Api</account_api>                    <account_api_v2>PACKAGENAME_Customer_Model_Account_Api_v2</account_api_v2>                </rewrite>            </customer>        </models>    </global></config>

Code of api.xml

<config>    <api>        <resources>                <customer_account translate="title" module="customer">                <title>Customer Account</title>                <model>customer/account_api</model>                <acl>customer/account</acl>                <methods>                    <login translate="title" module="customer">                        <title>Customer Account login</title>                        <method>login</method>                        <acl>customer/account/login</acl>                                           </login>                                    </methods>                <faults module="customer"> <!-- module="customer" specifies the module which will be used for translation. -->                    <data_invalid> <!-- if we get invalid input data for customers -->                        <code>100</code >                        <!-- we cannot know all the errors that can appear, their details can be found in error message for call -->                        <message>Invalid customer data. Details in error message.</message>                    </data_invalid>                    <filters_invalid>                        <code>101</code >                        <message>Invalid filters specified. Details in error message.</message>                    </filters_invalid>                    <not_exists>                        <code>102</code >                        <message>Customer doesn't exist.</message>                    </not_exists>                    <not_deleted>                        <code>103</code >                        <message>Customer was not deleted. Details in error message.</message>                    </not_deleted>                </faults>            </customer_account>                 </resources>        <resources_alias>            <account>customer_account</account>        </resources_alias>        <v2>            <resources_function_prefix>                <account>customerAccount</account>            </resources_function_prefix>        </v2>        <acl>            <resources>                <customer translate="title" module="customer">                <title>Customer</title>                <account translate="title" module="customer">                <title>Customer Account</title>                    <login translate="title" module="customer">                        <title>Customer Account login</title>                    </login>            </account>            </customer>            </resources>        </acl>            </api></config>
Code of 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/" />            <!-- somthing be omitted -->        </schema>    </types>    <message name="customerAccountLoginRequest">        <part name="sessionId" type="xsd:string"/>        <part name="userName" type="xsd:string"/>        <part name="password" type="xsd:string"/>        <part name="storeView" type="xsd:string"/>    </message>        <message name="customerAccountLoginResponse">        <part name="result" type="typens:boolean"/>    </message>    <portType name="{{var wsdl.handler}}PortType">        <operation name="customerAccountLoginResponse">            <documentation>Customer Account Login</documentation>            <input message="typens:customerAccountLoginRequest"/>            <output message="typens:customerAccountLoginResponse"/>        </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="customerAccountLogin">            <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>

Code of Session.php

class PACKAGENAME_Customer_Model_Session extends Mage_Customer_Model_Session{/** * Customer authorization * * @param   string $username * @param   string $password * @return  bool */public function apiLogin($username, $password ,$storid=null){if($storid){$websiteId = Mage::app()->getStore($storid)->getWebsiteId();}else{$websiteId = Mage::app()->getStore()->getWebsiteId();}/** @var $customer Mage_Customer_Model_Customer */$customer = Mage::getModel('customer/customer')->setWebsiteId($websiteId);if ($customer->authenticate($username, $password)) {$this->setCustomerAsLoggedIn($customer);$this->renewSession();return true;}return false;}}
Code of Api.php

class PACKAGENAME_Customer_Model_Account_Api extends Mage_Api_Model_Resource_Abstract{/** * Customer Account Login Api * * @param   string $username * @param   string $password * @paramint|Null    $storeId * @return  bool */public function login($username=null,$password=null, $store = null){$session = Mage::getSingleton('customer/session');if ($session->isLoggedIn()) {return true; }return array($username,$password,$session->isLoggedIn());if (!empty($username) && !empty($password)) {try {if(!$session->apiLogin($username, $password, $store)){return false;}} catch (Mage_Core_Exception $e) {return false;} catch (Exception $e) {return false;}}else{return false;}return true;}}
V2.php is the same to api.php or you can do something other

Note:Use your own package name instead of  PACKAGENAME


Hope it's helpful for you! Thanks



原创粉丝点击