Você está na página 1de 4

Utilizando SOAP com PHP

Ir para: navegao, pesquisa A seguir um exemplo simples de um webservice usando a extenso SOAP do PHP, utilizaremos os arquivos:

Na mquina cliente: 'index.html' e 'client.php', alocados no mesmo diretrio. Na mquina servidor: 'service.wsdl' e 'server.php', alocados no mesmo diretrio.

- Lembrando que estes quatro arquivos tambm podem estar na mesma mquina, assim ela prpria ser o cliente e o servidor.

ndice
[ocultar]

1 Arquivos o 1.1 Client : index.html o 1.2 Client : client.php o 1.3 Server : service.wsdl o 1.4 Server : server.php 2 Veja tambm 3 Referncia

Arquivos
Client : index.html

este ser colocado na mquina cliente.

<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso8859-1" /> <title>Exemplo com SOAP</title> </head> <body> <ul><b>Cliente SOAP</b> <li><a href="./client.php">Exemplo 1 (client.php)</a></li> </ul> <ul><b>Servidor SOAP</b>

<li><a href="http://SeuDominio/DiretorioSOAP/service.wsdl">Exemplo 2 (service.wsdl)</a></li> <li><a href="http://SeuDominio/DiretorioSOAP/server.php">Exemplo 3 (server.php, p&aacute;gina em branco)</a></li> </ul> <p>Aten&ccedil;&atilde;o: No arquivo 'service.wsdl' substitua a linha 30 por</p> <p>&lt;soap:address location="http://SeuDominio/DiretorioSOAP/[diretorio-doserver.php]/server.php"/&gt;</p> <div>Refer&ecirc;ncia: <a href="http://devzone.zend.com/node/view/id/689" target="_blank">PHP SOAP Extension</a> e <a href="http://blog.diegotremper.com/archives/29" target="_blank">Blog do Tremper &raquo; Depurar webservices com Zend Studio + PHP Soap</a></div> </body> </html>

Client : client.php
<pre> <?php ini_set("soap.wsdl_cache_enabled", "0"); // A seguir voc devera informar a URL do webservice. $oSoapClient = new SoapClient("http://SeuDominio/DiretorioSOAP/service.wsdl"); $aOptions = array ( "start_debug"=> "1", "debug_port"=> "10000", "debug_host"=> "localhost", "debug_stop"=> "1"); foreach($aOptions as $key => $val) { $oSoapClient->__setCookie($key,$val); } var_dump($oSoapClient->getUser()); ?> </pre> [<a href=".">Voltar</a>]

este ser colocado na mquina cliente.

Server : service.wsdl

este ser colocado na mquina servidor.

<?xml version='1.0' encoding='UTF-8'?> <!-- WSDL file generated by Zend Studio. --> <definitions name="Service" targetNamespace="urn:Service" xmlns:typens="urn:Service" 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/"> <message name="getUser"/> <message name="getUserResponse"> <part name="getUserReturn" type="xsd:string"/> </message> <portType name="UserFacadePortType"> <operation name="getUser"> <input message="typens:getUser"/> <output message="typens:getUserResponse"/> </operation> </portType> <binding name="UserFacadeBinding" type="typens:UserFacadePortType"> <soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/> <operation name="getUser"> <soap:operation soapAction="urn:UserFacadeAction"/> <input> <soap:body namespace="urn:Service" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </input> <output> <soap:body namespace="urn:Service" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"/> </output> </operation> </binding> <service name="ServiceService"> <port name="UserFacadePort" binding="typens:UserFacadeBinding"> <soap:address location="http://SeuDominio/DiretorioSOAP/server.php"/> </port> </service> </definitions>

Server : server.php

este ser colocado na mquina servidor.

<?php class UserFacade { /** * @return string */ public function getUser() { $sName = 'MeuNome'; $sName .= ' MeuSobrenome'; return $sName; } } $oSoapServer = new SoapServer('service.wsdl'); $oSoapServer->setClass("UserFacade"); $oSoapServer->handle(); ?>