create magento custom api

本文深入探讨了Magento框架中用于管理客户会话和实现API登录的详细过程,包括配置文件、模块、接口和WSDL定义。通过解析`config.xml`、`api.xml`和`wsdl.xml`文件,以及`Session.php`和`Account_Api.php`类,阐述了如何在Magento环境中安全地验证用户身份和执行API操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


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
	 * @param	int|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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值