You are on page 1of 20

Integrating PHP and ABAP Using Webservices

Purpose
One of the paradigms of modern architecture is service orientation
(SOA) with it's SAP favour ESA (enterprise service architecture). An
important principle of SOA is implementation transparency i.e. it
doesn't matter in which language a service is !eing implemented.
"his allows to com!ine services from di#erent specialised
environments into composite applications or processes.
P$P is a scripting language with an always growing user!ase having
its strenghts in a num!er of areas in which it can complement A%AP
well (e.g. &ml schema validation small footprint graphics
manipulation cryptology ...) 'ntegrating P$P and A%AP therefore
adds a valua!le opportunity for those creating state(of(the(art
solutions around the )etweaver platform.
'n this we!log ''ll give a simple tutorial to show how to ma*e use of
we!services technologies to integrate P$P and A%AP. 'n my opinion
the features of we!services and SOAP ma*e the use of the good old
P$P+,- li!rary ( which did such a good .o! for many years o!solet.
One interface technology less.
Prere/uisites
"he e&amples shown here re/uire A%AP 0AS 1.234 and P$P 5.&.
"he soap li!raries in P$P are not ena!led !y default so we have to
ena!le them !y adding
e&tension6php7soap.dll
to the php.ini 8le. ,or development we don't want the 0S9:s to !e
cached once downloaded so we have to set
soap.wsdl7cache7ena!led63
in php.ini.
-alling an A%AP we!service from P$P
,irst we have to create the we!service in A%AP. 0e create a very
simple we!service accepting one input parameter and returning a
parameter !ased on it. So we create a function module ;"070S<=
,>)-"'O) ;"07,>%<.
?@((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
?@?@:o*ale Schnittstelle=
?@ 'APO+"')B
?@ CA:>E(P7')) "DPE S"+')B
?@ EEPO+"')B
?@ CA:>E(P7O>") "DPE S"+')B
?@((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((
concatenate 'A%AP says= $ello' space p7in into p7out separated !y
space.
concatenate p7out 'F' into p7out.
E)9,>)-"'O).
"his is pro!a!ly the most simple function module to test the
functionality. 't e&pects a name say Sue and returns A%AP says=
$ello SueF 0e remote(ena!le save and activate it. )e&t we create a
we!service !ased on it. Since this is a !it di#erent for di#erent
versions of A%AP 0AS please consult the documentation for it.
+egardles of the version it's /uiet simple .ust right(clic* the function
module and 8nd the appropriate action or create a new o!.ect of
type we!service interface. 9o not forget to release the service for
the soap runtime. 0hen everything is set up we get the 0S9:
(we!service description languageG 'the service description') at the
following address=
http=HHtonysu!=I333HsapH!cHsrtHrfcHsapH;"070S<Jsap(
client6<33Kwsdl6<.<
where tonysu! is the name of our A%AP host. Lust view it in your
!rowser if you have never seen one =() "he ne&t thing is to create a
client in P$P to consume this service. SOAP support is !uilt(in for
P$P 5.& so we can immediately start to code our client=
MJ
Nwsdlurl 6@http=HHtonysu!=I333HsapH!cHsrtHrfcHsapH;"070S<Jsap(
client6<33Kwsdl6<.<@G
Nlogin 6 @!cuser@G
Npassword 6 @minisap@G
JO
M$"A:OM$EA9OM"'":EOsoap <MH"'":EOMH$EA9O
M%O9DO
MJ if(Fisset(Nname)) P JO
Mform action6@soap<.php@ method6@post@O
Dour name= Minput type6@te&t@ name6@name@O
M!rO9e!ugJ = Minput type6@chec*!o&@ name6@de!ug@
value6@true@O
M!rOMinput type6@su!mit@ value6@BoF@O
MHformO
MJ Q else P
Nclient 6 new Soap-lient(Nwsdlurl
array('login' 6O Nlogin
'password' 6O Npassword
'trace' 6O true
'e&ceptions' 6O true))G
try P
echo Nclient(O;tw,u!<(array('P'n' 6O Nname))(OPOutG
Q
catch (Soap,ault Ne) P
echo '-aught an Error= R' . Ne(Ofaultcode . 'S ( ' . Ne(OfaultstringG
Q
if(isset(Nde!ug)) P
echo @MhrOMpreOTn@G
echo @+e/uest =Tn@.htmlspecialchars(Nclient(
O77get:ast+e/uest()) .@Tn@G
echo @+esponse=Tn@.htmlspecialchars(Nclient(
O77get:ast+esponse()).@Tn@G
echo @MHpreO@G
Q
Q
JO
MH%O9DOMH$"A:O
"here's 'a lot' of coding mas*ing the simplicity of the we!service
call so here's the crucial parts again=
"he constructor=
Nclient 6 new Soap-lient(Nwsdlurl
array('login' 6O Nlogin
'password' 6O Npassword
'trace' 6O true
'e&ceptions' 6O true))G
And the actual service call(and display of the result)=
echo Nclient(O;tw,u!<(array('P'n' 6O Nname))(OPOutG
"he rest of the coding shows
an $"A: form letting you enter your name and decide if you
want to see de!ugging information
SOAP error handling
de!ugging of the SOAP communication
$ere is what we've got so far. An input screen to input your name=
An the result showing the answer of the we!service
-alling a P$P we!service from A%AP
,irst we create a we!service in P$P. "his is not so easy as one might
e&pect from P$P. "he reason for this is that using the standard SOAP
functions you need a 0S9: 8rst to instanciate a we!service. "here
are li!raries on the internet allowing to autogenerate a 0S9: !ased
on some P$P functions !ut those li!raries are not too ela!orate yet.
One good solution is the one provided !y nusoap. >nfortunately this
li!rary collides with the standard li!raries since they use the same
names for SOAP classes. So we create the 0S9: ourselves. 0e
already have one template which ma*es this tutorial nicely
symmetrical. 0e use the one created for the A%AP we!service and
adopt it for our needs.
MJ&ml version6@<.3@ encoding6@utf(I@ JO
Mwsdl=de8nitions target)amespace6@urn=tonysu!Hsoape&ample<@
&mlns=http6@http=HHschemas.&mlsoap.orgHwsdlHhttpH@
&mlns=soap6@http=HHschemas.&mlsoap.orgHwsdlHsoapH@
&mlns=tns6@urn=tonysu!Hsoape&ample<@
&mlns=wsdl6@http=HHschemas.&mlsoap.orgHwsdlH@
&mlns=&sd6@http=HHwww.wU.orgHV33<HEA:Schema@O
Mwsdl=typesO
M&sd=schema &mlns=&sd6@http=HHwww.wU.orgHV33<HEA:Schema@
&mlns=tns6@urn=tonysu!Hsoape&ample<@
target)amespace6@urn=tonysu!Hsoape&ample<@
element,orm9efault6@un/uali8ed@
attri!ute,orm9efault6@/uali8ed@O
M&sd=element name6@;tw,u!<@O
M&sd=comple&"ypeO
M&sd=se/uenceO
M&sd=element name6@P'n@ type6@&sd=string@ HO
MH&sd=se/uenceO
MH&sd=comple&"ypeO
MH&sd=elementO
M&sd=element name6@;tw,u!<+esponse@O
M&sd=comple&"ypeO
M&sd=se/uenceO
M&sd=element name6@POut@ type6@&sd=string@ HO
MH&sd=se/uenceO
MH&sd=comple&"ypeO
MH&sd=elementO
MH&sd=schemaO
MHwsdl=typesO
Mwsdl=message name6@;tw,u!<@O
Mwsdl=part name6@parameters@ element6@tns=;tw,u!<@ HO
MHwsdl=messageO
Mwsdl=message name6@;tw,u!<+esponse@O
Mwsdl=part name6@parameters@
element6@tns=;tw,u!<+esponse@ HO
MHwsdl=messageO
Mwsdl=port"ype name6@;"070SE@O
Mwsdl=operation name6@;tw,u!<@O
Mwsdl=input message6@tns=;tw,u!<@ HO
Mwsdl=output message6@tns=;tw,u!<+esponse@ HO
MHwsdl=operationO
MHwsdl=port"ypeO
Mwsdl=!inding name6@;"070S<Soap%inding@
type6@tns=;"070SE@O
Msoap=!inding style6@document@
transport6@http=HHschemas.&mlsoap.orgHsoapHhttp@ HO
Mwsdl=operation name6@;tw,u!<@O
Msoap=operation soapAction6@@ HO
Mwsdl=inputO
Msoap=!ody use6@literal@ HO
MHwsdl=inputO
Mwsdl=outputO
Msoap=!ody use6@literal@ HO
MHwsdl=outputO
MHwsdl=operationO
MHwsdl=!indingO
Mwsdl=service name6@;"070S<Service@O
Mwsdl=port name6@;"070S<Soap%inding@
!inding6@tns=;"070S<Soap%inding@O
Msoap=address
location6@http=HHtonysu!HwsHsoapserver<.php@ HO
MHwsdl=portO
MHwsdl=serviceO
MHwsdl=de8nitionsO
Aainly we adopt some namespaces to point to our own namespace
urn=tonysu!Hsoape&ample< and the SOAP=address location to point
at our we!service at http=HHtonysu!HwsHsoapserver<.php. 0e save
this as soapserver<.wsdl to our we!directory. $avin the 0S9: it's
very easy to create the we!service in P$P
MJ
function ;tw,u!<(Nname) P
return (array(POut 6O @P$P says= $ello @ . Nname(OP'n .
@F@))G
Q
Nserver 6 new SoapServer(@soapserver<.wsdl@)G
Nserver(Oadd,unction(@;tw,u!<@)G
Nserver(Ohandle()G
JO
"hat's the P$P we!service. 0e could test it !y simply pointing the
client a!ove to the new 0S9: location i.e.
http=HHtonysu!HwsHsoapserver<.wsdl (and clearing login an password
for this e&ample). %ut we /uic*ly move on to test it from the A%AP
side. 0e go to SEI3 again and create a so called we!service pro&y
o!!.ect using the same 0S9:. 0e follow the wiWard and get a pro&y
o!.ect e.g. ;"07-O7;"070SE. 99'- structures for the input and
and output parameters are created here ;"07;"07,>%< and
;"07;"07,>%<. ,inally we have to go to transaction :P-O),'B and
create a logical port for our pro&y o!.ect. So the only thing left to do
is write a report to ma*e use of this pro&y o!.ect.
?K(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((?
?K +eport ;"07-A::70S
?K
?K(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((?
?K
?K
?K(((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((?
+EPO+" ;"07-A::70S.
data= my7client type ref to Wtw7co7Wtw7ws&
my7result type Wtw7Wtw7fu!<response
my7input type Wtw7Wtw7fu!<.
parameters= my7name(V3) type c.
my7input(pin 6 my7name.
create o!.ect my7client.
call method my7client(O;"07,>%<
e&porting ')P>" 6 my7input
importing O>"P>" 6 my7result.
write= H my7result(pout.
CoilX we end up with the following= An input screen to enter a name
An the result showing the answer of the P$P we!service
%asically thats it. Everything else is 8ddling around with more
comple& data structures (nested structures in A%AP and nested
arrays in P$P). And of course ela!orating on more useful
e&amples =()
,ollow(up we!logs will cover such topics. Stay tuned.
,urther documentation on the P$P SOAP li!raries can !e found here.
Anton 0enWelhuemer is a solution architect wor*ing on
)etweaver solutions in the healthcare industry
Turn any SAP remote-enabled function module into a
Web Service Part 1
September 29, 2009 By Steve Park 5 Comments
It still amazes me that not a lot of developers know that yo !an trn any e"istin# S$P
remote enable fn!tion modle into a %eb Servi!e& It may sond like a !hallen#in#
task, bt it is 'st one of those thin#s that yo either know how to do it or yo don(t&
$nd to know how to do it only takes )5min to nderstand&
I have broken this down into 2 blo#s
Part ) will show how to !reate the simple str!tred web servi!e
Part 2 will show how to !onfi#re and test the web servi!e sin# S*$+$,$-./
and %S,$0
1or this s!enario, I !reated a really simple fn!tion modle with one inpt and one
otpt that will send ba!k a response of 2/esponse s!!essfl from test3 4his will
only !reate one operation for this servi!e& 5owever, if yo want to !reate mltiple
operations for a Servi!e yo !an also #enerate the servi!e from a fn!tion #rop&
6o will need to ensre that the attribte is set as /emote7enabled in the fn!tion
modle&
,ow #o to transa!tion S.80 and ri#ht7!li!k9:Create9:%eb Servi!e
Sin!e S$P will be hostin# this web servi!e we will want to sele!t 2Servi!e Provider3
Sele!t the radio btton 2Existing ABAP Object(Inside Out)
Sin!e we are bildin# this web servi!e from a 1n!tion +odle& 4he differen!e
between and inside ot vs& an otside is approa!h is where the str!tre is bilt& 1rom
a desi#n perspe!tive, it is re!ommended that yo se an *tside7in approa!h
whenever possible& 4his means that yo wold bild ot the str!tre in the .S/ and
then implement the !ode behind it in S$P&& 5owever, there will be !ases when yo
will want to trn a Standard S$P remote enable fn!tion modle into a %eb Servi!e,
ths sin# this approa!h, the Inside ot approa!h&&;
-ive a name to what yo want to !all yor .nterprise servi!e and spe!ify that the
endpoint will be a fn!tion modle&
.nter the name of the 1n!tion modle that the web servi!e will !all in the ba!kend&
5ere yo will have to spe!ify whi!h athenti!ation yo want this servi!e to have& 1or
this e"ample I have set it to no athenti!ation&
,ow yo are !omplete with bildin# this web servi!e& 5owever at this point yo
have 'st bilt the %eb Servi!e bt yo will now need to !onfi#re it so that an
e"ternal appli!ation !an !all this web servi!e&
4his will !ome in my ne"t Blo# for !onfi#rin# the newly !reated %eb Servi!e and as
well as testin# this servi!e&
Turn any SAP remote-enabled function module into a
Web Service Part
*!tober ), 2009 By Steve Park < Comments
In my previos blo# I showed the steps into trnin# a simple fn!tion modle into an
.nterprise Servi!e& ,ow that we have the servi!e defined in .CC, or ne"t step is to
!onfi#re it so an e"ternal appli!ation !an !all or newly !reated servi!e&
I will show how to !onfi#re and test the %eb Servi!e on .CC with Servi!e pa!k )=
and hi#her& Before Servi!e Pa!k )=, the !onfi#ration is !ompleted in .CC, however
after SP)= the !onfi#ration is !ompleted on the >ava Sta!k&
,ow let(s #et started&
."e!te the followin# transa!tion?
4ransa!tion? S*$+$,$-./
4his will initiate a %eb session and prompt yo to lo# into the %eb$S&
*n!e yo have lo##ed in, the ne"t step is to lo!ate the .nterprise Servi!e that yo
!reated earlier&
4o lo!ate yor .nterprise Servi!e #o to
Bsiness $dministrator 4ab 9: %eb Servi!e $dministration
1irst we will want to find the .nterprise Servi!e that we !reated& In or s!enario, we
named or test servi!e as 2@4.S4%.BS./0IC.3& $fter yo have fond yor
servi!e, yo need to display it by pressin# the 2$pply Sele!tion3 btton&
4his will display the !rrent settin#s for this servi!e& Sin!e we have not yet defined
the !onfi#ration we will want to !reate a new servi!e& In the Confi#ration tab,
sele!t 2Create Servi!e3&
.nter a Servi!e ,ame and a Bindin# name that yo wold like& I sally pt a prefi"
of 2S,3 and 2B,3 when definin# a Servi!e ,ame and a Bindin# name&
5ere yo will see all of the different type of rntime !onfi#ration options that are
available& $s yo !an see yo !an !onfi#re other se!rity and athenti!ation
options& *n!e yo are !omplete, press 2S$0.3&
,ow yo are !omplete& 4he %eb Servi!e shold be p and rnnin# ready for an
e"ternal appli!ation to !all it& *r ne"t step is to obtain the wsdl lo!ation and e"e!te
a test a !all to the %eb Servi!e from an e"ternal appli!ation&&
Testing the Web Service
Before we !an test the servi!e we will first need to #et the wsdl lo!ations& %ith yor
servi!e displayed, #o to the *verview tab&
5ere yo will want to !li!k on 2*pen wsdl do!ment for sele!ted bindin#3
4his will display the wsdl in another web session& 4his is the A/B that needs to be
#iven to the e"ternal appli!ation that is initiatin# the !all& In or !ase, we will be
sin# the %S ,avi#ation tool to be doin# this !all& Copy this A/B&
,ow we have the wsdl lo!ation, we will se the %S ,avi#ator to perform the test&
In *verview tab, sele!t the followin# link?
2*pen %eb Servi!e navi#ator for sele!ted bindin#3
5ere yo will enter the wsdl A/B that yo !opied in the previos step&
4hen press C,e"t(
.nter a valid ser that will lo# into .CC when !allin# the %eb Servi!e
,ow press C4est(&
5ere is a list of available operations to !all this Servi!e& /emember sin!e we bilt it
from 'st a fn!tion modle it will only !ontain one operation& If we want to bild an
servi!e with mltiple operation we wold #enerate this servi!e from a fn!tion #rop&
1or this simple e"ample, the servi!e will take any inpt and brin# ba!k a s!!essfl
response&
$nd there yo have it& 6o know how to trn a fn!tion modle into a %eb Servi!e
and test the servi!e with %S ,avi#ator&