Webservice Tutorial
Webservice Tutorial
Prerrequisitos.
Lo primero que debemos crear es una función de acceso remoto de tipo RFC y construir
nuestra lógica, lógica que será consumida mediante el Webservice.
Una vez terminemos este proceso, damos click derecho sobre la función y
seleccionamos Crear-> Servicio Web
Después nos aparecerá un Wizard que nos ayudara con la configuración del servicio
Web.
Ahora damos click en el último del árbol de jerarquía, y allí configuramos los datos de
login para el servicio Web, es posible dar estos datos dinámicamente pero en nuestro
caso asignaremos el usuario para evitar el proceso de autenticación.
-
Datos de autenticación, damos clic en guardar y volvemos a la pantalla anterior
Ya esta lista para consumir el servicio web, ahora para probarlo y hacerle test, se
recomienda crear un programa ABAP pero antes hay que crear un Proxy veamos como.
Acá nos pide nuestra URL, es el URL que usamos cuando vimos la página en nuestro
navegador, mi caso:
http://sapdes.jddominio.com:8010/sap/bc/srt/rfc/sap/ZIN_RADICACION_VI?sap-
client=100&wsdl=1.1
Después nos pide un paquete y un prefijo, damos el paquete donde esta toda nuestra
lógica. Una vez creado el Proxy vamos a la transacción LPCONFIG, en ella
configuramos el puerto. Damos clic en crear y ya tenemos todo listo ahora scon la sgte
explicación montamos el programa.
Integrating PHP and ABAP Using Webservices
Purpose
One of the paradigms of modern architecture is service orientation (SOA) with it's SAP flavour ESA (enterprise
service architecture). An important principle of SOA is implementation transparency, i.e. it doesn't matter in
which language a service is being implemented. This allows to combine services from different specialised
environments into composite applications or processes.
PHP is a scripting language with an always growing userbase having its strenghts in a number of areas, in which
it can complement ABAP well (e.g. xml schema validation, small footprint graphics manipulation, cryptology, ...)
Integrating PHP and ABAP therefore adds a valuable opportunity for those creating state-of-the-art solutions
around the Netweaver platform.
In this weblog I'll give a simple tutorial to show how to make use of webservices technologies to integrate PHP
and ABAP. In my opinion the features of webservices and SOAP make the use of the good old PHPRFC library -
which did such a good job for many years, obsolet. One interface technology less.
Prerequisites
The examples shown here require ABAP WAS 6.40+ and PHP 5.x. The soap libraries in PHP are not enabled by
default, so we have to enable them by adding
extension=php_soap.dll
to the php.ini file. For development we don't want the WSDLs to be cached once downloaded so we have to set
soap.wsdl_cache_enabled=0
in php.ini.
ENDFUNCTION.
This is probably the most simple function module to test the functionality. It expects a name, say Sue, and
returns ABAP says: Hello Sue! We remote-enable, save and activate it. Next we create a webservice based on it.
Since this is a bit different for different versions of ABAP WAS, please consult the documentation for it.
Regardles of the version it's quiet simple, just right-click the function module and find the appropriate action or
create a new object of type webservice interface. Do not forget to release the service for the soap runtime.
When everything is set up, we get the WSDL (webservice description language; 'the service description') at the
following address:
http://tonysub:8000/sap/bc/srt/rfc/sap/ZTW_WS1?sap-client=100&wsdl=1.1
where tonysub is the name of our ABAP host. Just view it in your browser if you have never seen one :-) The
next thing is to create a client in PHP to consume this service. SOAP support is built-in for PHP 5.x so we can
immediately start to code our client:
<?
$wsdlurl ="http://tonysub:8000/sap/bc/srt/rfc/sap/ZTW_WS1?sap-
client=100&wsdl=1.1";
$login = "bcuser";
$password = "minisap";
?>
<HTML><HEAD><TITLE>soap 1</TITLE></HEAD>
<BODY>
<? } else {
$client = new SoapClient($wsdlurl,
array('login' => $login,
'password' => $password,
'trace' => true,
'exceptions' => true));
try {
echo $client->ZtwFub1(array('PIn' => $name))->POut;
}
catch (SoapFault $e) {
echo 'Caught an Error: [' . $e->faultcode . '] - ' . $e-
>faultstring;
}
if(isset($debug)) {
echo "<hr><pre>\n";
echo "Request :\n".htmlspecialchars($client-
>__getLastRequest()) ."\n";
echo "Response:\n".htmlspecialchars($client-
>__getLastResponse())."\n";
echo "</pre>";
}
}
?>
</BODY></HTML>
There's 'a lot' of coding masking the simplicity of the webservice call, so here's the crucial parts again:
The constructor:
$client = new SoapClient($wsdlurl,
array('login' => $login,
'password' => $password,
'trace' => true,
'exceptions' => true));
And the actual service call(and display of the result):
echo $client->ZtwFub1(array('PIn' => $name))->POut;
The rest of the coding shows
an HTML form letting you enter your name and decide if you want to see debugging information
First we create a webservice in PHP. This is not so easy as one might expect from PHP.
The reason for this is that using the standard SOAP functions you need a WSDL first to
instanciate a webservice. There are libraries on the internet allowing to autogenerate a
WSDL based on some PHP functions but those libraries are not too elaborate yet. One
good solution is the one provided by nusoap. Unfortunately this library collides with the
standard libraries since they use the same names for SOAP classes. So we create the
WSDL ourselves. We already have one template which makes this tutorial nicely
symmetrical. We use the one created for the ABAP webservice and adopt it for our
needs.
<?xml version="1.0" encoding="utf-8" ?>
<wsdl:definitions targetNamespace="urn:tonysub/soapexample1"
xmlns:http="http://schemas.xmlsoap.org/wsdl/http/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="urn:tonysub/soapexample1"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<wsdl:types>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:tns="urn:tonysub/soapexample1"
targetNamespace="urn:tonysub/soapexample1"
elementFormDefault="unqualified"
attributeFormDefault="qualified">
<xsd:element name="ZtwFub1">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="PIn" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="ZtwFub1Response">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="POut" type="xsd:string" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="ZtwFub1">
<wsdl:part name="parameters" element="tns:ZtwFub1" />
</wsdl:message>
<wsdl:message name="ZtwFub1Response">
<wsdl:part name="parameters" element="tns:ZtwFub1Response" />
</wsdl:message>
<wsdl:portType name="ZTW_WSX">
<wsdl:operation name="ZtwFub1">
<wsdl:input message="tns:ZtwFub1" />
<wsdl:output message="tns:ZtwFub1Response" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="ZTW_WS1SoapBinding" type="tns:ZTW_WSX">
<soap:binding style="document"
transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="ZtwFub1">
<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="ZTW_WS1Service">
<wsdl:port name="ZTW_WS1SoapBinding"
binding="tns:ZTW_WS1SoapBinding">
<soap:address
location="http://tonysub/ws/soapserver1.php" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
Mainly we adopt some namespaces to point to our own namespace
urn:tonysub/soapexample1 and the SOAP:address location to point at our webservice at
http://tonysub/ws/soapserver1.php. We save this as soapserver1.wsdl to our
webdirectory. Havin the WSDL it's very easy to create the webservice in PHP
<?
function ZtwFub1($name) {
return (array(POut => "PHP says: Hello " . $name->PIn . "!"));
}
?>
That's the PHP webservice. We could test it by simply pointing the client above to the
new WSDL location, i.e. http://tonysub/ws/soapserver1.wsdl (and clearing login an
password for this example). But we quickly move on to test it from the ABAP side. We
go to SE80 again and create a so called webservice proxy obbject using the same
WSDL. We follow the wizard and get a proxy object, e.g. ZTW_CO_ZTW_WSX.
DDIC structures for the input and and output parameters are created, here
ZTW_ZTW_FUB1 and ZTW_ZTW_FUB1. Finally we have to go to transaction
LPCONFIG and create a logical port for our proxy object. So, the only thing left to do is
write a report to make use of this proxy object.
*&--------------------------------------------------------------------
-*
*& Report ZTW_CALL_WS
*&
*&--------------------------------------------------------------------
-*
*&
*&
*&--------------------------------------------------------------------
-*
REPORT ZTW_CALL_WS.
my_input-pin = my_name.
write: / my_result-pout.
Voilá, we end up with the following: An input screen to enter a name
Basically thats it. Everything else is fiddling around with more complex data structures
(nested structures in ABAP and nested arrays in PHP). And, of course, elaborating on
more useful examples :-)
Follow-up weblogs will cover such topics. Stay tuned.
Further documentation on the PHP SOAP libraries can be found here.
Here is what we've got so far. An input screen to input your name: