我正在创建我的第一个自定义API,并且正在努力。我无法使用V2 API称呼它(或将其用作.NET中的服务参考)。

<?php
$host = "127.0.0.1/magento/index.php";
$client = new SoapClient("http://" .$host. "/api/v2_soap?wsdl=1", array('trace' => 1));
$apiuser= "test";
$apikey = "pass";

$data = array(0 => array('date'=>'11-11-2014 10:12:25','operation'=>'PRICES9','successful'=>'0', 'requeue'=>'0'));

try { 
    $sessionId= $client->login($apiuser, $apikey);
$result = array();

$result = $client->syncreportsSyncCreate($sessionId, $data);
    print_r($result);

}
catch (Exception $e){
    echo var_dump($e);
    throw $e;
}

我遇到了这个错误:

" Fatal error: SOAP-ERROR: Parsing WSDL: Couldn't load from 'http://127.0.0.1/magento/index.php/api/v2_soap?wsdl=1' : failed to load external entity"

据我了解,我的自定义API WSDL.xml/wsi.xml不正确。

谁能看WSDL.xml/wsi.xml文件?还是要告诉我在哪里可以找到自定义API的正确示例?

这是我现在拥有的:

文件夹结构:

Mycompany
    Syncreports
      etc
        config.xml
        api.xml
        wsi.xml
        wsdl.xml
      Model
        Sync
          Api
            V2.php
          Api.php

/app/code/local/mycompany/syncreports/etc/api.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Mycompany_Syncreports>
          <version>1.0.0</version>
        </Mycompany_Syncreports>
    </modules>
  <api>
      <resources>
        <syncreports_sync translate="title" module="mycompany_syncreports">
            <title>Synchronization API</title>
            <model>syncreports/sync_api</model>
            <acl>syncreports/sync</acl>
            <methods>
                <info translate="title" module="mycompany_syncreports">
                    <title>Retrieve synchronization info</title>
                    <acl>syncreports/sync/info</acl>
                    <method>info</method>
                </info>
            </methods>
            <faults module="mycompany_syncreports">
                <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>
        </syncreports_sync>
    </resources>
    <resources_alias>
        <sync>syncreports_sync</sync>
    </resources_alias>
    <v2>
        <resources_function_prefix>
            <sync>syncreportsSync</sync>
        </resources_function_prefix>
    </v2>
    <acl>
        <resources>
            <syncreports translate="title" module="mycompany_syncreports">
                <title>SyncReports</title>
                <sync translate="title" module="mycompany_syncreports">
                    <title>Synchronization</title>
                    <sort_order>110</sort_order>
                    <info translate="title" module="mycompany_syncreports">
                        <title>Info</title>
                    </info>
                </sync>
            </syncreports>
        </resources>
    </acl>
  </api>
</config>

/app/code/local/mycompany/syncreports/etc/config.xml

<?xml version="1.0" ?>
<config>
    <modules>
        <Mycompany_Syncreports>
            <version>1.0.0</version>
        </Mycompany_Syncreports>
    </modules>

    <global>            
        <helpers>
            <syncreports>
                <class>Mycompany_Syncreports_Helper</class>
            </syncreports>
        </helpers>

        <models>
            <syncreports>
                <class>Mycompany_Syncreports_Model</class>
                <resourceModel>syncreports_resource</resourceModel>
            </syncreports>
            <syncreports_resource>
                <class>Mycompany_Syncreports_Model_Resource</class>
                <entities>
                    <syncreports>
                        <table>synchronization_reports</table>
                    </syncreports>
                </entities>
            </syncreports_resource>
        </models>

        <resources>
            <syncreports_setup>
                <setup>
                    <module>Mycompany_Syncreports</module>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </syncreports_setup>
            <syncreports_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </syncreports_write>
            <syncreports_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </syncreports_read>
        </resources>
    </global>   
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <modules>
                        <syncreports before="Mage_Adminhtml">Mycompany_Syncreports_Adminhtml</syncreports>
                    </modules>
                </args>
            </adminhtml>
        </routers>
    </admin>

    <frontend>
        <routers>
            <syncreports>
                <use>standard</use>
                <args>
                    <module>Mycompany_Syncreports</module>
                    <frontName>syncreports</frontName>
                </args>
            </syncreports>
        </routers>
    </frontend>

    <adminhtml>
        <layout>
            <updates>
                <syncreports>
                    <file>syncreports.xml</file>
                </syncreports>
            </updates>
        </layout>
    </adminhtml>    
</config>

/app/code/local/mycompany/syncreports/etc/wsi.xml

<?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="syncreportsSyncItemsEntityArray">
                <xsd:sequence>
                    <xsd:element maxOccurs="unbounded" name="complexObjectArray" type="typens:syncreportsSyncItemsEntity" />
                </xsd:sequence>
            </xsd:complexType>
            <xsd:complexType name="syncreportsSyncItemsEntity">
                <xsd:sequence>
                    <xsd:element name="date" type="xsd:date" />
                    <xsd:element name="operation" type="xsd:string" />
                    <xsd:element name="successful" type="xsd:int" />
                    <xsd:element name="requeue" type="xsd:int" />
                </xsd:sequence>
            </xsd:complexType>

            <xsd:complexType name="syncreportsSyncCreateEntity">
                <xsd:sequence>
                    <xsd:element name="date" type="xsd:date" />
                    <xsd:element name="operation" type="xsd:string" />
                    <xsd:element name="successful" type="xsd:int" />
                    <xsd:element name="requeue" type="xsd:int" />
                </xsd:sequence>
            </xsd:complexType>


            <xsd:element name="syncreportsSyncCreateRequestParam">
                <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:syncreportsSyncItemsEntity" />
                    </xsd:sequence>
                 </xsd:complexType>
            </xsd:element>
            <xsd:element name="syncreportsSyncCreateResponseParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="result" type="typens:syncreportsSyncItemsEntityArray" />
                    </xsd:sequence>
                 </xsd:complexType>
            </xsd:element>


            <xsd:element name="syncreportsSyncItemsRequestParam">
                <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:syncreportsSyncItemsEntity" />
                    </xsd:sequence>
                 </xsd:complexType>
            </xsd:element>
            <xsd:element name="syncreportsSyncItemsResponseParam">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:element minOccurs="1" maxOccurs="1" name="result" type="typens:syncreportsSyncItemsEntityArray" />
                    </xsd:sequence>
                 </xsd:complexType>
            </xsd:element>

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


    <wsdl:message name="syncreportsSyncItemsRequest">
        <wsdl:part name="parameters" element="typens:syncreportsSyncItemsRequestParam" />
    </wsdl:message>
    <wsdl:message name="syncreportsSyncItemsResponse">
        <wsdl:part name="parameters" element="typens:syncreportsSyncItemsResponseParam" />
    </wsdl:message>


    <wsdl:message name="syncreportsSyncCreateRequest">
        <wsdl:part name="parameters" element="typens:syncreportsSyncCreateRequestParam" />
    </wsdl:message>
    <wsdl:message name="syncreportsSyncItemsResponse">
        <wsdl:part name="parameters" element="typens:syncreportsSyncCreateResponseParam" />
    </wsdl:message>


    <wsdl:portType name="{{var wsdl.handler}}PortType">
        <wsdl:operation name="syncreportsSyncItems">
            <wsdl:documentation>Retrieve project info</wsdl:documentation>
            <wsdl:input message="typens:syncreportsSyncItemsRequest" />
            <wsdl:output message="typens:syncreportsSyncItemsResponse" />
        </wsdl:operation>
    </wsdl:portType>


    <wsdl:portType name="{{var wsdl.handler}}PortType">
        <wsdl:operation name="syncreportsSyncCreate">
            <wsdl:documentation>Retrieve project info</wsdl:documentation>
            <wsdl:input message="typens:syncreportsSyncCreateRequest" />
            <wsdl:output message="typens:syncreportsSyncCreateResponse" />
        </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="syncreportsSyncItems">
            <soap:operation soapAction="" />
            <wsdl:input>
                <soap:body use="literal" />
            </wsdl:input>
            <wsdl:output>
                <soap:body use="literal" />
            </wsdl:output>
        </wsdl:operation>

        <wsdl:operation name="syncreportsSyncCreate">
            <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/mycompany/syncreports/etc/wsdl.xml

<?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="syncreportsSyncItemsEntity">
                <all>
                    <element name="date" type="xsd:date" minOccurs="1" />
                    <element name="operation" type="xsd:string" minOccurs="1" />
                    <element name="successful" type="xsd:int" minOccurs="1" />
                    <element name="requeue" type="xsd:int" minOccurs="1" />
                </all>
            </complexType>
            <complexType name="syncreportsSyncItemsEntityArray">
                <complexContent>
                    <restriction base="soapenc:Array">
                        <attribute ref="soapenc:arrayType" wsdl:arrayType="typens:syncreportsSyncItemsEntity[]" />
                    </restriction>
                </complexContent>
            </complexType>

            <complexType name="syncreportsSyncCreateEntity">
                <all>
                    <element name="date" type="xsd:date" minOccurs="1" />
                    <element name="operation" type="xsd:string" minOccurs="1" />
                    <element name="successful" type="xsd:int" minOccurs="1" />
                    <element name="requeue" type="xsd:int" minOccurs="1" />
                </all>
            </complexType>

        </schema>
    </types>
    <message name="syncreportsSyncItemsRequest">
        <part name="sessionId" type="xsd:string" />

    </message>
    <message name="syncreportsSyncItemsResponse">
        <part name="result" type="typens:syncreportsSyncItemsEntityArray" />
    </message>
    <message name="syncreportsSyncCreateRequest">
        <part name="sessionId" type="xsd:string" />
        <part name="data" type="xsd:syncreportsSyncCreateEntity" />
    </message>
    <message name="syncreportsSyncCreateResponse">
        <part name="result" type="typens:syncreportsSyncCreateEntityArray" />
    </message>
    <portType name="{{var wsdl.handler}}PortType">
        <operation name="syncreportsSyncItems">
            <documentation>Retrieve sync info</documentation>
            <input message="typens:syncreportsSyncItemsRequest" />
            <output message="typens:syncreportsSyncItemsResponse" />
        </operation>
        <operation name="syncreportsSyncCreate">
            <documentation>Retrieve sync info</documentation>
            <input message="typens:syncreportsSyncCreateRequest" />
            <output message="typens:syncreportsSyncCreateResponse" />
        </operation>
    </portType>
    <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="syncreportsSyncItems">
            <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="syncreportsSyncCreate">
            <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>
    <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/mycompany/syncreports/model/sync/api.php

<?php
  class Mycompany_Syncreports_Model_Sync_Api extends Mage_Api_Model_Resource_Abstract
  {
      public function info($data){
            //some actions with $data object
            return "This is the message: ";
      }
}

/App/code/local/mycompany/syncreports/model/sync/api/v2.php

<?php
class Mycompany_Syncreports_Model_Sync_Api_V2 extends Mycompany_Syncreports_Model_Sync_Api{

  public function create($data){
    //some actions with $data object
    return "This is the message: ";
  }
}
有帮助吗?

解决方案

我已经测试了您的WS-I合规性禁用,这是结果。
WSDL是正确的,并且(几乎)起作用。
首先在您的 config.xml.

    <helpers>
        <syncreports>
            <class>Mycompany_Syncreports_Helper</class>
        </syncreports>
    </helpers>

为此:

    <helpers>
        <mycompany_syncreports>
            <class>Mycompany_Syncreports_Helper</class>
        </mycompany_syncreports>
    </helpers>

下一:我在你的 api.xml 你只宣布 info 方法,但在 wsdl 您还有其他2种方法 syncreportsSyncCreate, syncreportsSyncCreate.
将这些方法添加到 api.xml 否则,您将获得无效的路径错误消息。

            <methods>
                <info translate="title" module="mycompany_syncreports">
                    <title>Retrieve synchronization info</title>
                    <acl>syncreports/sync/info</acl>
                    <method>info</method>
                </info>
                <create translate="title" module="mycompany_syncreports">
                    <title>Create synchronization</title>
                    <acl>syncreports/sync/create</acl>
                    <method>create</method>
                </create>
                <items translate="title" module="mycompany_syncreports">
                    <title>List items</title>
                    <acl>syncreports/sync/list</acl>
                    <method>items</method>
                </items>
            </methods>

还将方法添加到您的API模型(如果需要的话,V2)。
现在它可以工作。
这就是我发送的:

<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:Magento">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:syncreportsSyncCreate soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
         <sessionId xsi:type="xsd:string">SESSION KEY HERE</sessionId>
      </urn:syncreportsSyncCreate>
   </soapenv:Body>
</soapenv:Envelope>

这就是我得到的。

<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="urn:Magento" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
   <SOAP-ENV:Body>
      <ns1:syncreportsSyncCreateResponse>
         <result xsi:type="xsd:string">This is the message:</result>
      </ns1:syncreportsSyncCreateResponse>
   </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

所以我猜该方法有效。

当我启用WS-I合规性时,我不会从任何电话中获得任何结果。甚至不是来自 login. 。如果我发现一些东西,我将编辑答案。但是您至少可以测试WS-I合规性。

编辑
我想我发现了你的问题 wsi 还文件。
您已经声明了类型 syncreportsSyncItemsResponse 两次和声明 syncreportsSyncCreateResponse 不见了。我认为这是一个副本/粘贴/替换错误(您忘记了替换)。
更改此:

<wsdl:message name="syncreportsSyncItemsResponse">
    <wsdl:part name="parameters" element="typens:syncreportsSyncCreateResponseParam" />
</wsdl:message>

为此:

<wsdl:message name="syncreportsSyncCreateResponse">
    <wsdl:part name="parameters" element="typens:syncreportsSyncCreateResponseParam" />
</wsdl:message>

旁注...您可能还想更改 <xsd:element name="syncreportsSyncCreateResponseParam"> 适合该方法的响应类型。现在您声明您返回 array, ,但最后我认为你会返回 boolean 或字符串。

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