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

extension=php_saprfc_528.dll

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:

SAP RFC

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:

require_once('saprfc.php');
$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"
					),
			"show_errors"=>;true,
			"debug"	     =>;false));

	return $sap;
}
function logoff($sap) {
	$sap->;logoff();
}
/**
 * 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",
             array(
                   array("IMPORT","CUST_PARAMS",$cust_params),
                   array("IMPORT","TASK_PARAMS",$task_params),
                   array("IMPORT","PROJ_PARAMS",$proj_params),
                   array("EXPORT","CATEGORIES",array()),
                   array("EXPORT","DATA_ACTUAL",array()),
                   array("EXPORT","DATA_ESTIMATE",array())
				       )
					);
	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!

One thought on “Connecting SAP and PHP”

Comments are closed.