Thursday, May 28, 2009

Calling SAP RFC from PHP thru message server

If you have the need to call your SAP R/3 system from PHP (which we do), you have several possibilities to do so. One of them is using the proprietary RFC protocol. Fortunately, there exists a PHP SAPRFC library for this purpose.

Because we want high availability and scalability in our SAP, we use multiple application servers and spread the load between them with help from a so called message server. Connection thru this message server has some special requirements, though, so I hope this short post will help somebody in the same position that we were.

Normally, you'd use something like this to connect:


<?php
$rfc = saprfc_open (array (
"ASHOST"=>"server",
"SYSNR"=>"01",
"CLIENT"=>"001",
"USER"=>"test",
"PASSWD" =>"test"
) );

?>


Where ASHOST is your application server hostname, SYSNR is the system number etc. Even if you are using a message server, you can still connect to any of your application servers like this directly.

With the message server however, you have to use different connection variables:


<?php
$rfc = saprfc_open (array (
"CLIENT"=>"001",
"USER"=>"test",
"PASSWD" =>"test",
"MSHOST"=>"server1",
"R3NAME"=>"ABC",
"GROUP"=>"PUBLIC"
) );

?>


Where MSHOST is the hostname of your message server, R3NAME is the three-letter system name and the GROUP parameter specifies a logon group to use. If you do not use logon groups, leave the parameter empty or use a " " string (blank space). Logon group PUBLIC is almost always defined as well.

But, here is the catch: this will not work by itself. You may have noticed, that in the connect parameters, we never specified which port to use to connect. This must be defined in you /etc/services file on the server, where you run the PHP code.

You must add a line to /etc/services specifying on what port does the message server listen on:


sapmsABC 3601/tcp


The service name is constructed as sapms<SID>, where the <SID> is the three-letter system name used in the R3NAME connection attribute. The port obviously must be the one you use. If you do not know the port number, it is most usually 3600 + system number. So if our system has system number 01, the port is 3601. If the system number is 40, the port is 3640.

After adding this line to your services definition, the connection should start working.

2 komentářů:

  1. HI Michal,

    I've followed your indications but still encountering following error

    LOCATION CPIC (TCP/IP) on local host
    ERROR partner '127.0.0.1:3300' not reached

    any suggestion would be appreciated.

    Regards,
    Said
    ReplyDelete
  2. Are you running the message server on the same host as you are running the PHP script. It looks so from the error messages. This is quite unusal, but of course could be valid.

    Please post here the contents of the saprfc_open call together with the contents of the variables and describe your landscape.
    ReplyDelete