質問

私はMagentoが初めてです。私はカスタムMagento APIを作成しました マリウスの指示.

私のコードは次のとおりです。

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コンフィランスに使用されています

 <?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を表示しており、MCA APIにアクセスする許可を与えられるようにユーザー名と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帰属
所属していません magento.stackexchange
scroll top