Làm cách nào tôi có thể tạo Api SOAP / XML-RPC v1 & V2 tùy chỉnh trong magento?


11

Làm cách nào tôi có thể tạo API SOAP / XML-RPC V1 & V2 tùy chỉnh trong Magento?


cảm ơn câu trả lời của bạn và mã của bạn Tôi đã thử mã của bạn (sau rất nhiều thời gian trên mã tùy chỉnh của tôi) trong cài đặt Magento của tôi và tôi luôn gặp lỗi này khi tôi gọi một trong các phương thức danh mục đầu tư mới: Đường dẫn Api không hợp lệ. Mã này có tương thích 1.8 không? Cảm ơn nhiều vì sự giúp đỡ !!
Gerfaut

@GmapsMakeMeCrazy, nó hoạt động tốt với Magento Community Edition 1.7. Tôi chưa thử 1.8.
Manoj Kumar

Câu trả lời:


15

Bạn có thể đọc nó . Nó giải thích rất nhiều nhưng chủ yếu là cho API V1.
Để cho bạn thấy làm thế nào bạn có thể tạo một API tôi nghĩ tốt hơn là đưa ra một ví dụ.
Giả sử bạn có một mô-đun được đặt tên Easylife_Portfoliovới một thực thể có tên Project.
Đối với thực thể này, bạn có thể đặt tên, mô tả và trạng thái.
Dưới đây là các tệp bạn cần ngoài các tệp còn lại (bộ điều khiển, mô hình, khối ..).
app/code/local/Easylife/Portfolio/etc/api.xml- hồ sơ khai báo api.

<?xml version="1.0"?>
<config>
    <api>
        <resources>
            <portfolio_project translate="title" module="portfolio">
                <title>Project API</title>
                <model>portfolio/project_api</model>
                <acl>portfolio/project</acl><!-- acl resource alias -->
                <methods><!-- definne the methods -->
                    <list translate="title" module="portfolio"><!-- list projects -->
                        <title>Retrieve list of projects</title>
                        <method>items</method>
                        <acl>portfolio/project/list</acl>
                    </list>
                    <info translate="title" module="portfolio"><!-- project details -->
                        <title>Retrieve project info</title>
                        <acl>portfolio/project/info</acl>
                    </info>
                    <add translate="title" module="portfolio"><!-- add project -->
                        <title>Add project</title>
                        <acl>portfolio/project/add</acl>
                    </add>
                    <update translate="title" module="portfolio"><!-- update project -->
                        <title>Update project</title>
                        <acl>portfolio/project/update</acl>
                    </update>
                    <remove translate="title" module="portfolio"><!-- remove project-->
                        <title>Remove project</title>
                        <acl>portfolio/project/remove</acl>
                    </remove>
                </methods>
                <faults module="portfolio"><!-- errors that might appear-->
                    <project_not_exists>
                        <code>101</code>
                        <message>Requested project does not exist.</message>
                    </project_not_exists>
                    <invalid_data>
                        <code>102</code>
                        <message>Provided data is invalid.</message>
                    </invalid_data>
                    <save_error>
                        <code>103</code>
                        <message>Error while saving project. Details in error message.</message>
                    </save_error>
                    <remove_error>
                        <code>104</code>
                        <message>Error while removing project. Details in error message.</message>
                    </remove_error>
                </faults>
            </portfolio_project>
        </resources>
        <resources_alias>
            <project>portfolio_project</project>
        </resources_alias>
        <v2>
            <resources_function_prefix>
                <project>portfolioProject</project>
            </resources_function_prefix>
        </v2>
        <acl><!-- acl definition -->
            <resources>
                <portfolio translate="title" module="portfolio">
                    <title>Portfolio</title>
                    <project translate="title" module="portfolio">
                        <title>Project</title>
                        <sort_order>110</sort_order>
                        <list translate="title" module="portfolio">
                            <title>List</title>
                        </list>
                        <info translate="title" module="portfolio">
                            <title>Info</title>
                        </info>
                        <add translate="title" module="portfolio">
                            <title>Add</title>
                        </add>
                        <update translate="title" module="portfolio">
                            <title>Update</title>
                        </update>
                        <remove translate="title" module="portfolio">
                            <title>Remove</title>
                        </remove>
                    </project>
                </portfolio>
            </resources>
        </acl>
    </api>
</config>

app/code/local/Easylife/Portfolio/etc/wsdl.xml - phần wsdl cho V2

<?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="portfolioProjectListEntity"><!-- define type for project list -->
                <all>
                    <element name="entity_id" type="xsd:string" minOccurs="1" />
                    <element name="name" type="xsd:string" minOccurs="1" />
                    <element name="description" type="xsd:string" minOccurs="0" />
                    <element name="status" type="xsd:string" minOccurs="0" />
                    <element name="created_at" type="xsd:string" minOccurs="1" />
                    <element name="updated_at" type="xsd:string" minOccurs="1" />
                </all>
            </complexType>
            <complexType name="portfolioProjectListEntityArray"><!-- define type array of projects -->
                <complexContent>
                    <restriction base="soapenc:Array">
                        <attribute ref="soapenc:arrayType" wsdl:arrayType="typens:portfolioProjectListEntity[]" />
                    </restriction>
                </complexContent>
            </complexType>
            <complexType name="portfolioProjectAddEntity"><!-- define type for add project -->
                <all>
                    <element name="name" type="xsd:string" minOccurs="1" />
                    <element name="description" type="xsd:string" minOccurs="0" />
                    <element name="status" type="xsd:string" minOccurs="0" />

                </all>
            </complexType>
            <complexType name="portfolioProjectUpdateEntity"><!-- define type for update project -->
                <all>
                    <element name="name" type="xsd:string" minOccurs="1" />
                    <element name="description" type="xsd:string" minOccurs="0" />
                    <element name="status" type="xsd:string" minOccurs="0" />

                </all>
            </complexType>
            <complexType name="portfolioProjectInfoEntity"><!-- define type for retrieve info -->
                <all>
                    <element name="entity_id" type="xsd:string" minOccurs="1" />
                    <element name="name" type="xsd:string" minOccurs="1" />
                    <element name="description" type="xsd:string" minOccurs="0" />
                    <element name="status" type="xsd:string" minOccurs="0" />

                    <element name="created_at" type="xsd:string" minOccurs="1" />
                    <element name="updated_at" type="xsd:string" minOccurs="1" />
                </all>
            </complexType>
                </schema>
    </types>
    <!--[+] define messages -->
    <message name="portfolioProjectListRequest">
        <part name="sessionId" type="xsd:string" />
        <part name="filters" type="typens:filters"/>
    </message>
    <message name="portfolioProjectListResponse">
        <part name="result" type="typens:portfolioProjectListEntityArray" />
    </message>
    <message name="portfolioProjectInfoRequest">
        <part name="sessionId" type="xsd:string" />
        <part name="projectId" type="xsd:string" />
    </message>
    <message name="portfolioProjectInfoResponse">
        <part name="result" type="typens:portfolioProjectInfoEntity" />
    </message>
    <message name="portfolioProjectAddRequest">
        <part name="sessionId" type="xsd:string" />
        <part name="data" type="typens:portfolioProjectAddEntity" />
    </message>
    <message name="portfolioProjectAddResponse">
        <part name="result" type="xsd:int"/>
    </message>
    <message name="portfolioProjectUpdateRequest">
        <part name="sessionId" type="xsd:string" />
        <part name="projectId" type="xsd:string" />
        <part name="data" type="typens:portfolioProjectUpdateEntity" />
    </message>
    <message name="portfolioProjectUpdateResponse">
        <part name="result" type="xsd:boolean" />
    </message>
    <message name="portfolioProjectRemoveRequest">
        <part name="sessionId" type="xsd:string" />
        <part name="projectId" type="xsd:string" />
    </message>
    <message name="portfolioProjectRemoveResponse">
        <part name="result" type="xsd:boolean" />
    </message>
    <!--[-] define messages -->
    <!--[+] define portTypes -->
    <portType name="{{var wsdl.handler}}PortType">
        <operation name="portfolioProjectList">
            <documentation>Retrieve list of project</documentation>
            <input message="typens:portfolioProjectListRequest" />
            <output message="typens:portfolioProjectListResponse" />
        </operation>
        <operation name="portfolioProjectInfo">
            <documentation>Retrieve project info</documentation>
            <input message="typens:portfolioProjectInfoRequest" />
            <output message="typens:portfolioProjectInfoResponse" />
        </operation>
        <operation name="portfolioProjectAdd">
            <documentation>Add project</documentation>
            <input message="typens:portfolioProjectAddRequest" />
            <output message="typens:portfolioProjectAddResponse" />
        </operation>
        <operation name="portfolioProjectUpdate">
            <documentation>Update project</documentation>
            <input message="typens:portfolioProjectUpdateRequest" />
            <output message="typens:portfolioProjectUpdateResponse" />
        </operation>
        <operation name="portfolioProjectRemove">
            <documentation>Remove project</documentation>
            <input message="typens:portfolioProjectRemoveRequest" />
            <output message="typens:portfolioProjectRemoveResponse" />
        </operation>
    </portType>
    <!--[-] define portTypes -->
    <!--[+] define binding -->
    <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="portfolioProjectList">
            <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>
        <operation name="portfolioProjectInfo">
            <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>
        <operation name="portfolioProjectAdd">
            <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>
        <operation name="portfolioProjectUpdate">
            <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>
        <operation name="portfolioProjectRemove">
            <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>
    <!--[-] define portTypes -->
    <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>

app/code/local/Easylife/Portfolio/etc/wsi.xml- nó tương tự wsdl.xmlnhưng nó được sử dụng cho sự tuân thủ WS-I

<?xml version="1.0" encoding="UTF-8"?>
<wsdl: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/"
     name="{{var wsdl.name}}"
     targetNamespace="urn:{{var wsdl.name}}">
    <wsdl:types>
        <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="urn:{{var wsdl.name}}">
            <xsd:complexType name="portfolioProjectListEntityArray">
                <xsd:sequence>
                    <xsd:element maxOccurs="unbounded" name="complexObjectArray" type="typens:portfolioProjectListEntity" />
                </xsd:sequence>
            </xsd:complexType>
            <xsd:complexType name="portfolioProjectListEntity">
                <xsd:sequence>
                    <xsd:element name="entity_id" type="xsd:string" />
                    <xsd:element name="name" type="xsd:string" />
                    <xsd:element name="description" type="xsd:string" />
                    <xsd:element name="status" type="xsd:string" />

                    <xsd:element name="created_at" type="xsd:string" />
                    <xsd:element name="updated_at" type="xsd:string" />
                </xsd:sequence>
            </xsd:complexType>
            <xsd:complexType name="portfolioProjectAddEntity">
                <xsd:sequence>
                    <xsd:element name="name" type="xsd:string" />
                    <xsd:element name="description" type="xsd:string" />
                    <xsd:element name="status" type="xsd:string" />

                </xsd:sequence>
            </xsd:complexType>
            <xsd:complexType name="portfolioProjectUpdateEntity">
                <xsd:sequence>
                    <xsd:element name="name" type="xsd:string" />
                    <xsd:element name="description" type="xsd:string" />
                    <xsd:element name="status" type="xsd:string" />

                </xsd:sequence>
            </xsd:complexType>
            <xsd:complexType name="portfolioProjectInfoEntity">
                <xsd:sequence>
                    <xsd:element name="entity_id" type="xsd:string" />
                    <xsd:element name="name" type="xsd:string" />
                    <xsd:element name="description" type="xsd:string" />
                    <xsd:element name="status" type="xsd:string" />

                    <xsd:element name="created_at" type="xsd:string" />
                    <xsd:element name="updated_at" type="xsd:string" />
                </xsd:sequence>
            </xsd:complexType>

            <xsd:element name="portfolioProjectListRequestParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="sessionId" type="xsd:string" />
                        <xsd:element minOccurs="0" maxOccurs="1" name="filters" type="typens:filters" />
                    </xsd:sequence>
                 </xsd:complexType>
            </xsd:element>
            <xsd:element name="portfolioProjectListResponseParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="result" type="typens:portfolioProjectListEntityArray" />
                    </xsd:sequence>
                 </xsd:complexType>
            </xsd:element>
            <xsd:element name="portfolioProjectInfoRequestParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="sessionId" type="xsd:string" />
                        <xsd:element minOccurs="1" maxOccurs="1" name="projectId" type="xsd:string" />
                    </xsd:sequence>
                 </xsd:complexType>
            </xsd:element>
            <xsd:element name="portfolioProjectInfoResponseParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="result" type="typens:portfolioProjectInfoEntity" />
                    </xsd:sequence>
                 </xsd:complexType>
            </xsd:element>
            <xsd:element name="portfolioProjectAddRequestParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="sessionId" type="xsd:string" />
                        <xsd:element minOccurs="1" maxOccurs="1" name="data" type="typens:portfolioProjectAddEntity" />
                    </xsd:sequence>
                 </xsd:complexType>
            </xsd:element>
            <xsd:element name="portfolioProjectAddResponseParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="result" type="xsd:int" />
                    </xsd:sequence>
                 </xsd:complexType>
            </xsd:element>
            <xsd:element name="portfolioProjectUpdateRequestParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="sessionId" type="xsd:string" />
                        <xsd:element minOccurs="1" maxOccurs="1" name="projectId" type="xsd:string" />
                        <xsd:element minOccurs="1" maxOccurs="1" name="data" type="typens:portfolioProjectUpdateEntity" />
                    </xsd:sequence>
                 </xsd:complexType>
            </xsd:element>
            <xsd:element name="portfolioProjectUpdateResponseParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="result" type="xsd:boolean" />
                    </xsd:sequence>
                 </xsd:complexType>
            </xsd:element>
            <xsd:element name="portfolioProjectRemoveRequestParam">
                <xsd:complexType>
                    <xsd:sequence>
                    <xsd:element minOccurs="1" maxOccurs="1" name="sessionId" type="xsd:string" />
                        <xsd:element minOccurs="1" maxOccurs="1" name="projectId" type="xsd:string" />
                    </xsd:sequence>
                 </xsd:complexType>
            </xsd:element>
            <xsd:element name="portfolioProjectRemoveResponseParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="result" type="xsd:boolean" />
                    </xsd:sequence>
                 </xsd:complexType>
            </xsd:element>
                </xsd:schema>
    </wsdl:types>
    <wsdl:message name="portfolioProjectListRequest">
        <wsdl:part name="parameters" element="typens:portfolioProjectListRequestParam" />
    </wsdl:message>
    <wsdl:message name="portfolioProjectListResponse">
        <wsdl:part name="parameters" element="typens:portfolioProjectListResponseParam" />
    </wsdl:message>
    <wsdl:message name="portfolioProjectInfoRequest">
        <wsdl:part name="parameters" element="typens:portfolioProjectInfoRequestParam" />
    </wsdl:message>
    <wsdl:message name="portfolioProjectInfoResponse">
        <wsdl:part name="parameters" element="typens:portfolioProjectInfoResponseParam" />
    </wsdl:message>
    <wsdl:message name="portfolioProjectAddRequest">
        <wsdl:part name="parameters" element="typens:portfolioProjectAddRequestParam" />
    </wsdl:message>
    <wsdl:message name="portfolioProjectAddResponse">
        <wsdl:part name="parameters" element="typens:portfolioProjectAddResponseParam"/>
    </wsdl:message>
    <wsdl:message name="portfolioProjectUpdateRequest">
        <wsdl:part name="parameters" element="typens:portfolioProjectUpdateRequestParam" />
    </wsdl:message>
    <wsdl:message name="portfolioProjectUpdateResponse">
        <wsdl:part name="parameters" element="typens:portfolioProjectUpdateResponseParam"/>
    </wsdl:message>
    <wsdl:message name="portfolioProjectRemoveRequest">
        <wsdl:part name="parameters" element="typens:portfolioProjectRemoveRequestParam" />
    </wsdl:message>
    <wsdl:message name="portfolioProjectRemoveResponse">
        <wsdl:part name="parameters" element="typens:portfolioProjectRemoveResponseParam" />
    </wsdl:message>
    <wsdl:portType name="{{var wsdl.handler}}PortType">
        <wsdl:operation name="portfolioProjectList">
            <wsdl:documentation>Retrieve list of projects</wsdl:documentation>
            <wsdl:input message="typens:portfolioProjectListRequest" />
            <wsdl:output message="typens:portfolioProjectListResponse" />
        </wsdl:operation>
        <wsdl:operation name="portfolioProjectInfo">
            <wsdl:documentation>Retrieve project info</wsdl:documentation>
            <wsdl:input message="typens:portfolioProjectInfoRequest" />
            <wsdl:output message="typens:portfolioProjectInfoResponse" />
        </wsdl:operation>
        <wsdl:operation name="portfolioProjectAdd">
            <wsdl:documentation>Add project</wsdl:documentation>
            <wsdl:input message="typens:portfolioProjectAddRequest" />
            <wsdl:output message="typens:portfolioProjectAddResponse" />
        </wsdl:operation>
        <wsdl:operation name="portfolioProjectUpdate">
            <wsdl:documentation>Update project</wsdl:documentation>
            <wsdl:input message="typens:portfolioProjectUpdateRequest" />
            <wsdl:output message="typens:portfolioProjectUpdateResponse" />
        </wsdl:operation>
        <wsdl:operation name="portfolioProjectRemove">
            <wsdl:documentation>Remove project</wsdl:documentation>
            <wsdl:input message="typens:portfolioProjectRemoveRequest" />
            <wsdl:output message="typens:portfolioProjectRemoveResponse" />
        </wsdl:operation>
        </wsdl:portType>
    <wsdl:binding name="{{var wsdl.handler}}Binding" type="typens:{{var wsdl.handler}}PortType">
        <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http" />
        <wsdl:operation name="portfolioProjectList">
            <soap:operation soapAction="" />
            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="portfolioProjectInfo">
            <soap:operation soapAction="" />
            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="portfolioProjectAdd">
            <soap:operation soapAction="" />
            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="portfolioProjectUpdate">
            <soap:operation soapAction="" />
            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
        <wsdl:operation name="portfolioProjectRemove">
            <soap:operation soapAction="" />
            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>
        </wsdl:binding>
    <wsdl:service name="{{var wsdl.name}}Service">
        <wsdl:port name="{{var wsdl.handler}}Port" binding="typens:{{var wsdl.handler}}Binding">
            <soap:address location="{{var wsdl.url}}" />
        </wsdl:port>
    </wsdl:service>
</wsdl:definitions>

Ví dụ không phù hợp với một câu trả lời. Xem phần còn lại của các tập tin ở đây


Lưu ý : Những tệp này được tạo bằng Ultimate Module Creator . Tôi vừa thêm một số nhận xét trong các tệp xml.


1
Bạn có thể đọc nó, một hướng dẫn tốt về bit.ly/1OXlHMD
Rinto George

7

Thật không may, ví dụ không phù hợp với một câu trả lời. Dưới đây là phần còn lại của các tệp bạn cần:

Bây giờ chúng ta đã hoàn thành với các XML. Hãy lấy mã:

app/code/local/Easylife/Portfolio/Model/Project/Api.php - mô hình để xử lý các yêu cầu Api v1 (và một số yêu cầu V2)

<?php
class Easylife_Portfolio_Model_Project_Api extends Mage_Api_Model_Resource_Abstract{
    /**
     * init project
     * @access protected
     * @param $projectId
     * @return Easylife_Portfolio_Model_Project
     */
    protected function _initProject($projectId){
        $project = Mage::getModel('portfolio/project')->load($projectId);
        if (!$project->getId()) {
            $this->_fault('project_not_exists');
        }
        return $project;
    }
    /**
     * get projects
     * @access public
     * @param mixed $filters
     * @return array
     */
    public function items($filters = null){
        $collection = Mage::getModel('portfolio/project')->getCollection();
        $apiHelper = Mage::helper('api');
        $filters = $apiHelper->parseFilters($filters);
        try {
            foreach ($filters as $field => $value) {
                $collection->addFieldToFilter($field, $value);
            }
        } 
        catch (Mage_Core_Exception $e) {
            $this->_fault('filters_invalid', $e->getMessage());
        }
        $result = array();
        foreach ($collection as $project) {
            $result[] = $project->getData();
        }
        return $result;
    }
    /**
     * Add project
     * @access public
     * @param array $data
     * @return array
     */
    public function add($data){
        try {
            if (is_null($data)){
                throw new Exception(Mage::helper('portfolio')->__("Data cannot be null"));
            }
            $project = Mage::getModel('portfolio/project')
                ->setData((array)$data)
                ->save();
        } 
        catch (Mage_Core_Exception $e) {
            $this->_fault('data_invalid', $e->getMessage());
        } 
        catch (Exception $e) {
            $this->_fault('data_invalid', $e->getMessage());
        }
        return $project->getId();
    }

    /**
     * Change existing project information
     * @access public
     * @param int $projectId
     * @param array $data
     * @return bool
     */
    public function update($projectId, $data){
        $project = $this->_initProject($projectId);
        try {
            $project->addData((array)$data);
            $project->save();
        } 
        catch (Mage_Core_Exception $e) {
            $this->_fault('save_error', $e->getMessage());
        }

        return true;
    }
    /**
     * remove project
     * @access public
     * @param int $projectId
     * @return bool
     */
    public function remove($projectId){
        $project = $this->_initProject($projectId);
        try {
            $project->delete();
        } 
        catch (Mage_Core_Exception $e) {
            $this->_fault('remove_error', $e->getMessage());
        }
        return true;
    }
    /**
     * get info
     * @access public
     * @param int $projectId
     * @return array
     */
    public function info($projectId){
        $result = array();
        $project = $this->_initProject($projectId);
        $result = $project->getData();
        return $result;
    }
}

app/code/local/Easylife/Portfolio/Model/Project/Api/V2.php - mô hình để xử lý các yêu cầu Api v2

<?php
class Easylife_Portfolio_Model_Project_Api_V2 extends Easylife_Portfolio_Model_Project_Api{
    /**
     * Project info
     * @access public
     * @param int $projectId
     * @return object
     */
    public function info($projectId){
        $result = parent::info($projectId);
        $result = Mage::helper('api')->wsiArrayPacker($result);
        return $result;
    }
}

Đó là nó. Điều này sẽ cung cấp cho bạn một chức năng API cơ bản cho thực thể của bạn. Bạn có thể thêm các phương thức của mình tương tự như các phương thức đã được thêm hoặc sửa đổi chúng để phù hợp với nhu cầu của bạn.


Lưu ý : Những tệp này được tạo bằng Ultimate Module Creator . Tôi vừa thêm một số nhận xét trong các tệp xml.


Tôi đã cập nhật mã của mình và nó đang hiển thị trên danh sách SOAP / XML-RPC của tôi và được cấp quyền cho người dùng magento. Khi truy cập id phiên đang quay trở lại, nhưng phương thức được trả về lỗi exf SoapFault: [3] Đường dẫn api không hợp lệ. mã của tôi là: <? php $ proxy = new SoapClient (' localhost / ics / index.php / api / soap? wsdl' ); $ sessionId = $ proxy-> đăng nhập ('magento', 'magento @ 123'); echo "ID đăng nhập: $ sessionId"; $ result = $ proxy-> call ($ sessionId, 'mca.create', mảng ('param1' => 'Máy khách thử nghiệm')); echo $ result; phương thức là: hàm tạo công khai ($ stuId) {return $ stuId. 'Thông điệp tùy chỉnh của tôi' ;; } $ proxy-> endSession ($ sessionId);
Manoj Kumar

Tại sao tôi gặp lỗi nghiêm trọng: Uncaught SoapFault ngoại lệ: [VersionMismatch] Phiên bản sai ...
user1240207
Khi sử dụng trang web của chúng tôi, bạn xác nhận rằng bạn đã đọc và hiểu Chính sách cookieChính sách bảo mật của chúng tôi.
Licensed under cc by-sa 3.0 with attribution required.