Frage

Ich bin neu in Magento. Ich habe die benutzerdefinierte Magento -API erstellt, um der zu folgen Marius Anweisungen.

Mein Code ist:

App/Code/Lokal/Kurs/MCA/etc/api.xml - Die API -Deklarationsdatei.

 <?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/Lokal/Kurs/MCA/etc/wsdl.xml - WSDL -Teil für 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="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/Lokal/Kurs/MCA/etc/wsi.xml - Es ist ähnlich wie wsdl.xml, wird aber für WS-I-Komplettien verwendet

 <?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/Modell/MCA/API.PHP - Modell zum Umgang mit den API -V1 -Anforderungen (und einigen V2 -Anforderungen)

 <?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/Modell/MCA/API/V2.PHP - Modell zum Umgang mit den API -V2 -Anforderungen

 <?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;
    }

}

Es zeigt auch die SOAP/XML-RPC-Liste WSDL und ich habe Benutzername und API für den Zugriff auf die MCA-API erstellt.

enter image description here

enter image description here

Schließlich habe ich die My PHP -Datei aufgerufen, die einen Fehler zurückgibt.

Mein PHP -Code ist:

 <?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>';
}

Fehler ist:enter image description here

vielen Dank im Voraus.

War es hilfreich?

Lösung

Es ist eine Art Fehler, der zeigt, dass Magento die Methode Ihrer API nicht findet. Sie haben also ein Problem, wie Sie diese Methode in der api.xml definieren. Übrigens habe ich mehrere potenzielle Probleme gesehen. Siehe unten

  • Sie müssen die öffentliche Funktion nicht erneut erstellen create in deiner Klasse Course_Mca_Model_Mca_Api_V2 Weil Sie sich erstrecken Course_Mca_Model_Mca_Api Und dieser hat die gleiche Methode.
  • Ihre Methode Course_Mca_Model_Mca_Api::create Erwartet eine Zeichenfolge als Parameter und eine Zeichenfolge als zurückgegebener Wert. In Ihrer WSDL -Definition definieren Sie eine Art Array (das ohnehin schlecht definiert ist). Für Ihren Testzweck und um Ihnen eine Schritt -für -Schritt -Codierung zu ermöglichen, tun Sie:

Ersetzen Sie in 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>

Mit

<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>
  • Sie müssen auch die WSI.xml -Datei aktualisieren, wie Sie es für die WSDL.xml -Datei getan haben
  • In api.xml haben Sie vielleicht auch ein Problem. Machen Sie Folgendes:

Ersetzen

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

mit

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

Verwenden Sie dann in Ihrem Testcode die V2 -SOAP -Version

$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);

Andere Tipps

Das ist Dein Problem.
Im api.xml du hast das:

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

Das sollte sein

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

Wenn Sie genau das Beispiel ansehen, das ich gegeben habe hier, da war das in api.xml

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

Also das Tag unter dem <resources> sollte sein <[module]_[entity]>. In Ihrem Fall haben das Modul und die Entität denselben Namen mca, so sollte es sein <mca_mca>

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit magento.stackexchange
scroll top