// SAPRFC_TEST.PHP - $Id: saprfc_test.php,v 1.7 2004/07/15 05:42:52 koucky Exp $
// Test script for SAPRFC extension module for PHP 4 and PHP 5
//
// Features:
// - similar function as "Single Test" in SE37 transaction (Function Builder)
// - support login to application server or to logon group (using load balancing)
// - login data are saved in cookie on client side
// - select of function module from list of available RFC functions in target R/3 (* convention is supported)
// - display R/3 system information (system id, version R/3, ....) after succesfull login
// - possibility to set import parameters and fill internal tables
// - generate fully functional PHP script calling of the function module
// - call selected RFC function and show results
function print_header ($title)
{
echo "
$titleSAPRFC test script extension module for PHP 4 and PHP 5
";
}
function print_footer ()
{
global $RFC_SYSTEM_INFO;
if ($RFC_SYSTEM_INFO != "")
echo ("
$RFC_SYSTEM_INFO\n");
echo "";
}
function print_window ($title,$content,$bottom)
{
echo ("
Warning: The password is send as the clear text !!! Don't use this script in the Internet environment. Don't login to the productive system. \n";
print_header ("Login to SAP R/3");
print_window ("Login to SAP R/3",$form,"");
print_footer ();
}
function print_select_form ($function, $last_entries, $functions_list)
{
$form = "
\n";
print_header ("Selection of function module");
print_window ("Selection of function module",$form,"New login");
print_footer ();
}
function print_error_message ($title, $message, $debug)
{
print_header ($title);
echo ("
";
for ($i=0;$i%s\n",$def[$i]["name"]);
$form .= "
\n";
for ($j=0;$j%s\n",$value[$j][$def[$i]["name"]]);
$form .= "\n";
}
$form .= "
\n
\n";
return ($form);
}
//-----------------------------------------------------------------------------
// MAIN
//-----------------------------------------------------------------------------
if ( ini_get("register_globals") == false )
{
if ( isset ($_COOKIE) ) {
foreach ($_COOKIE as $var=>$val) {
$$var = $val;
}
} elseif ( isset ($HTTP_COOKIE_VARS) ) {
foreach ($HTTP_COOKIE_VARS as $var=>$val) {
$$var = $val;
}
}
if ( isset ($_POST) ) {
foreach ($_POST as $var=>$val) {
$$var = $val;
}
} elseif ( isset ($HTTP_POST_VARS) ) {
foreach ($HTTP_POST_VARS as $var=>$val) {
$$var = $val;
}
}
if ( isset ($_GET) ) {
foreach ($_GET as $var=>$val) {
$$var = $val;
}
} elseif ( isset ($HTTP_GET_VARS) ) {
foreach ($HTTP_GET_VARS as $var=>$val) {
$$var = $val;
}
}
}
if (! extension_loaded ("saprfc"))
{
print_error_message ( "SAPRFC extension not loaded",
"This script use SAPRFC extension module for PHP and the extension isn't loaded. You can download ".
"it with installation instructions from ".
"http://saprfc.sourceforge.net. If you have already the extension compiled and installed, check your ".
"php.ini configuration ",
""
);
exit;
}
if (isset($SAPRFC_LOGON)) // if cookie with login data, decode it
{
$SAPRFC_LOGON = urldecode($SAPRFC_LOGON);
$l = unserialize ($SAPRFC_LOGON); // l = array with login data (user,client, host....)
}
if (isset($SAPRFC_ENTRIES)) {
$SAPRFC_ENTRIES = urldecode($SAPRFC_ENTRIES); // cookie with last entries
$e = unserialize ($SAPRFC_ENTRIES); // e = array with last entries (selected funcions)
}
if (!isset ($p) ) $p="";
// if no cookie with login data exist or if newlogin request, show login page
if ( ! isset($SAPRFC_LOGON) && $p != "login" || $p == "newlogin" )
{
if (! isset($l["ASHOST"])) $l["ASHOST"] = "";
if (! isset($l["SYSNR"])) $l["SYSNR"] = "";
if (! isset($l["CLIENT"])) $l["CLIENT"] = "";
if (! isset($l["USER"])) $l["USER"] = "";
if (! isset($l["PASSWD"])) $l["PASSWD"] = "";
if (! isset($l["GWHOST"])) $l["GWHOST"] = "";
if (! isset($l["GWSERV"])) $l["GWSERV"] = "";
if (! isset($l["MSHOST"])) $l["MSHOST"] = "";
if (! isset($l["R3NAME"])) $l["R3NAME"] = "";
if (! isset($l["GROUP"])) $l["GROUP"] = "";
if (! isset($l["LANG"])) $l["LANG"] = "";
if (! isset($l["TRACE"])) $l["TRACE"] = "";
if (! isset($l["LCHECK"])) $l["LCHECK"] = "";
if (! isset($l["CODEPAGE"])) $l["CODEPAGE"] = "";
print_login_form ($l["ASHOST"], $l["SYSNR"], $l["CLIENT"], $l["USER"], $l["PASSWD"], $l["GWHOST"],
$l["GWSERV"], $l["MSHOST"], $l["R3NAME"], $l["GROUP"],
$l["LANG"], $l["TRACE"], $l["LCHECK"], $l["CODEPAGE"]);
exit;
}
else
{
if ( $p == "login" ) // process data from login page
{
if (isset($ASHOST)) $l["ASHOST"] = $ASHOST;
if (isset($SYSNR)) $l["SYSNR"] = $SYSNR;
if (isset($CLIENT)) $l["CLIENT"] = $CLIENT;
if (isset($USER)) $l["USER"] = $USER;
if (isset($PASSWD)) $l["PASSWD"] = $PASSWD;
if (isset($GWHOST)) $l["GWHOST"] = $GWHOST;
if (isset($GWSERV)) $l["GWSERV"] = $GWSERV;
if (isset($MSHOST)) $l["MSHOST"] = $MSHOST;
if (isset($R3NAME)) $l["R3NAME"] = $R3NAME;
if (isset($GROUP)) $l["GROUP"] = $GROUP;
if (isset($LANG)) $l["LANG"] = $LANG;
if (isset($TRACE)) $l["TRACE"] = $TRACE;
if (isset($LCHECK)) $l["LCHECK"] = $LCHECK;
if (isset($CODEPAGE)) $l["CODEPAGE"] = $CODEPAGE;
$SAPRFC_LOGON = serialize ($l); // set cookie variable
$p = "select"; // switch to selection of function module
}
}
// login data avaible, can open connection to SAP R/3
$rfc = @saprfc_open ($l);
if (! $rfc ) // if login failed, show error message
{
if (isset ($l["use_load_balancing"]))
print_error_message ( "Login error",
"Can't login to client ".$l["CLIENT"]." of the system ".$l["R3NAME"]." (Host: ".$l["MSHOST"].", Group: ".$l["GROUP"].") as user ".$l["USER"].". New login",
saprfc_error()
);
else
print_error_message ( "Login error",
"Can't login to client ".$l["CLIENT"]." and host ".$l["ASHOST"]." (System number: ".$l["SYSNR"].") as user ".$l["USER"].". New login",
saprfc_error()
);
exit;
}
// if SAP codepage set, set codepage for opened rfc connection
//if ($l[codepage]!="")
// saprfc_set_code_page ($rfc,$l[codepage]);
// save login data as cookie at the client side
setcookie ("SAPRFC_LOGON", urlencode($SAPRFC_LOGON),time()+7200);
// retrieve a system information from connected R/3
// use function module RFC_SYSTEM_INFO
// discover interface of function module and set resource $sysinfo_fce
$sysinfo_fce = @saprfc_function_discover ($rfc,"RFC_SYSTEM_INFO");
if ($sysinfo_fce)
{
// do RFC call to connected R/3 system
$retval = @saprfc_call_and_receive ($sysinfo_fce);
if ($retval == SAPRFC_OK)
{
// retrieve export (output) parametr RFCSI_EXPORT
$sysinfo = saprfc_export ($sysinfo_fce,"RFCSI_EXPORT");
$RFC_SYSTEM_INFO = sprintf ("system id: %s (%s), client=%03d, user=%s, application server=%s (%s,%s,%s), database=%s (%s)",
$sysinfo["RFCSYSID"],$sysinfo["RFCSAPRL"],$l["CLIENT"],$l["USER"],$sysinfo["RFCHOST"], $sysinfo["RFCOPSYS"],
$sysinfo["RFCIPADDR"],$sysinfo["RFCKERNRL"], $sysinfo["RFCDBHOST"], $sysinfo["RFCDBSYS"] );
}
// free allocated resources
@saprfc_function_free ($sysinfo_fce);
}
if ( $p == "select_do" ) // action for selection of function module page
{
if (isset ($list) && $list != "") // if function from function list selected, get it
{
$function = $list;
$p = "input";
}
elseif (isset ($entry) && $entry != "") // if function from last entries selected, get it
{
$function = $entry;
$p = "input";
}
else
{ // other, use function module RFC_FUNCTION_SEARCH to
// get a list of RFC functions of target R/3
$search_fce = saprfc_function_discover ($rfc,"RFC_FUNCTION_SEARCH");
if ($search_fce)
{
saprfc_import ($search_fce,"FUNCNAME",$function); // you can use asterix *, e.g. RFC_*
saprfc_optional ($search_fce,"GROUPNAME",1); // set optional parameters, the default values will be used
saprfc_optional ($search_fce,"LANGUAGE",1);
saprfc_table_init ($search_fce,"FUNCTIONS"); // init internal table named FUNCTIONS, equivalent of abap
// commands: refresh functions; clear functions;
$retval = @saprfc_call_and_receive ($search_fce); // rfc call
$numrows = saprfc_table_rows ($search_fce,"FUNCTIONS"); // get number of rows internal table
if ($numrows == 0) // no function found, show selection page again
$p = "select";
elseif ($numrows == 1)
{ // exactly one function found, show input page
$row = saprfc_table_read ($search_fce,"FUNCTIONS",1);
$function = $row["FUNCNAME"];
$p = "input";
}
else
{ // retrive list of founded function to array
for ($i=1;$i<=$numrows;$i++) // note: lines of internal tables are indexed from 1, (e.g 1,2,3....)
{
$row = saprfc_table_read ($search_fce,"FUNCTIONS",$i);
$functions_list[$i-1] = $row["FUNCNAME"];
}
$p = "select"; // show selection page again
}
saprfc_function_free ($search_fce);
}
else // error, can't discover interface of RFC_FUNCTION_SEARCH
$p = "select";
}
}
if ( $p == "select" || $p == "") // selection of the function module page
{ // show also if have login data from cookie (p=="")
if (! isset ($function) ) $function="";
if (! isset ($e) ) $e="";
if (! isset ($functions_list) ) $functions_list="";
print_select_form ($function, $e, $functions_list);
}
if ( $p == "input" ) // input values for import parameters or internal tables
{ // of function module $function
if (!isset ($action) ) $action = "";
if ($action == "") // if enter from selection page, set last entries cookie
{
if (! isset ($e) || ! is_array ($e)) $e = array();
$t_e = $e;
unset ($e);
$e[0] = strtoupper($function);
foreach ($t_e as $t_func) {
if (strtoupper($function) != $t_func) $e[] = $t_func;
}
unset ($t_e);
$SAPRFC_ENTRIES=serialize($e);
setcookie ("SAPRFC_ENTRIES", urlencode($SAPRFC_ENTRIES),time()+3600);
}
$fce = @saprfc_function_discover ($rfc,$function); // discover interface of function module $function
if (! $fce )
{
print_error_message ( "Discovering function module error",
"Can't discover interface of function module $function. Please check if the module is Remote-enable (transaction SE37, Atrributes).Other function module",
saprfc_error()
);
exit;
}
$def = @saprfc_function_interface($fce); // retrieve definition of interface in array $def
// $def has following format:
/* $def = array (
array ("name"=>"PARAMETER", // name of import, export parameter or internal table
"type"=>"IMPORT", // type: IMPORT, EXPORT, TABLE
"optional"=>0 // 1 - optional parameter
"def"=> typedef // type definition (elementary SAP data type or structure)
),
............
)
typedef = array (
array ("name"=>"item1" "abap"=>"C", "len"=>30, "dec"=>0),
array ("name"=>"item2" "abap"=>"I", "len"=>4, "dec"=>0),
.....
)
*/
if ($action == "Call $function") // switch to call/result page if Call button pressed
$p = "call";
elseif ($action == "Generate PHP")
{
Header ("Content-type: application/x-httpd-php-source\n");
Header ("Content-Disposition: inline; filename=${function}.php\n");
echo ("\n//RFC Call for $function\n");
echo ("//Generated by saprfc_test.php (http://saprfc.sourceforge.net)\n\n");
echo ("//Login to SAP R/3\n");
echo ("\$login = array (");
$first = true;
foreach ($l as $key => $value)
{
if ($value == "") continue;
if ($first)
{$first = false; echo ("\n");}
else
echo (",\n");
echo ("\t\t\t\"$key\"=>\"$value\"");
}
echo (");\n");
echo ("\$rfc = saprfc_open (\$login );\n");
echo ("if (! \$rfc ) { echo \"RFC connection failed\"; exit; }\n");
echo ("//Discover interface for function module $function\n");
echo ("\$fce = saprfc_function_discover(\$rfc,\"$function\");\n");
echo ("if (! \$fce ) { echo \"Discovering interface of function module failed\"; exit; }\n");
echo ("//It's possible to define interface manually. If you would like do it, uncomment following lines:\n");
echo ("/*\$def = array (\n");
for ($i=0;$i\"".$interface["name"]."\",\n");
echo (" \t\t\t\t \"type\"=>\"".$interface["type"]."\",\n");
echo (" \t\t\t\t \"optional\"=>\"".$interface["optional"]."\",\n");
echo (" \t\t\t\t \"def\"=> array (\n");
$typedef = $interface["def"];
for ($j=0; $j\"".$typedef[$j]["name"]."\",\"abap\"=>\"".$typedef[$j]["abap"]."\",\"len\"=>".$typedef[$j]["len"].",\"dec\"=>".$typedef[$j]["dec"].",\"offset\"=>".$typedef[$j]["offset"].")");
if ($j < (count($typedef)-1) )
echo (",\n");
else
echo ("\n");
}
echo (" \t\t\t\t\t)\n");
echo (" \t\t\t)");
if ($i < (count($def)-1) )
echo (",\n");
else
echo ("\n");
}
echo (" \t\t); \n");
echo (" \$fce = saprfc_function_define(\$rfc,\"$function\",\$def);\n");
echo ("*/\n");
echo ("//Set import parameters. You can use function saprfc_optional() to mark parameter as optional.\n");
for ($i=0;$i $value)
{
if ($first)
{$first = false;}
else
echo (",");
echo ("\"$key\"=>\"$value\"");
}
echo ("));\n");
}
}
}
echo ("//Fill internal tables\n");
for ($i=0;$i $value)
{
if ($first)
{$first = false;}
else
echo (",");
echo ("\"$key\"=>\"$value\"");
}
echo ("));\n");
}
}
}
}
echo ("//Do RFC call of function $function, for handling exceptions use saprfc_exception()\n");
echo ("\$rfc_rc = saprfc_call_and_receive (\$fce);\n");
echo ("if (\$rfc_rc != SAPRFC_OK) { if (\$rfc == SAPRFC_EXCEPTION ) echo (\"Exception raised: \".saprfc_exception(\$fce)); else echo (saprfc_error(\$fce)); exit; }\n");
echo ("//Retrieve export parameters\n");
for ($i=0;$i\n";
$form .= "\n";
$form .= "\n";
$form .= "
\n";
$form .= "
IMPORT PARAMETERS
\n";
for ($i=0;$i\n";
$var="RFC_TABLE_".$interface["name"];
if ( !isset ($$var) ) $$var="";
$vararray = unserialize (urldecode($$var)); // get saved array of values from RFC_TABLE_*
$var="RFC_".$interface["name"];
if ($action=="Append ".$interface["name"]) // if Append button pressed, add new line to internal table (array)
$vararray[]=$$var;
$form .= show_table ($interface["name"],$interface["def"],$vararray);
}
}
$form .= "
\n";
$form .="
\n";
$form .= "
\n";
print_window ("Function module $function",$form,"BackNew loginSelect other function");
print_footer();
}
}
if ( $p == "call" ) // set input parameters and tables, call function module, show results
{
for ($i=0;$iOther function module",
saprfc_error()
);
exit;
}
print_header ("Results of function module $function");
$form = "
";
print_window ("Results of function module $function",$form,"BackNew loginSelect other function");
print_footer();
}
// free resources and close rfc connection
@saprfc_function_free($fce);
@saprfc_close($rfc);
?>