Domanda

Sono nuovo di Magento. Ho creato il costume Magento Api di seguire il istruzioni Marius .

il mio codice è:

app / code / local / Corso / Mca / etc / api.xml -. Il file di dichiarazione 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 / Corso / Mca / etc / wsdl.xml - parte WSDL per 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 / local / Corso / Mca / etc / wsi.xml - è simile a wsdl.xml ma è utilizzato per WS-I complience

 <?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 / Corso / Mca / Modello / Mca / Api.php - modello per la gestione delle richieste API v1 (e alcune richieste 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 / Corso / Mca / Modello / Mca / Api / V2.php - modello per la gestione delle richieste 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;
    }

}

Si sta mostrando la lista SOAP / XML-RPC e WSDL e il nome utente e api ho creato per il permesso di accesso alla Mca Api.

entrare descrizione dell'immagine qui

entrare descrizione dell'immagine qui

Finalmente ho invocando il mio file php è restituire un errore.

il mio codice 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>';
}

L'errore è: entrare descrizione dell'immagine qui

grazie in anticipo.

È stato utile?

Soluzione

E 'un genere di errore che dimostra che Magento non trova il metodo del vostro API. In modo da avere un problema come si definisce questo metodo nel api.xml. Tra l'altro ho visto diversi problemi potenziali. Vedi sotto

  • Non c'è bisogno di creare di nuovo la funzione create pubblica nella classe Course_Mca_Model_Mca_Api_V2 perché si estende Course_Mca_Model_Mca_Api e questo ha lo stesso metodo.
  • Il tuo metodo Course_Mca_Model_Mca_Api::create si aspetta una stringa come parametro e una stringa come un valore restituito, nella definizione WSDL, si definisce una sorta di array (che è comunque mal definito). Per il vostro scopo di test e per consentire di un passo per passo di codifica, do:

Sostituire 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>

Con

<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>
  • Si dovrà aggiornare anche il file wsi.xml come avete fatto per il file wsdl.xml
  • In api.xml, avete forse qualche problema troppo, procedere come segue:

Sostituire

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

con

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

Poi, nel tuo codice di prova, utilizzare il V2 Sapone 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);

Altri suggerimenti

Questo è il tuo problema.
In api.xml avete questo:

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

Questo dovrebbe essere

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

Se si guarda da vicino l'esempio che ho dato qui , c'era questo in api.xml

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

Così il tag sotto il <resources> dovrebbe essere <[module]_[entity]>. Nel tuo caso il modulo e l'entità hanno lo stesso nome mca, quindi dovrebbe essere <mca_mca>

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a magento.stackexchange
scroll top