我是Magento的新手。我创建了自定义的Magento API以遵循 Marius说明.

我的代码是:

app/code/local/course/mca/etc/api.xml- API声明文件。

 <?xml version="1.0"?>
<config>
    <api>
        <resources>
            <mca_mca translate="title" module="mca">
                <title>Mca API</title>
                <model>mca/mca_api</model>
                <acl>mca/mca</acl><!-- acl resource alias -->
                <methods><!-- definne the methods -->

                    <create translate="title" module="mca"><!-- add Mca -->
                        <title>Add Mca</title>
                        <acl>mca/mca/create</acl>
                    </create>

                    <info translate="title" module="mca"><!-- project details -->
                        <title>Retrieve project info</title>
                        <acl>mca/mca/info</acl>
                    </info>

                </methods>
                <faults module="mca"><!-- errors that might appear-->
                    <mca_not_exists>
                        <code>101</code>
                        <message>Requested Mca does not exist.</message>
                    </mca_not_exists>
                    <invalid_data>
                        <code>102</code>
                        <message>Provided data is invalid.</message>
                    </invalid_data>
                    <save_error>
                        <code>103</code>
                        <message>Error while saving Mca. Details in error message.</message>
                    </save_error>
                    <remove_error>
                        <code>104</code>
                        <message>Error while removing Mca. Details in error message.</message>
                    </remove_error>
                </faults>
            </mca_mca>
        </resources>
        <resources_alias>
            <mca>mca_mca</mca>
        </resources_alias>
        <v2>
            <resources_function_prefix>
                <mca>mcaMca</mca>
            </resources_function_prefix>
        </v2>
        <acl><!-- acl definition -->
            <resources>
                <mca translate="title" module="mca">
                    <title>mca</title>
                    <mca translate="title" module="mca">
                        <title>Students</title>
                        <sort_order>110</sort_order>

                        <create translate="title" module="mca">
                            <title>Create</title>
                        </create>

                        <info translate="title" module="mca">
                            <title>Info</title>
                        </info>

                    </mca>
                </mca>
            </resources>
        </acl>
    </api>
</config>

App/code/local/course/mca/etc/wsdl.xml -V2的WSDL部分

 <?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="mcaMcaCreateEntity"><!-- define type for add project -->
                <all>
                    <element name="firstname" type="xsd:string" minOccurs="1" />
                    <element name="lastname" type="xsd:string" minOccurs="0" />
                    <element name="telephone" type="xsd:string" minOccurs="0" />

                </all>
            </complexType>

            <complexType name="mcaMcaInfoEntity"><!-- define type for retrieve info -->
                <all>
                    <element name="mca_id" type="xsd:string" minOccurs="1" />
                    <element name="firstname" type="xsd:string" minOccurs="1" />
                    <element name="lastname" type="xsd:string" minOccurs="0" />
                    <element name="telephone" type="xsd:string" minOccurs="0" />

                    <element name="created_time" type="xsd:string" minOccurs="1" />
                    <element name="updated_time" type="xsd:string" minOccurs="1" />
                </all>
            </complexType>

        </schema>
    </types>
    <!--[+] define messages -->

    <message name="mcaMcaCreateRequest">
        <part name="sessionId" type="xsd:string" />
        <part name="data" type="xsd:string"  />
    </message>
    <message name="mcaMcaCreateResponse">
        <part name="result" type="xsd:string"/>
    </message>

    <message name="mcaMcaInfoRequest">
        <part name="sessionId" type="xsd:string" />
        <part name="projectId" type="xsd:string" />
    </message>
    <message name="mcaMcaInfoResponse">
        <part name="result" type="typens:mcaMcaInfoEntity" />
    </message>
    <!--[-] define messages -->
    <!--[+] define portTypes -->
    <portType name="{{var wsdl.handler}}PortType">
        <operation name="mcaMcaCreate">
            <documentation>Create the Mca</documentation>
            <input message="typens:mcaMcaCreateRequest" />
            <output message="typens:mcaMcaCreateResponse" />
        </operation>

        <operation name="mcaMcaInfo">
            <documentation>Retrieve MCA info</documentation>
            <input message="typens:mcaMcaInfoRequest" />
            <output message="typens:mcaMcaInfoResponse" />
        </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="mcaMcaCreate">
            <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="mcaMcaInfo">
            <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/course/mca/etc/wsi.xml - 它类似于wsdl.xml,但用于ws-i compriency

 <?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="mcaMcaCreateEntity">
                <xsd:sequence>
                    <xsd:element name="firstname" type="xsd:string" />
                    <xsd:element name="lastname" type="xsd:string" />
                    <xsd:element name="telephone" type="xsd:string" />
                </xsd:sequence>
            </xsd:complexType>

            <xsd:element name="mcaMcaCreateRequestParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="sessionId" type="xsd:string" />
                        <xsd:element minOccurs="1" maxOccurs="1" name="data" type="xsd:string" />
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>

            <xsd:element name="mcaMcaCreateResponseParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="result" type="xsd:string" />
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>

            <xsd:complexType name="mcaMcaInfoEntity">
                <xsd:sequence>
                    <xsd:element name="mca_id" type="xsd:string" />
                    <xsd:element name="firstname" type="xsd:string" />
                    <xsd:element name="lastname" type="xsd:string" />
                    <xsd:element name="telephone" type="xsd:string" />

                    <xsd:element name="created_time" type="xsd:string" />
                    <xsd:element name="updated_time" type="xsd:string" />
                </xsd:sequence>
            </xsd:complexType>

            <xsd:element name="mcaMcaInfoRequestParam">
                <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="mcaMcaInfoResponseParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="result" type="typens:mcaMcaInfoEntity" />
                    </xsd:sequence>
                </xsd:complexType>
            </xsd:element>

        </xsd:schema>
    </wsdl:types>

    <wsdl:message name="mcaMcaCreateRequest">
        <wsdl:part name="parameters" element="typens:mcaMcaCreateRequestParam" />
    </wsdl:message>
    <wsdl:message name="mcaMcaCreateResponse">
        <wsdl:part name="parameters" element="typens:mcaMcaCreateResponseParam"/>
    </wsdl:message>

    <wsdl:portType name="{{var wsdl.handler}}PortType">

        <wsdl:operation name="mcaMcaCreate">
            <wsdl:documentation>Add Mca</wsdl:documentation>
            <wsdl:input message="typens:mcaMcaCreateRequest" />
            <wsdl:output message="typens:mcaMcaCreateResponse" />
        </wsdl:operation>

        <wsdl:message name="mcaMcaInfoRequest">
            <wsdl:part name="parameters" element="typens:mcaMcaInfoRequestParam" />
        </wsdl:message>
        <wsdl:message name="mcaMcaInfoResponse">
            <wsdl:part name="parameters" element="typens:mcaMcaInfoResponseParam" />
        </wsdl:message>

    </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="mcaMcaCreate">
            <soap:operation soapAction="" />
            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>

        <wsdl:operation name="mcaMcaInfo">
            <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>

App/code/local/course/MCA/Model/MCA/API.PHP-用于处理API V1请求的型号(以及一些V2请求)

 <?php
class Course_Mca_Model_Mca_Api extends Mage_Api_Model_Resource_Abstract
{

    /**
     * init mca students
     * @access protected
     * @param $mcaId
     * @return Course_Mca_Model_Mca
     */
    protected function _initMca($mcaId){
        $mca = Mage::getModel('mca/mca')->load($mcaId);
        if (!$mca->getId()) {
            $this->_fault('mca_not_exists');
        }
        return $mca;
    }

    /**
     * Add Students
     * @access public
     * @param array $data
     * @return array
     */
    public function create($data){
        try {
            if (is_null($data)){
                throw new Exception(Mage::helper('mca')->__("Data cannot be null"));
            }
            $mca = Mage::getModel('mca/mca')->setData((array)$data)->save();
        }
        catch (Mage_Core_Exception $e) {
            $this->_fault('data_invalid', $e->getMessage());
        }
        return $mca->getId();
    }

    /**
     * get info
     * @access public
     * @param int $mcaId
     * @return array
     */
    public function info($mcaId){
        $result = array();
        $mca = $this->_initMca($mcaId);
        $result = $mca->getData();
        return $result;
    }

    public function items($filters)
    {
    }

    public function update($stuId, $stuData)
    {
    }

    public function delete($stuId)
    {
    }
}

App/code/local/course/mca/model/mca/api/v2.php-用于处理API V2请求的型号

 <?php
class Course_Mca_Model_Mca_Api_V2 extends Course_Mca_Model_Mca_Api
{

    /**
     * Add Students
     * @access public
     * @param array $data
     * @return array
     */
    public function create($data){
        try {
            if (is_null($data)){
                throw new Exception(Mage::helper('mca')->__("Data cannot be null"));
            }
            $mca = parent::create($data);
            $mca = Mage::helper('api')->wsiArrayPacker($mca);
        }
        catch (Mage_Core_Exception $e) {
            $this->_fault('data_invalid', $e->getMessage());
        }
        return $mca->getId();
    }

    /**
     * Project info
     * @access public
     * @param int $mcaId
     * @return object
     */
    public function info($mcaId){
        $result = parent::info($mcaId);
        $result = Mage::helper('api')->wsiArrayPacker($result);
        return $result;
    }

}

它正在显示SOAP/XML-RPC列表以及WSDL,我创建了用户名和API,以允许访问MCA API。

enter image description here

enter image description here

最后,我调用我的PHP文件,它正在返回错误。

我的PHP代码是:

 <?php 
//$proxy = new SoapClient('http://localhost/ics/index.php/api/soap?wsdl'); //edit the address and put the url to your magento here
//$proxy = new SoapClient('http://localhost/ics/index.php/api/?wsdl');
try
{

$proxy = new SoapClient('http://localhost/ics/index.php/api/v2_soap?wsdl');
$sessionId = $proxy->login("magento","magento@123"); // put in the info for your user here
echo "Login ID : $sessionId";

echo '<br/>';

//$params = array('param1'=>'Dotnet','param2'=>'Php','param3'=>'9676588589');

//v1
//$result = $proxy->call($sessionId, 'mca.create',$params);

//v2
$result = $proxy->mcaMcaCreate($sessionId, 'Dotnet','Php','9676588589');

print_r($result); 


$proxy->endSession($sessionId);
} catch (Exception $e) {
    echo '<h1>Error</h1>';
    echo '<p>' . $e->getMessage() . '</p>';
}

错误是:enter image description here

提前致谢。

有帮助吗?

解决方案

这是一种错误,表明Magento找不到API的方法。因此,您有一个问题,如何在API.xml中定义此方法。顺便说一句,我看到了几个潜在的问题。见下文

  • 您无需再次创建公共功能 create 在你的班上 Course_Mca_Model_Mca_Api_V2 因为你扩展 Course_Mca_Model_Mca_Api 而且这个方法具有相同的方法。
  • 您的方法 Course_Mca_Model_Mca_Api::create 在WSDL定义中,您期望字符串作为参数和字符串作为返回的值,您定义了一种数组(无论如何,这是错误定义的)。为了您的测试目的,并允许您进行逐步编码,请执行:

替换为wsdl.xml

<message name="mcaMcaCreateRequest">
    <part name="sessionId" type="xsd:string" />
    <part name="data" type="typens:mcaMcaCreateEntity" />
</message>
<message name="mcaMcaCreateResponse">
    <part name="result" type="xsd:int"/>
</message>

<message name="mcaMcaCreateRequest">
    <part name="sessionId" type="xsd:string" />
    <part name="data" type="xsd:string" />
</message>
<message name="mcaMcaCreateResponse">
    <part name="result" type="xsd:string"/>
</message>
  • 您还必须像wsdl.xml文件一样更新wsi.xml文件
  • 进入api.xml,您也许也有一些问题,请执行以下操作:

代替

    <resources_alias>
        <mca>mca_mca</mca>
    </resources_alias>
    <v2>
        <resources_function_prefix>
            <mca>mcamca</mca>
        </resources_function_prefix>
    </v2>

    <resources_alias>
        <mca>mca_mca</mca>
    </resources_alias>
    <v2>
        <resources_function_prefix>
            <mca>mcaMca</mca>
        </resources_function_prefix>
    </v2>

然后在您的测试代码中使用V2 SOAP版本

$proxy = new SoapClient('http://localhost/ics/index.php/api/v2_soap?wsdl');
$sessionId = $proxy->login('magento', 'magento@123');
$result = $proxy->mcaMcaCreate($sessionId, 'my text test');
print_r($result);

其他提示

这是你的问题。
api.xml 你有这个:

<resources>
    <mca translate="title" module="mca">
        <title>Mca API</title>
        ....

应该是

<resources>
    <mca_mca translate="title" module="mca">
        <title>Mca API</title>
        ....

如果您仔细观察我给的示例 这里, ,在api.xml中有这个

<api>
    <resources>
        <portfolio_project translate="title" module="portfolio">

所以标签 <resources> 应该 <[module]_[entity]>. 。在您的情况下,模块和实体具有相同的名称 mca, ,所以应该是 <mca_mca>

许可以下: CC-BY-SA归因
scroll top