2012-08-29 23 views
13

Tôi mới trong Magento và tôi muốn tạo phương pháp API v2 của riêng mình. tôi đã xây dựng một dự án đơn giản ...
Magento API: Xuất bản một phương pháp mới trong xà phòng V2

Mycompany 
    Mymodule 
     etc 
      api.xml 
      config.xml 
      wsdl.xml 
     Model 
      Api 
       V2.php 
      Api.php 


Đây là các tập tin chính ...
(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> 

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

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

(4) V2.php

<?php 
class Mycompany_Mymodule_Model_Api_V2 extends Mycompany_Mymodule_Model_Api 
{   

} 

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

Sử dụng Url sau, kết quả là:

.../test.php/ver = 1

version 1 
string 'This is the message : My message ....' (length=37) 
end test 

Đó là để nói: sử dụng xà phòng v1, phương pháp này công trinh!. ?

Nhưng nếu tôi sử dụng cuộc gọi xà phòng v2 ...

.../test.php/ver = 2 kết quả là:

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 


Vai trò có quyền truy cập vào tất cả tài nguyên api ...

Tôi không biết điều gì là sai? bất cứ ai có thể giúp tôi với vấn đề này? Có thể liên quan đến acl không? Cảm ơn trước !!!

+1

có thể bộ nhớ cache wsdl được bật hoặc PHP và các ứng dụng khách khác lưu vào bộ nhớ cache wsdl. để đảm bảo 'sudo rm -rf/tmp/wsdl *' chạy lệnh này trên thiết bị đầu cuối. –

Trả lời

1

Mã của bạn tuyệt vời! không có sai lầm nào trên

Theo như tôi biết (từ kinh nghiệm của tôi). ngoại lệ đó được hiển thị vì vị trí thẻ trên api.xml của bạn không khớp với hàm bạn gọi là hàm.


kiểm tra trên các tập tin

lõi \ Mage \ Catalog \ mẫu \ Sản phẩm \ Api.php

lõi \ Mage \ Catalog \ mẫu \ Sản phẩm \ Api \ V2.php

có một hàm có tên mục trên cả hai lớp.

lẽ bạn phải thêm mã của bạn trên api_v1 trên api_v2 lớp như thế này:

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

hoặc

có lẽ nó bộ nhớ cache.

Bộ nhớ cache mạnh mẽ vì đó là API V2. cố xóa bộ nhớ cache của bạn trên:

  1. admin -> system -> cache management -> clear magento cache.
  2. cố xóa bộ nhớ cache wsdl.xml trên /tmp/wsdl.xml (ẩn trên máy chủ của bạn) thử grep.
  3. xóa tất cả các tệp trên var/log/*
+2

Cảm ơn rất nhiều Josua.
Tôi đã giải quyết được sự cố. Đã xảy ra sự cố với tên của phương thức trong tệp wsdl và các vấn đề với đường dẫn trong tệp api.xml. –

7

Đó là vấn đề của đường dẫn.
Tôi đã giải quyết những vấn đề như sau ...
Cấu trúc thư mục của dự án của tôi là:

Mycompany-> 
    Mymodule-> 
     etc-> 
      api.xml 
      config.xml 
      wsdl.xml 
      wsi.xml 
     Model-> 
      Folder-> 
       Api.php 
       Api-> 
        V2.ph 

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

Chú ý đến các tên của phương thức api "myapimethod" và thư mục "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> 

Tên của phương pháp api phải: folderMethodname", trong đó "thư mục" là tên của thư mục theo "Model" nơi phương pháp api được khai báo .., trong trường hợp của chúng tôi: "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; 
     } 
} 

(4) V2.php

<?php 
class Mycompany_Mymodule_Model_Folder_Api_V2 extends Mycompany_Mymodule_Model_Folder_Api 
{ 
    //empty  
} 


(5) kiểm tra.php

$result = $client->folderMyapimethod($session,$message); 

dòng này cho thấy làm thế nào để gọi tới phương pháp api của tôi ...

Nó hoạt động!

+0

mã đẹp. nhưng đối với tôi nó không hoạt động: ( –

+0

@ArulJames Nó hoạt động nhưng có thể bạn đang chạy api trong chế độ tuân thủ WS-I. Trong trường hợp đó bạn cũng cần định nghĩa wsi.xml –

Các vấn đề liên quan