Interagindo com web services
RPC, SOAP e REST
utilizando PHP
EDUARDO CESAR
DESENVOLVEDOR - DAKSA
TRILHA - PHP
WEB SERVICES
WEB SERVICES
AWeb service is a software system designed to
support interoperable machine-to-machine
interaction over a network. (W3C)
WEB SERVICES
Não importa se o software está consumindo ou
fornecendo um serviço, o web service é uma parte
fundamental em todo aplicativo.
(Mitchell,Web Services PHP, 2013)
SERVIÇOS RPC
SERVIÇOS RPC
Funciona sobre o modelo Cliente/Servidor
chamada de funções são similares a chamadas de
funções locais, a diferença que é nestes casos nem
sempre a máquina que processa a função é a
mesma que a invocou.
SERVIÇOS RPC
 Deve informar a função
e seus parâmetros
Cliente realiza uma
requisição
HTTP
 Arquivo XML com nome
da função e os
parâmetros a serem
processados informados
Conteúdo da
requisição
 Deve executar e
retornar os dados de
acordo com a função
informada na requisição
Servidor que irá
responder
Utilizando XML-RPC PHP
XML-RPC CLIENT PHP
Request
<?php
$request = xmlrpc_encode_request("createUser", ['eduardo']);
$server = 'http://localhost/web-services/xml-rpc/user/server.php';
$curl = curl_init($server);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $request);
$file = curl_exec($curl);
curl_close($curl);
$response = xmlrpc_decode($file);
echo $response;
XML-RPC CLIENT PHP
XML
<?xml version="1.0" encoding="iso-8859-1"?>
<methodCall>
<methodName>createUser</methodName>
<params>
<param>
<value>
<string>eduardo</string>
</value>
</param>
</params>
</methodCall>
XML-RPC CLIENT PHP
Response
<?php
function get(string $method_name) : array {/¨ do anything */}
function create(string $method_name, array $args) : string {/¨ do anything */}
function update(string $method_name, array $args) : string {/¨ do anything */}
function delete(string $method_name, array $args) : string{/¨ do anything */}
<?php
$request = file_get_contents("php://input");
$server = xmlrpc_server_create();
xmlrpc_server_register_method($server, "getUser", "get");
xmlrpc_server_register_method($server, "createUser", "create");
xmlrpc_server_register_method($server, "updateUser", "update");
xmlrpc_server_register_method($server, "deleteUser", "create");
header('Content-Type: text/xml');
print xmlrpc_server_call_method($server, $request, array());
SERVIÇOS SOAP
SERVIÇOS SOAP
... SOAP is a lightweight protocol for exchange of
information in a decentralized, distributed
environment…
(W3C)
SERVIÇOS SOAP
Pode ou não utilizarWSDL
Formato de requisição mais “rígidos”
Funciona como um serviço RPC
Utilizando SOAP PHP
SOAP CLIENT PHP
<?php
$options = [
'uri' => 'http://localhost/web-services/soap/user',
'location' => 'http://localhost/web-services/soap/user/server.php'
];
$api = new SoapClient(null, $options);
//$api = new SoapClient(“api.wsdl”);
foreach($api->get() as $user)
echo $user;
echo $api->create('luiz');
echo $api->update(1, 'cesar');
echo $api->delete(1);
O ENVELOPE SOAP
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope ...">
<SOAP-ENV:Body>
<ns1:create>
<nome xsi:type="xsd:string">eduardo</nome>
</ns1:create>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
SOAP SERVER PHP
<?php
class UserService
{
public function get() : array
{
return $this->users;
}
public function create(string $nome, string $email) : string
{
return 'user '.$nome.' created with success';
}
public function update(int $id, string $nome, string $email) : string
{
return 'user '.$id.' updated with success';
}
public function delete(int $id) : string
{
return 'user '.$id.' deleted with success';
}
}
<?php
ini_set('soap.wsdl_cache_enabled', 0);
$options = ['uri' => 'http://localhost/web-services/soap/mode-non-wsdl/user'];
$server = new SoapServer(null, $options);
//$server = new SoapServer("api.wsdl");
$server->setClass('UserService');
$server->handle();
SERVIÇOS REST
SERVIÇOS REST
…Diferente dos estilos XML-RPC ou SOAP, REST
tende mais a um conjunto de princípios do que um
protocolo…
(Mitchell,Web Services PHP, 2013)
SERVIÇOS REST
Uso do protocolo HTTP
Formato de requisição mais “flexível”
Não é um serviço RPC
SERVIÇOS REST PHP
SERVIÇOS REST PHP
<?php
$app->get(‘/users', function () {
});
$app = new SilexApplication();
$app->post('/users', function(Request $request) use($app) {
});
$app = new SlimApp;
$app->delete('/users/{id}', function (Request $request, Response $response) {
});
Referências
https://www.w3.org/TR/ws-arch/#introduction
http://xmlrpc.scripting.com/spec.html
http://php.net/xml-rpc
https://www.w3.org/TR/2000/NOTE-SOAP-20000508/
https://www.w3.org/TR/wsdl
http://php.net/soap
https://lumen.laravel.com/
https://silex.symfony.com/doc/2.0/
https://www.slimframework.com/
https://www.amazon.com/PHP-Web-Services-APIs-Modern/dp/1491933097
https://github.com/Bolinha1/web-services
/in/eduardo-cesar-oliveira
/Bolinha1
www.daksa.com.br
eduardo.borsato.oli@gmail.com
http://techinterior.com.br/
OBRIGADO!

Interagindo com web services RPC, SOAP e REST utilizando PHP