Getting started with PHP and SAP


So you might be an experienced ABAPer or even an experienced PHPer, but do you know how to get the basic development environment setup to actually connect your PHP application to your SAP backend system?

This is actually one of the most confusing parts, simply because of the various version levels and pieces of the puzzle. I decided to jot down some notes on this setup and what better way to preserve my notes than by sharing them with others? Therefore here’s the next blog in my series on discovering the flexibility of PHP together with SAP.Now with the onset of web services within a SAP 6.20+ system it would be best to choose PHP5 due to the fact that it’s quite easy to simply start using web services, as opposed to PHP4 where you needed to add in some extensions and modules. So we are going to need the following items:

This will give us the best combination of being able to use both web services as well as RFC calls to our SAP system. Once you have those downloaded you can install them to your drive.

I’ve setup a folder under my C: drive called “Development” and I’ve installed first the Apache there, by installing I basically just unpacked it there. Then I setup my PHP to reside in a directory under Apache. The reason for this is simple, I have it all together and it’s not to difficult to move it later if I need to.

Now just unpack your SAPRFC, you can do this straight to the htdocs directory under Apache.

Now with everything unpacked we just need to do a bit of configuration and we can get started with a simple application.

httpd.conf file located under apacheconf:

I’m not going to go through each line of the config just the ones related to connecting PHP to it and getting it to start up, all of these lines are documented within the file so you should be OK there.


ServerRoot “C:/Development/Apache”
LoadModule php5_module “C:DevelopmentApachePHPphp5apache2.dll”
DocumentRoot “C:/Development/htdocs”
SetEnv PHPRC “C:/Development/Apache/PHP”
PHPIniDir “C:/Development/Apache/PHP”
ScriptAlias /cgi-bin/ “C:/Development/Apache/cgi-bin/”
ScriptAlias /PHP/ ” C:/Development/Apache/PHP/”
AddType application/x-httpd-php .php .phtml
Action application/x-httpd-php “/php/php-cgi.exe”

So with my Apache configuration all taken care of, we need to take a look at the PHP config. That’s the php.ini file located inder Apache/php. This one doesn’t require too much just a couple of lines need to modified to ensure that PHP can find the SAPRFC module.


include_path = “.;C:DevelopmentApachePHPincludes;C:DevelopmentApachePHPpear”
extension_dir = “C:DevelopmentApachePHPext”
extension=php_mbstring.dll
extension=php_gd2.dll
extension=php_mysql.dll
extension=php_saprfc.dll
extension=php_soap.dll

You should find each of those lines OK, most you may only need to remove the ; in front of it.

Then I also copied the php_saprfc.dll to the extension directory listed above. Just copy the file from Apache/htdocs/saprfc.

With those settings all done you should be able to start your Apache server now from Apache/bin/apache.exe, provided there are no typos your server should now be started and if you type http://localhost into your browser you should get the intial Apache page.

Now to ensure that SAPRFC is loaded properly, you will need to create a simple PHP page under Apache/htdocs. Call the page “phpinfo.php” and the contents should be:


<?php
// Show all information, defaults to INFO_ALL
phpinfo();

// Show just the module information.
// phpinfo(8) yields identical results.
phpinfo(INFO_MODULES);
?>

If you then load that page in your browser, “http://localhost/phpinfo.php&#8221; you should get a page displaying all of the information about your server and PHP including the modules loaded. Just scroll down until you find SAPRFC.

If it’s there then we can get started and try out a simple connection to our SAP backend. If you don’t have a backend SAP system readily available I would recommend you download the free preview additon of the 6.40 system from the SAP Developer Network, it takes only a few minutes to install and then you will be able to test to your heart’s content. The latest as of this blog was the 6.40 Full ABAP edition with Web Dynpro for ABAP.

I’ve already downloaded and installed as well as the SAP GUI for logging into it. The GUI is actually the other key element for connecting using RFC to an SAP system as you need the “librfc32.dll” which comes with the GUI loaded into your system directory.

From this point on I will assume that you have your SAP system as well as your GUI installed or accessible and we will now create a simple connection to the system using PHP.


<html>
<body>
<h1>SAPRFC-Class: Get List of Users in SAP-System</h1>
<?
// Example for using the saprfc-class-library for accessing sap-functions via rfc
// provided by lars laegner, btexx business technologies, august 2001
// !!!! PLEASE CHANGE THE LOGINDATA TO YOUR SAP-SYSTEM !!!!

// $Id: example_userlist.php,v 1.2 2001/08/16 15:54:35 llaegner Exp $

// saprfc-class-library
require_once("saprfc.php");

// Create saprfc-instance
$sap = new saprfc(array(
"logindata"=>array(
"ASHOST"=>"localhost" // application server
,"SYSNR"=>"00" // system number
,"CLIENT"=>"000" // client
,"USER"=>"bcuser" // user
,"PASSWD"=>"minisap" // password
)
,"show_errors"=>false // let class printout errors
,"debug"=>false)) ; // detailed debugging information

// Call-Function
$result=$sap->callFunction("SO_USER_LIST_READ",
array( array("IMPORT","USER_GENERIC_NAME","*"),
array("TABLE","USER_DISPLAY_TAB",array())
));

// Call successfull?
if ($sap->getStatus() == SAPRFC_OK) {
// Yes, print out the Userlist
?><table>
<tr><td>SAP-Name</td><td>User-Nummer</td></tr><?

foreach ($result["USER_DISPLAY_TAB"] as $user) {
echo "<tr><td>", $user["SAPNAM"],"</td><td>",$user["USRNO"],"</td></tr>";
}

?></table><?
} else {
// No, print long Version of last Error
$sap->printStatus();
// or print your own error-message with the strings received from
// $sap->getStatusText() or $sap->getStatusTextLong()
}

 

// Logoff/Close saprfc-connection LL/2001-08
$sap->logoff();
?>
</body>
</html>

The example I chose is one of the standard ones that comes with the SAPRFC module, example_userlist.php. In order to use it as well you need to ensure that the “saprfc.php” file can be located. This file is the main class file containing all the little bits and pieces you need to connect, thus making life much easier.


// saprfc-class-library
require_once(“saprfc.php”);

As for editing and working with your PHP files I tend to use Eclipse with PHP Eclipse mainly because I still do Java and Perl development. However, if you are strictly PHP you might want to check out one of these editors, located at Snapfiles or jump over to PHP.net there you’ll find some good links for editors as well. There are many choices available so you will need to choose the one that best fits your needs.

Remember in terms of RFC connections to SAP you need to ensure your function module (created internally or by SAP) is “remote enabled” otherwise you won’t be able to connect to it.

I think that covers the basics for now and should be something to get you started…

Tags: ,

About ccmehil

A developer, writer, photographer, talker - words that describe Craig, but who is Craig? Craig is a person driven to interact and communicate, always looking to innovate over the years he's been through all spectrums of the development process for all platforms. He's built communities and helped define strategies to communicate and engage. Now though his focus has become more precise... With a major emphasis on interaction and collaboration Craig spends a lot of time both speaking and writing about effects of social in the work place and ways to improve efficiency through the use of "social media" in the day to day work you do.

39 responses to “Getting started with PHP and SAP”

  1. ilektro says :

    I have read this blog with much interest. I had a Windows server in the same network as SAP and all worked fine (on SAPRFC level). As we had speed issues, we decided to move all to an external Linux server. Since then the SAP connections are very unstable.

    When we log in, it mostly doesn’t work and only when refreshing the page it works. We get no error number or specific error message.

    Is there any logs that SAPRFC saves to check what happens?

    We use
    PHP 5.2.9
    SAPRFC 1.4.1
    on Linux

    ***

    Not sure you saw the new post but unfortunately due to time constraints this blog has moved – feel free to pop over to the new location.

    https://enterprisegeeks.com/2006/03/20/getting-started-with-php-and-sap/

    As for the logs PHPRFC should have one “dev_trc” and are there entries in ST22?

    ***

    I have asked the hosting company as we now have a managed server.

    It is now clear that every customer needs to refresh the page to be able to establish a connection with SAP through the VPN tunnel.

  2. ilektro says :

    hosting comp doesn’t find a file with name dev_trc and asks what we mean with ST22

  3. Craig Cmehil says :

    Hi ilektro, ST22 is the transaction code in your SAP system (SAPGUI). The “dev_trc” will be a file found most likely in your webserver “root” directory or the root directory of the application itself.

    So what I am not clear about is what you have running, is it NetWeaver or what version of SAP? Is it your application that is now running on Linux and SAP or just the application?

  4. ilektro says :

    SAP : Version ERP 5.00 Kernel 640

    On Linux we have the GUI installed but SAP is on a remote server linked up with a VPN tunnel.

    Hope I get your question right.

    Thanks for your (quick) replies by the way !!! I’m in big trouble 😦

  5. ilektro says :

    two lines as example (other lines have low duration):

    |14:11:09.069|1.459.562|SAPLZBA |prddb_PRD |Server | | | 1| 0| |vps.testserver.com prddb_PRD_00 Server ZBAPI_CUSTOMER_GETINFO2 5.976 251

    |14:13:44.941|1.578.382|SAPL2032|prddb_PRD |Server | | | 1| 0| |vps.testserver.com prddb_PRD_00 Server BAPI_SALESORDER_CREATEFROMDAT2 5.120 5.902

  6. Craig Cmehil says :

    OK if you are using a VPN to connect your app to the SAP system then you’ll need to ensure that the VPN connection is open, this is the refresh that your users have to do.

    There are a few ways to deal with that but it all depends on your app and of course your VPN and server setup.

    Best tip I can give is to look at the connection between your server, VPN and SAP system and look at the “timeout” settings.

  7. ilektro says :

    that also was our first thought

    we have raised the time out already to 24hours and put a ping every second on it to keep it open. we have a tracking on it and it shows no downtimes. we also have tracking on a page that shows the SAP connection and that also has no failures ( $rfc = saprfc_open ($l); )

    that is why we thought that a particular Bapi is causing an issue of response time. eg SAPRFC only requests once and if it doesn’t come immediately, it doesn’t connect. is that possible?

    or .. any other possibilities?

  8. ilektro says :

    the hosting company confirms that dev_trc cannot be found on the server

    [root@srv ~]# updatedb
    [root@srv ~]# locate dev_trc
    [root@srv ~]#

  9. ilektro says :

    Found this in the PHP error logs many times:

    [Tue Apr 07 12:19:45 2009] [error] [client 83.37.79.157] PHP Warning: RFC Error Info :
    Group : 102
    Key : RFC_ERROR_COMMUNICATION
    Message : SAP_CMINIT3 : rc=20 > Connect to SAP gateway failed
    Connect_PM GWHOST=GWSERV=, GWSERV=sapgw20, ASHOST=100.100.100.58, SYSNR=20

    LOCATION CPIC (TCP/IP) on local host
    ERROR hostname ‘GWSERV=’ unknown
    TIME Tue Apr 7 12:19:45 200
    RELEASE 640
    COMPONENT NI (network interface)
    VERSION 37
    RC -2
    MODULE niuxi_mt.c
    LINE 388
    DETAIL NiPGetHostByName2: hostname ‘GWSERV=’ not found
    SYSTEM CALL gethostbyname_r
    COUNTER 1

    in /var/www/vhosts/testserver.com/httpdocs/cat/cart.php on line 19, referer: https://www.testserver.com/cat/search.php

    ***

    [Tue Apr 07 12:19:25 2009] [error] [client 84.37.79.167] PHP Warning: RFC Error Info :
    Group : 101
    Key : RFC_ERROR_PROGRAM
    Message : Missing R3NAME=… or ASHOST=… in connect_param in RfcOpenEx
    in /var/www/vhosts/testserver.com/httpdocs/cat/index.php on line 96, referer: https://www.testserver.com/cat.php

    Does that help?

  10. Craig Cmehil says :

    You need to specifically activate “debug” for SAPRFC to get the dev_trc files. But it looks like a network issue and not an SAP or PHP one.

  11. ilektro says :

    I have searched but didn’t find info. Can you tell me how to do that in the SAPRFC module of PHP? (Koucky)

    Would it be that SAP doesn’t send the info if it considers the duration too high (for certain bapis > 1.5 as mentioned in the comments above). Can that limit be changed?

  12. Craig Cmehil says :

    At a lose myself, no system at the moment to do any testing, have you tried the PHP forum over on http://sdn.sap.com/irj/sdn/forums ? If not I will check as soon as I can.

  13. ilektro says :

    thanks for the tip, I will check it out!

  14. Nimz says :

    Craig,

    I installed APACHE 2.2 + PHP 5.2.9 and configured with httpd.conf and php.ini.
    Both are working fine.
    I got the SAPRFC. and copied the php_saprfc.dll file to the c:phpext directory and also set the extension_dir = “c:phpext” in php.ini.

    but still not able to see SAPRFC in phpinfo .

    could you please tell me what i have missed?

    Regards,
    nimz

  15. Craig Cmehil says :

    Where is your librfc32.dll on your system that is required as well.

  16. nimz says :

    its in C:WINsystem32

  17. Craig Cmehil says :

    You can also try c:windowssystem32 the directory may not exist but I have seen that solve the problem in the past. But typically the problem you are having is because the SAPRFC.dll can’t find the lib – do you have SAP GUI installed?

  18. nimz says :

    Yes it was already installed . Version 640.

  19. Craig Cmehil says :

    Are you getting any specific errors?

  20. nimz says :

    No specific error !!
    I did following steps.
    —SAP GUI 6.40 already installed.
    1. installed APACHE 2.2 in c:/program files/
    2. checked its successfully installed by their default page.
    3. installed PHP 5.2.9 on c:/php/
    4. executed phpinfo() and its displayed info.
    —–Upto this stage both are running fine.
    5. downloaded SAPRFC 1.4.1 from sourceforge.net and extracted to htdocs folder of installed Apache.
    – copied php_saprfc.dll to c:phpext
    – add line “extension = php_saprfc.dll” in php.ini

    Am I doing right way?

    Regards,
    nimz

  21. Craig Cmehil says :

    So far that sounds perfectly correct, you might want to hit the forums over on http://sdn.sap.com I know that Piers Harding was working on a new version of the SAPRFC as it might be an issue here for your PHP version and SAP GUI version.

  22. SEDINSKI Willy says :

    i don’t speak very well english, but i would like how i can use “order by” for the result in my php code :

    $TRAITEMENT=$sap->callFunction(“RFC_READ_TABLE”,
    array(
    array(“IMPORT”,”QUERY_TABLE”,”TBTCO”),
    array(“IMPORT”,”DELIMITER”,”;”),
    array(“IMPORT”,”NO_DATA”,” “),

    array(“TABLE”,”OPTIONS”,
    array(

    array(“TEXT”=>”STRTDATE = ‘$jour_en’ and SDLUNAME ‘OXYA'” ),
    array(“ORDER”=>”STRTTIME”),
    )),

    array(“TABLE”,”FIELDS”,
    array(
    array(“FIELDNAME”=>”JOBNAME”),
    array(“FIELDNAME”=>”SDLUNAME”),
    array(“FIELDNAME”=>”STRTDATE”),
    array(“FIELDNAME”=>”STRTTIME”),
    array(“FIELDNAME”=>”ENDDATE”),
    array(“FIELDNAME”=>”ENDTIME”),
    array(“FIELDNAME”=>”STATUS”),
    )),

    array(“TABLE”,”DATA”,
    array()),

    ));

    Thanks for you repply.

    Best regards.
    Willy

  23. Craig Cmehil says :

    Do you mean when calling the RFC or after you have the results? Which might be easier depending on the amount of data you are pulling.

  24. Alex says :

    I would like to know how I transfer data from SAP with PHP (or PowerBuilder for example) through RFC.

  25. Craig Cmehil says :

    HI Alex, using RFC you can access data from within the SAP system; pull it into your PHP application (or Ruby, Python, Perl, Flex, etc) make changes and then put back – it’s all a matter of using the proper Function modules, Classes or BAPI’s

  26. SEDINSKI Willy says :

    hello,
    I have data in my table, but are not sorted, how do I get them sorted by date and time, should save this data in my mysql database and sort them later?
    thanks,
    Best regards,
    Willy

  27. Craig Cmehil says :

    Hi Willy,

    You can pull it into an Array and sort it then without a secondary storage (e.g. MySQL) or are you pulling data out to work with?

  28. Alex says :

    Hi…

    PowerBuilder since I could read data from SAP. I could not backwards, from accessing SAP PowerBuilder.
    If you have an example of how to extract data from SAP to PHP or another language. For RFC or another.
    Thanks a lot.

  29. Craig Cmehil says :

    Tons of examples Alex, check this post for some links:

    https://enterprisegeeks.com/2009/09/24/sap-and-php/

  30. Lisa says :

    I have tried to setup SAPRFC with both WAMP 2.1 and XAMPP 1.7.4.

    Both include PHP 5.3.5 and both give me the error:
    “PHP startup: saprfc: unable to initialize module
    Module compiled with build ID = API20090626, TS
    PHP compiled with build ID = API20090626, TS, VC6
    These options need to match”

    Where can I get a non-VC6 PHP?

  31. Sandy Troxil says :

    I really enjoy looking through on this site, it has great articles .

  32. cgova says :

    hi, i know this post is old. but know im starting with this, it turns out i downloaded the apache, php and saprfc with the same version you did. then when i restart my services in wampserver.. it doesn´t work, when i go to localhost its not loading the page. the thing is, i have the same version, i put the extension in the php.ini, then past it in the php/ext folder and i downloaded the librfc32 and put it in windows folder.

    so i hope you can help me!
    thank you.

  33. cgova says :

    hi, its me again. i changed the apache version to 2.0.52 so, i fix the problem with the server… now its working fine. but i still have the trouble with the extension php_saprfc.

    when im going to ¨localhost¨ i can´t see that extension loaded. ints not there.

    i did the procedure right, i guess! i put the extension at php/etc, i put the library at windows folder, i have the sap gui in the computer and add the extension to the php.ini.

    so i dont know how to fix the trouble, without that library i cant do anything…

    thanks.

  34. Craig Cmehil says :

    OK BUT what version of the RFC library do you have? This above was all based on NetWeaver 6.2 to 6.4 I believe.

  35. jos says :

    Hi, i have some problem with SAPRFC.
    I followed you advice to install PHP and SAP But i got this error
    “PHP startup: saprfc: unable to initialize module
    Module compiled with build ID = API20060613, debug=0, thread-safety=1
    PHP compiled with build ID = API20050922, debug=0, thread-safety=1
    These options need to match”
    I don’t know how to fix this problem )= .
    Thanks

  36. ccmehil says :

    A lot of things have changed since I first posted this – the error sounds like your SAPRFC, lib files and your PHP version do not match.

  37. Gustavo says :

    Can someone help me with the parameters sintax for the BAPI_SALESORDER_CREATEFROMDAT2 in PHP?

    Thanks

Trackbacks / Pingbacks

  1. SAP and PHP 2nd Edition | Friday Morning Report - November 21, 2013
  2. SAP and PHP | EnterpriseGeeks - May 27, 2014

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: