Magento API:在soap V2中发布一个新方法

Jua*_* R. 13 api soap magento

我是Magento的新手,我想创建一个自己的API v2方法.我已经建立了一个简单的项目......

Mycompany
    Mymodule
        etc
            api.xml
            config.xml
            wsdl.xml
        Model
            Api
                V2.php
            Api.php
Run Code Online (Sandbox Code Playgroud)


这些是主要文件...
(1)api.xml

<config>
    <api>
        <resources>
            <mymodule translate="title" module="mymodule">
                <title>mymodule</title>
                <model>mymodule/api</model>
                <methods>                    
                    <myapimethod translate="title" module="mymodule">
                        <title>myapimethod</title>
                        <acl>mymodule/myapimethod</acl>
                    </myapimethod>
                </methods>
            </mymodule>
        </resources>
        <v2>
            <resources_function_prefix>
                <mymodule>mymodule</mymodule>
            </resources_function_prefix>
        </v2>
        <acl>
            <resources>
                <mymodule translate="title" module="mymodule">
                    <title>Mymodule</title>
                    <sort_order>2000</sort_order>                    
                    <myapimethod translate="title" module="mymodule">
                        <title>myapimethod</title>
                    </myapimethod>  
                </mymodule>
            </resources>
        </acl>
    </api>
</config>
Run Code Online (Sandbox Code Playgroud)

(2)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/" />
        </schema>
    </types>
    <message name="myapimethodRequest">
        <part name="sessionId" type="xsd:string"/>
        <part name="message" type="xsd:string" />
    </message>
    <message name="myapimethodResponse">
        <part name="result" type="xsd:string" />
    </message>
    <portType name="{{var wsdl.handler}}PortType">
        <operation name="myapimethod">
            <documentation>this is an example of api method...</documentation>
            <input message="typens:myapimethodRequest" />
            <output message="typens:myapimethodResponse" />
        </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="myapimethod">
            <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>
Run Code Online (Sandbox Code Playgroud)

(3)Api.php

<?php
class Mycompany_Mymodule_Model_Api extends Mage_Api_Model_Resource_Abstract
{        
        public function myapimethod($sessionId, $message)
        {
            return "This is the message : ".$message;
        }
}
Run Code Online (Sandbox Code Playgroud)

(4)V2.php

<?php
class Mycompany_Mymodule_Model_Api_V2 extends Mycompany_Mymodule_Model_Api
{        

}
Run Code Online (Sandbox Code Playgroud)

(5)test.php

<?php
try {
    define("SOAP_WSDL",'http://localhost:8080/magento/index.php/api/?wsdl');
    define("SOAP_WSDL2",'http://localhost:8080/magento/index.php/api/v2_soap?wsdl=1');
    define("SOAP_USER","dtsSoapUser");
    define("SOAP_PASS","casares");

    if($_GET['ver'] == '2') {
        $client = new SoapClient(SOAP_WSDL2, array('trace' => 1,'cache_wsdl' => 0));
        echo "<br>version 2 <br>";
    }
    else {
        $client = new SoapClient(SOAP_WSDL,array('trace' => 1,'cache_wsdl' => 0));

        echo "<br>version 1 <br>";
    }
    $session = $client->login(SOAP_USER, SOAP_PASS);
    $result = array();

    try {
        if($_GET['ver'] == '2') {
             $result = $client->Myapimethod($session, "My message....");
             var_dump ( $result);        
        } else {            
            $result= $client->call($session, 'mymodule.myapimethod', array($session, "My message ...."));
            var_dump($result);
        }
    } catch (SoapFault $exception) {
        echo 'EXCEPTION='.$exception;
    }

    echo "<br>end test<br>";
} catch (Exception $e){
    echo var_dump($e);
    throw $e;
}   
?>
Run Code Online (Sandbox Code Playgroud)

使用以下Url,结果是:

.../test.php的/?版本= 1

version 1
string 'This is the message : My message ....' (length=37)
end test
Run Code Online (Sandbox Code Playgroud)

这就是说:使用Soap v1,该方法有效!

但如果我使用soap v2电话......

.../test.php /?ver = 2结果是:

version 2
EXCEPTION=SoapFault exception: [3] Invalid api path. in C:\wamp\www\PruebasPHP\test.php:22 Stack trace: #0 C:\wamp\www\PruebasPHP\test.php(22): SoapClient->__call('Myapimethod', Array) #1 C:\wamp\www\PruebasPHP\test.php(22): SoapClient->Myapimethod('b9e1e8d15a61398...', 'My message....') #2 {main}
end test
Run Code Online (Sandbox Code Playgroud)


该角色可以访问所有api资源......

我不知道出了什么问题?任何人都可以帮我解决这个问题吗?可以是任何与acl相关的东西吗?提前致谢!!!

Jua*_* R. 7

这是路径问题.
我已经解决了以下问题......
我的项目的文件夹结构是:

Mycompany->
    Mymodule->
        etc->
            api.xml
            config.xml
            wsdl.xml
            wsi.xml
        Model->
            Folder->
                Api.php
                Api->
                    V2.ph
Run Code Online (Sandbox Code Playgroud)

(1)api.xml

<?xml version="1.0"?>
<config>
    <api>
        <resources>
            <mymodule_folder translate="title" module="mymodule">
                <title>mymodule</title>
                <!-- acl>mymodule/api</acl>-->
                <model>mymodule/folder_api</model>
                <acl>mymodule/folder</acl>
                <methods>                    
                    <myapimethod translate="title" module="mymodule">
                        <title>myapimethod</title>
                        <acl>mymodule/folder/myapimethod</acl>
                    </myapimethod> 
                </methods>
            </mymodule_folder>
        </resources>
        <resources_alias>
            <folder>mymodule_folder</folder>
         </resources_alias>

        <v2>
            <resources_function_prefix>
                <folder>folder</folder>
            </resources_function_prefix>
        </v2>
        <acl>
            <resources>
                <mymodule translate="title" module="mymodule">
                    <title>mymodule</title>
                    <sort_order>1</sort_order>
                    <folder translate="title" module="mymodule">
                        <title>Folder</title>
                        <sort_order>2000</sort_order>                    
                <myapimethod translate="title" module="mymodule">
                            <title>myapimethod</title>
                        </myapimethod>  
                    </folder>
             </mymodule>
            </resources>
        </acl>
    </api>
</config>
Run Code Online (Sandbox Code Playgroud)

注意api方法"myapimethod"的名称和文件夹"Model\Folder".

(2)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/" />
        </schema>
    </types>
    <message name="folderMyapimethodRequest">
        <part name="sessionId" type="xsd:string"/>
        <part name="message" type="xsd:string" />
    </message>
    <message name="folderMyapimethodResponse">
        <part name="result" type="xsd:string" />
    </message>
    <portType name="{{var wsdl.handler}}PortType">
        <operation name="folderMyapimethod">
            <documentation>this is an example of api method...</documentation>
            <input message="typens:folderMyapimethodRequest" />
            <output message="typens:folderMyapimethodResponse" />
        </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="folderMyapimethod">
            <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> 
Run Code Online (Sandbox Code Playgroud)

api方法的名称必须是:folderMethodname",其中"folder"是声明api方法的"Model"下文件夹的名称..在我们的例子中:"folderMyapimethod"
(3)api.php

<?php
class Mycompany_Mymodule_Model_Folder_Api extends Mage_Api_Model_Resource_Abstract
{        
        public function myapimethod( $message)
        {
            return "This is the message: ".$message;
        }
}
Run Code Online (Sandbox Code Playgroud)

(4)V2.php

<?php
class Mycompany_Mymodule_Model_Folder_Api_V2 extends Mycompany_Mymodule_Model_Folder_Api
{   
     //empty     
}
Run Code Online (Sandbox Code Playgroud)


(5)test.php

$result = $client->folderMyapimethod($session,$message);
Run Code Online (Sandbox Code Playgroud)

这一行显示了如何调用我的api方法......

有用!


Jos*_*ano 1

你的代码非常好!没有错误

据我所知(根据我的经验)。显示该异常是因为您在 api.xml 上的标记位置与您调用的函数不匹配。


检查文件

核心\法师\目录\模型\产品\Api.php

核心\法师\目录\模型\产品\Api\V2.php

这两个类上都有一个名为items的函数。

也许您必须在 api_v2 类的 api_v1 上添加代码,如下所示:

<?php
class Mycompany_Mymodule_Model_Api_V2 extends Mycompany_Mymodule_Model_Api
{        
    public function myapimethod($sessionId, $message)
    {
        return "This is the message : ".$message;
    }
}
Run Code Online (Sandbox Code Playgroud)

或者

也许是缓存

缓存很强大,因为它是 API V2。尝试清除缓存:

  1. 管理->系统->缓存管理->清除magento缓存。
  2. 尝试清除 /tmp/wsdl.xml 上的 wsdl.xml 缓存(隐藏在您的服务器上)尝试grep它。
  3. 删除 var/log/* 上的所有文件

  • 非常感谢约书亚。&lt;br&gt;我已经解决了这个问题。wsdl 文件中的方法名称存在问题,api.xml 中的路径也存在问题。 (2认同)