API personalizado V2 no funciona
-
16-10-2019 - |
Pregunta
Estoy creando mi primera API personalizada y estoy luchando mucho con ella. No puedo llamarlo con V2 API (o usarlo como referencia de servicio en .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;
}
Recibo este error:
" 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"
Por lo que entendí, mi API personalizada wsdl.xml/wsi.xml no es correcta.
¿Alguien puede echar un vistazo a los archivos wsdl.xml/wsi.xml? ¿O tal vez para decir dónde podría encontrar un ejemplo correcto y de trabajo de API personalizada?
Esto es lo que tengo por ahora:
Estructura de carpeta:
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: ";
}
}
Solución
He probado su discapacitado para el cumplimiento de WS-I y aquí están los resultados.
El WSDL es correcto y (casi) funciona.
Primero cambia esto en tu config.xml
.
<helpers>
<syncreports>
<class>Mycompany_Syncreports_Helper</class>
</syncreports>
</helpers>
A esto:
<helpers>
<mycompany_syncreports>
<class>Mycompany_Syncreports_Helper</class>
</mycompany_syncreports>
</helpers>
Siguiente: Veo eso en tu api.xml
declaraste solo un info
método, pero en el wsdl
tienes otros 2 métodos syncreportsSyncCreate
, syncreportsSyncCreate
.
Agregar esos métodos a api.xml
De lo contrario, recibirá un mensaje de error de ruta no válido.
<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>
También agregue los métodos a su modelo API (V2 también si es necesario).
Ahora se cose funcionar.
Esto es lo que envío:
<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>
Esto es lo que obtengo.
<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>
Así que supongo que el método funciona.
Cuando habilito el cumplimiento de WS-I, no obtengo ningún resultado de ninguna llamada. Ni siquiera de login
. Si encuentro algo, editaré la respuesta. Pero al menos puede probar el cumplimiento de WS-I.
Editar
Creo que encontré el problema con tu wsi
archivo también.
Has declarado el tipo syncreportsSyncItemsResponse
dos veces y la declaración para syncreportsSyncCreateResponse
Está perdido. Supongo que fue un error de copia/pegar/reemplazar (olvidó el reemplazo).
Cambia esto:
<wsdl:message name="syncreportsSyncItemsResponse">
<wsdl:part name="parameters" element="typens:syncreportsSyncCreateResponseParam" />
</wsdl:message>
A esto:
<wsdl:message name="syncreportsSyncCreateResponse">
<wsdl:part name="parameters" element="typens:syncreportsSyncCreateResponseParam" />
</wsdl:message>
Una nota al margen ... también es posible que desee cambiar la definición de <xsd:element name="syncreportsSyncCreateResponseParam">
Para encajar con el tipo de respuesta del método. Ahora declaras que devuelves un array
, pero al final supongo que devolverás un boolean
o una cadena.