Connecting SAP and PHP

SAP is said to be the most comprehensive and widely-used Enterprise Resource Planning system ever. Due to its widespread use, SAP users and developers come across a variety of situations where they need to connect an installation of SAP to a different platform/programming language so that the data and data interpretations contained in relation to that installation can be accessed by that platform or programming language. Hence a myriad of SAP connectors for different programming languages have been born.

PHP, being the most widely used server-side programming language on the web, is one of the most probable languages that would need to connect to SAP at some point or the other. For this, the extremely handy SAPRFC module for PHP has been developed. This post will discuss an end-to-end implementation of the SAPRFC PHP module and explain how to connect SAP to the module as well.

The first step is to go to the above link and download the complete saprfc package. The easiest way to interface saprfc with PHP and to get it to work is to download XAMPP 1.7.1 (NOT the latest version). This contains PHP 5.28 which seems to be the only PHP version which works correctly with the SAPRFC module.

Unzip XAMPP and copy php_saprfc_528.dll from your saprfc/ext folder over to the /php/ext/ folder in your XAMPP directory. Locate the php.ini file in XAMPP and open it for editing. Here you will have to find the extensions section and add the line


to this section. Now open the XAMPP Control Panel and start Apache. If you followed the above steps correctly, Apache should start now without any problems. IF it gives an error, go over the above steps again.

First of all, we’ll see what we need to do on the SAP side to expose a Remote Function Module which can be accessed via RFC. All you need to do is to create a ‘remote-enabled’ Function Module in SAP. This can be done by ticking ‘Remote-enabled module’ under the function module’s attributes, as shown below:


You can write whatever program logic in ABAP in the Function Module. Pay special attention to the Importing and Exporting parameters of the Function Module as these will be what are passed to and from your PHP program.

Next, you need to copy over the saprfc.php library file over from the saprfc module folder to your PHP project directory. You can use the functions contained in this file to create a saprfc object which can login to SAP and access your remote-enabled Function Module.

The following code is essential for your PHP program to access SAP:

$submitted = $_GET['submitted'];
 * Login to SAP system
 * @param String $user
 * @param String $pwd
function login($user, $pwd) {
	//Create SAPRFC instance
	$sap = new saprfc(array(
			"logindata" =>; array(
			      "ASHOST"	=>; "HOSTNAME",
			      "SYSNR"	=>; "SYSTEM NUMBER",
			      "CLIENT"	=>; "CLIENT NUMBEr",
		              "USER"	=>; "USERNAME",
			      "PASSWD"	=>; "PASSWORD"
			"debug"	     =>;false));

	return $sap;
function logoff($sap) {
 * Function to call SAP RFC
 * @param saprfc $sap
function callRFC($sap, $params) {
	$cust_params = $params['cust_params'];
	$task_params = $params['task_params'];
	$proj_params = $params['proj_params'];
	$result = $sap->;callFunction("ZGRAPH_TOTALDAYS_RFC",
	return $result;

Note the callRFC function: Importing parameters on the SAP RFC are passed to the $sap->callFunction function as “IMPORT” and Exporting parameters are defined as “EXPORT”. By leaving the “debug” value as ‘true’ on the login function, it is possible to get debug output for each SAP RFC call, as seen here:

SAP PHP Debug Output

Now you’re ready to go out in to the world with your own PHP program that connects to SAP! Good Luck!

Here’s a final look at what my program looks like. It connects to SAP to pull out project data for reporting using the JavaScript HighCharts library:

Charts from SAP Data in PHP!

Misadventures of a SAP n00b

SAP is considered to be the best Enterprise Resource Planning system in the world today, and there are thousands of SAP implementations in many mega-corporations around the world.

I had the opportunity to mess around (at a very beginner level) with ABAP and some Z Programs on the SD and MM modules, and would like to document some of the things I learnt. Especially after reading an awesome SAP blog by my friend Jeewana Premarathne at, I thought of documenting my (rather minimal) knowledge as an ABAP programmer on my blog as well.

1. Search keyword chaining

Just like with any other keyword, it is possible to chain the search keyword as well to search for multiple strings at the same time. If any of the search strings are found within the target string, sy-subrc will be set to 0.


SEARCH target_string FOR:
  'Search string 1',
  'Search string 2'.
IF sy-subrc = 0.
  "Do something if search string is found

2.  Some useful naming conventions:

3.   Function to give the user a dropdown list to choose a file from the server to save to

  directory = 'directory on server'
  filemask = ' '
  serverfile = "return variable name
  canceled_by_user = 1

IF sy-subrc <> 0.
  MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
  WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.

The above code will give the user a dropdown list to choose a file from. You can use this file as a variable within the program to write data to or read from. If the file is not available on the server or the directory path you provided is not available, this will result in a dump, so this should be handled accordingly.

4. Using OPEN DATASET to write to a file

Once you have obtained the path to the file, you can use the OPEN DATASET keyword to open this file for writing, and simply write a character string or an internal table to it.

"Open file for writing

  IF sy-subrc = 0.
    * Write output string to file.
    TRANSFER t_output TO output_file.
    WRITE 'The file could not be written.'.
CLOSE DATASET output_file.

5.   Concatenating char/string type variables

For this, it is possible to use the CONCATENATE keyword. If you want to separate the strings using a delimiter, this can be arranged by adding the SEPARATED BY keyword.

INTO lv_final_string SEPARATED BY 'delimiter'.

That’s about it for now from me. Since I’m such a newbie to this, there may be some mistakes. It should also be noted that SAP ABAP programming is not simply about learning the code and programming away like in other programming languages, but you have to have a thorough understanding of the business involved, as well as its processes. More on that later.