Re: Retornar arreglos y mostrarlos con web services
| From: | Julio Araya | Date: | Wed, 05 Nov 2008 22:03:22 +0000 |
| Subject: | Re: Retornar arreglos y mostrarlos con web services | ||
| References: | 1 | Groups: | php.general.es |
| Request: | Send a blank email to general-es+get-63364@lists.php.net to get a copy of this message | ||
2008/11/5 Ricardo Vila <ricardo.vila@gmail.com>:
> BUenas listeros, una consulta.
>
> Estoy empezando con webservices y estoy haciendo un ejemplo simple, pero
> quisiera saber como hacer para devolver un arreglo mediante web services.
>
> y como podria hacer para poder imprimirlo. LO que tengo hasta ahora solo me
> imprime lo siguiente:
> *string(5) "Array"*
>
> mi codigo:
>[..]
Podrias tener el siguiente webservice
<?php
class AlgunaClase{
/**
* Enter description here...
*
* @param string $pmName
* @return string[]
*/
function getName ($pmName ) {
$pm = array();
if(trim($pmName)=='testpm'){
$pm = array('Carlos','Jose','Maria');
}else{
$pm = array ('Andres', 'Pedro');
}
return $pm;
}
}
$server = new SoapServer("php-es-0001.wsdl");
$server->setClass("AlgunaClase");
$server->handle();
?>
y el wsdl sería algo asi
<?xml version='1.0' encoding='UTF-8'?>
<definitions name="php-es-001" targetNamespace="urn:php-es-001"
xmlns:typens="urn:php-es-001"
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/"
xmlns:typens0="http://192.168.0.1/miwebservice">
<types>
<xsd:schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="urn:php-es-001">
<xsd:complexType name="stringArray">
<xsd:complexContent>
<xsd:restriction base="soapenc:Array">
<xsd:attribute ref="soapenc:arrayType"
wsdl:arrayType="xsd:string[]"/>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
</xsd:schema>
</types>
<message name="getName">
<part name="pmName" type="xsd:string"/>
</message>
<message name="getNameResponse">
<part name="getNameReturn" type="typens:stringArray"/>
</message>
<portType name="AlgunaClasePortType">
<operation name="getName">
<documentation>
Enter description here...
</documentation>
<input message="typens:getName"/>
<output message="typens:getNameResponse"/>
</operation>
</portType>
<binding name="AlgunaClaseBinding" type="typens:AlgunaClasePortType">
<soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="getName">
<soap:operation soapAction="urn:AlgunaClaseAction"/>
<input>
<soap:body namespace="urn:php-es-001" use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</input>
<output>
<soap:body namespace="urn:php-es-001" use="encoded"
encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/>
</output>
</operation>
</binding>
<service name="php-es-001Service">
<port name="AlgunaClasePort" binding="typens:AlgunaClaseBinding">
<soap:address location="http://192.168.3.33/miwebservice"/>
</port>
</service>
</definitions>
y el cliente casi igual que el anterior
<?php
$client = new SoapClient("http://192.168.3.33/php-es-001.wsdl");
var_dump($client->getName("algo"));
?>
--
Julio Araya C. Linux User #386141
Estudiante de Ingeniería Civil Informática Ubuntu User #14778
Universidad Técnica Federico Santa María Valparaíso - Chile
http://www.delpho.cl