Wednesday, August 5, 2009

Create a Web Service in SAP R/3 and Consume it with PHP-SOAP

Intro
If you do much like to work developing web applications, SAP R/3, and mobile smartphones, eventually you find that there's is some task that you want to do with the phone and the data is in SAP. For example, if you are a student of some university and this institution has the Student Life Cycle Management, you may wish to know your personal data form your phone, or you want to see your grades. But the institution doesn't have the SAP Netweaver Mobile 7.1. Well as a developer i recommend to publish a web service because it can work with any plataform and the idea of course is to solve a simple and different task.

Steps
First of all we need to know what we want to publish in the web for our clients, specially in SAP R/3. Second you have to define your architecture for this solution because they are different ways to solve this. I'm going to show a solution, it's not the best way but it is the only resources that I have in my work.

Ok, lets roll. I'm going to publish the personal data of a student, just some values.
First we create the function module Z_WS_PANDA in transaction SE37 or SE80, and of course you should try to create it in a function group for the web services just for SLCM, or in the module that you are working for.

Once you finish creating the Function Module, you need to select the attribute for remote access as shown in picture below.


Second create the variables for export & import as shown below with the following pictures.



Now you develop the application with the following functions. For Example: BAPI_STUDENT_GET_NUMBERS, BAPI_STUDENT_GETDETAIL3,
HRIQ_STUDENT_STUDIES_GET_RFC. If you know ABAP, you will notice that this is really straight foward. Now if your function module it's ready and activated, you have to follow the next step. In the Menu Bar, choose Utilities->More Utilities->Generate Web Service->For Function Module. This will popup a wizard window. Just follow the steps and you are done!!!
(In the figures the name of the service is different)








Final Step with SAP
It is important now to view your WSDL. There is different ways to view the WSDL generated. The easy way is go to TX SE80, In the Browser Repository, select the package in the dropdown list, then search for the package where is the Web Service that you just created. The next step is to open de Enterprise Service folder, then the DefinitionServ and doubleclick the Service that you created. For Example: ZWS_PANDA. (In the figure is selected another service).


Now you need to select the tab WSDL. This one provides with the URL, for Example: http://xxxx.xxx.com:8001/sap/bc/srt/wsdl/sdef_ZWS_PANDA_ADM/wsdl11/ws_policy/document?sap-client=100

PHP
Now I'm just going to try to show you how i call the web service with php-soap. The trick here is to change the part in the URL ws_policy to standard. For example:
http://xxxx.xxx.com:8001/sap/bc/srt/wsdl/sdef_ZWS_PANDA_ADM/wsdl11/ws_policy/document?sap-client=100
change it to
http://xxxx.xxx.com:8001/sap/bc/srt/wsdl/sdef_ZWS_PANDA_ADM/wsdl11/standard/document?sap-client=100

require_once('SOAP/Client.php');

$url = 'http://xxxx.xxx.com:8001/sap/bc/srt/wsdl/sdef_ZWS_PANDA/wsdl11/standard/document?sap-client=100';

$login='user';
$pass='xxxxx';

//Call the client
client = new SoapClient($url,
array('login' => $login,
'password' => $password,
'trace' => true,
'exceptions' => true));

try {
$person = $client->ZWsPanda(array('Cedula' => $_GET['cedula']));
//print_r ( $person );
$datos = $person->PersonaldataT;
print_r( $datos );
}
catch (SoapFault $e) {
echo 'Caught an Error: [' . $e->faultcode . '] - ' . $e->faultstring;

}

Conclusion
It is nice to do some simple task with a very useful webserver like apache and php5. This could easily
shown in the web browser for a mobile solution.

I hope you could enjoy the article!

2 comments:

  1. hi. thanks for sharing the soap for php on r/3 ..

    i am looking for the similar way to consume for iphone(object-c)..do u have any idea?


    thanks.


    big.render@gmail.com

    ReplyDelete
  2. Hi, thank you, it was very informative.I'm trying to call sap soap in php.
    But, i couldn't manage it.My url is http://*****/sap/bc/srt/wsdl/flv_10***/bndg_url/sap/bc/srt/rfc/sap/*******?sap-client=100?wsdl.
    But it didn't contain any ws_policy.Can you help me?

    ReplyDelete