SAP R/3 форум ABAP консультантов
Russian ABAP Developer's Club

Home - FAQ - Search - Memberlist - Usergroups - Profile - Log in to check your private messages - Register - Log in - English
Blogs - Weblogs News

Использование Perl для доступа к SAP



 
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Connect to External system, Unix и Perl
View previous topic :: View next topic  
Author Message
admin
Администратор
Администратор



Joined: 01 Sep 2007
Posts: 1639

PostPosted: Sun Sep 02, 2007 4:33 pm    Post subject: Использование Perl для доступа к SAP Reply with quote

SAP_RFC - Perl extension to Interface with SAP RFC

Permission to use, copy, and distribute without charge this software, documentation etc. is granted, provided that this comment and the author's name is retained. Neither the author or Abapers.com assume any responsibility for problems resulting from the use of this software, documentation. SAP and ABAP/4 are copyrights of SAP AG

SYNOPSIS

Code:
use SAP_RFC;
$ret = &SAP_RFC::open($system, $client, $user, $passwd, $trace);
$ret = &SAP_RFC::close();
($ret, $string) = &SAP_RFC::ping();
($ret, $string) = &SAP_RFC::callrx1($abap, $string1);
($ret, $string1, $string2) = &SAP_RFC::callrx2($abap, $string3);
($ret, $string) = &SAP_RFC::callrxx($abap, $string1, $from_rec, $to_rec);
$string = &SAP_RFC::help();
($ret, $string) = &SAP_RFC::sysinfo();
$ver = &SAP_RFC::version();


DESCRIPTION
SAP_RFC is an extension to Perl which allows access to SAP's RFC interface through a scripting mechanism. This extension requires that you have the SAP RFC libraries and include files. These should come standard with an SAP system. (If you don't have an SAP system, or have to ask what SAP is, then this extension is of no use to you.) This extension creates a statically linked perl executable called rfcperl. The SAP libraries require that the resulting rfcperl

SAP_RFC was developed for two primary reasons. First was an effort to not require the use of C for all interface activities, and second was a desire to be able to quickly make changes to interfaces and immediately see the result. A result is the capability to, outside of SAP, quickly put together interface programs for a variety of short term needs.

A secondary purpose in the development of SAP_RFC was to have a mechanism by which CGI-BIN access to SAP systems could be provided, without being required to use C.

Principal Functions

The functions for SAP_RFC are open, close, ping, callrx1, callrx2, callrxx, help, sysinfo and version.

* open

Open a connection to an SAP Instance

$ret = &SAP_RFC::open($system, $client, $user, $passwd, $trace)

The open function tries to make a connection and then login into a SAP System. This requires that a SAP sideinfo file be present (in the local directory or in other normal SAP places).

Please note that ALL SAP userids and password MUST be in upper case.

Open returns a value of zero (0) if a successful connection was made. This does not guarantee that the login was also successful. To verify that the login to SAP was successful, it is strongly recommended that a SAP_RFC::ping command be executed after successful completion of the SAP_RFC::open command.

* close

Close an SAP session

$ret = &SAP_RFC::close()

Close the connection to an SAP Instance. This closes the single open connection from SAP_RFC to an SAP instance. If close was successful, then close returns a value of zero (0);

* ping

Test a connection with an SAP System.

($ret, $string) = &SAP_RFC::ping()

This command attempts to execute the SAP RFCPING RFC on the SAP system to which we are connected by the SAP_RFC::open command. The primary reason for the inclusion of this command is to provide a relatively easy way to verify that an attempted was successful. A positive return from SAP_RFC::open does not make any promises about successful logins.

If the SAP_RFC::ping operation was successful, a value of zero (0) will be returned as the return value. The return string may contain an error message, for some failure categories. Do not count on finding an absolute cause for the failure in the returned string.

* callrx1

Send and receive a message string to an SAP RFC

($ret, $string) = &SAP_RFC::callrx1($abap, $message_string1)

SAP_RFC::callrx1 executes a SAP RFC RfcCallReceive() library command with the target SAP ABAP/4 program passed in the variable $abap. The message to be given to the ABAP/4 program is contained in the character string $message_string. The message_string MUST be less than 248 characters in length.

The 248 character limit is due to a 249 character limit within the ABAP development environment when the debugger is in use.

The SAP RFC library RfcCallReceive() function sends an RFC Call to the SAP system and waits to receive the transaction results from the SAP system. Only the single result string may be returned from an SAP ABAP/4 transaction.

This function assumes that the name of the record being passed to the SAP RFC ABAP/4 program is IMPORT_STRING and the record received from the SAP RFC ABAP/4 program is named EXPORT_STRING receiving end, no data will be passed.

The tables functionality of the SAP library is not implemented.

SAP_RFC::callrx1 expects the return of one string from the SAP system

If the SAP RFC library function RfcCallReceive() completed successfully, that is is the return was RFC_OK, then the return value from SAP_RFC::callrx1 is a value of zero (0).

Note: SAP seems to have a tendancy to occasionally "forget" that a connection has been made. To prevent this from "hanging" a program, a TIME_OUT response (return value of 9) will be returned after a 180 second delay. If this return value is received, retry the SAP_RFC::callrx1 transaction that timed out.

* callrx2

Send one and receive two messages to an SAP RFC

($ret, $string1, $string2) = &SAP_RFC::callrx2($abap, $string3)

SAP_RFC::callrx2 is the same as SAP_RFC::callrx1 EXCEPT that SAP_RFC::callrx2 expects the return of two (2) strings from the SAP system. This functionality is included because it is a local feature at the author's site where the second string is used for error management.

There may be situations where a return string of greater than 248 characters are required, in this case the ABAP/4 program could use both of the returned strings to pass data.

SAP_RFC::callrx2 executes a SAP RFC library RfcCallReceive() command with the ABAP passed as the target program and the message_string as the contents to be passed. The message_string MUST be less than 248 characters in length.

The 248 character limit is due to a 249 character limit within the ABAP development environment when the debugger is in use.

The SAP RFC library RfcCallReceive() function sends a call to the SAP system and waits to receive transaction results from the SAP system. Only the two result strings may be returned from a transaction.

This function assumes that the name of the record being passed to the SAP RFC ABAP/4 program is IMPORT_STRING and the first record received from the SAP RFC ABAP/4 program is named EXPORT_STRING and the second record is named EXPORT_STRING2. If the record names do not match in the ABAP/4 program and on the RFC receiving end, no data will be passed.

The tables functionality of the SAP library is not implemented.

Callrx2 expects the return of two strings from the SAP system, the contents of both strings are returned by callrx2.

If the RfcCallReceive() completed successfully, that is is the return was RFC_OK, then callrx2 returns a value of zero (0).

Note: SAP seems to have a tendancy to occasionally "forget" that a connection has been made. To prevent this from "hanging" a program, a TIME_OUT response (return value of 9) will be returned after a 180 second delay. If this return value is received, retry the SAP_RFC::callrx2 transaction that timed out.

* callrxx

Send and receive a message string to an SAP RFC

($ret, $string) = &SAP_RFC::callrxx($abap, $message_string1,

$from_record_name, $to_record_name)

SAP_RFC::callrxx executes a SAP RFC RfcCallReceive() library command with the target SAP ABAP/4 program passed in the variable $abap. The message to be given to the ABAP/4 program is contained in the character string $message_string. The name of the record to be received from the SAP APAB/4 RFC program is contained in the string $from_record_name and the name of the record to be sent to the SAP ABAP/4 RFC program is contained in the string $to_record_name names are to be less than 20 and greater than three (3) characters in length.

The 248 character limit is due to a 249 character limit within the ABAP development environment when the debugger is in use.

The SAP RFC library RfcCallReceive() function sends an RFC Call to the SAP system and waits to receive the transaction results from the SAP system. Only the single result string may be returned from an SAP ABAP/4 transaction.

This function makes no assumptions as to the names of the records passed between the SAP RFC ABAP/4 program and the SAP_RFC perl extension. This is because the record names are passed as part of the parameters of the function call. What ever the names may be, they must match or no data will be passed.

The tables functionality of the SAP library is not implemented.

SAP_RFC::callrxx expects the return of one string from the SAP system

If the SAP RFC library function RfcCallReceive() completed successfully, that is is the return was RFC_OK, then the return value from SAP_RFC::callrxx is a value of zero (0).

If the function does not like the record names passed to it, it will return a RFC_BAD_INPUT (11) value. This usually means no record names were passed to the function, or that the length of the names was too short (less than four(4) characters). The function only requires that a single name be passed, however, you may want to pass names for both send and receive in all cases.

Note: SAP seems to have a tendancy to occasionally "forget" that a connection has been made. To prevent this from "hanging" a program, a TIME_OUT response (return value of 9) will be returned after a 180 second delay. If this return value is received, retry the SAP_RFC::callrxx transaction that timed out.

* help

Brief message describing the SAP_RFC commands

$string = &SAP_RFC::help()

This returns a message containing a copyright notice and a list of the public functions in the

SAP_RFC extension.

This function is probably of minimal use.

* sysinfo

SAP_RFC::sysinfo calls the SAP ABAP/4 program RFC_SYSTEM_INFO and returns a string containing a record of system ``information'' about the connected SAP system.

($ret, $string) = &SAP_RFC::sysinfo()

This command is included mostly for demonstration purposes. This function is helpful as a means of building confidence that the interface really works.

The SAP ABAP/4 function RFC_SYSTEM_INFO should be contained on all shipped SAP systems.

The layout of the internal SAP record returned by RFC_SYSTEM_INFO is as follows:

Data Description Length Position

---------------------- ------ --------

RFC Log Version 3 0 - 2

Character Set (SAP) 4 3 - 6

Integer Format 3 7 - 9

Floating Point Format 3 10 - 12

Logical SAP Destination 32 13 - 44

Contacted Host 8 45 - 52

SAP System ID (RFC) 8 53 - 60

SAP System ID (DB) 8 61 - 68

Database Host Name 32 69 - 100

Database System Type 10 101 - 110

SAP Basis Release 4 111 - 114

SAP Machine ID 5 115 - 119

Operating System 10 120 - 129

Time Zone Offset (secs) 6 130 - 135

DST Flag 1 136 - 136

SAP_RFC::sysinfo returns the data from the SAP record as a string of of value-name pairs. Each of the value-name pairs are separated by colons (':'). The value and the name in the value-name pairs are separated by semicolons (';'). See the sample program for specifics.

As a footnote, the name of the record that the SAP ABAP program RFC_SYSTEM_INFO

returns is RFCSI_EXPORT.

* version

$ver = &SAP_RFC::version()

This is an internal function that returns an internal version number. There is no guarantee that this number will be current at any time.

This function is probably useless.

Example

The following example assumes that the SAP RFC services are established and tested. This is not the place to discuss how to establish SAP RFC services. (See the WWW site in Error! Bookmark not defined..)

There are two requirements for an rfcperl program. These are: an SAP sideinfo file and a program script. There is a sample program file included with the distribution, called sample.pl, which is similar to the program contained in this document.

The sample program has six major activities using the SAP_RFC module. These are:

Open a connection to an SAP system

Verify that there is a login on the SAP system

Get the system information from the SAP system

Execute an ABAP/4 program returning one string

Execute an ABAP/4 program returning one string

Disconnect from the SAP system

These show use of the major SAP_RFC module functions

Note: The sample program executes ABAP/4 programs local to the author's SAP system.

* sideinfo file

The sideinfo file is required by the underlying SAP libraries to provide connection path information.

This file is either in the local directory or in one of the ``standard'' SAP locations.

The following is a sample sideinfo file with a logical destination of ``C01'', the SAP host in

``c01name'' and the SAP gateway host is ``gwhost''. The services are ``00'' in both cases. The ``I''

protocol is for Unix to SAP over TCP/IP.

DEST=C01

LU=c01name

TP=sapdp00

GWHOST=gwhost

GWSERV=sapgw00

PROTOCOL=I

You may have multiple destinations in a single sideinfo file, separated by blank lines.

* Sample Program

#!./rfcperl -w

# test the SAP_RFC Module

# This is from the development library, so beware !

use lib './blib'; # For development

use SAP_RFC;

# connect to System C01, on client 888 as USER with a

# password of PASS with no trace flag.

$open_ret = &SAP_RFC::open ('C01', '888', 'USER', 'PASS', 'NO');

if ($open_ret != 0) # Return was non-zero

{

print "SAP::open() - FAILED !! \n";

print "did you specify a valid destination ? \n";

print "Bye Bye \n";

exit;

}

# Check the connection to make sure we are really logged in

($ping_ret, $ping_str) = &SAP_RFC::ping();

if ($ping_ret != 0)

{

print "SAP::ping() - FAILED !! \n";

print "Ping Error String - $ping_str \n";

print "Did you use the right client, user and password ?\n";

print "Bye Bye \n";

exit;

}

# This should work on all systems

# Go get the system info from the SAP system

print "\nSAP System Information \n";

($ret_val, $str) = &SAP_RFC::sysinfo();

if ($ret_val != 0)

{

print "SAP_RFC::sysinfo() - FAILED !! \n";

}

else

{

#split the name-value pairs

@pairs = split(/:/,$str);

foreach $pair (@pairs)

{

($name, $value) = split(/;/, $pair);

$SYS_INFO{$name} = $value;

}

# Display the pairs sorted by the key name

foreach $key (sort(keys %SYS_INFO))

{

if ($key eq 'DST_Flag')

{

if ($SYS_INFO{$key} eq 'X')

{

print "This System uses Daylight Savings Time \n";

}

else

{

print "This System DOES NOT use Daylight Savings Time \n";

}

}

else

{

print "$key = $SYS_INFO{$key} \n";

}

}

}

print "\n";

# This is a local function

# Now lets try to use callrx1.

# Z_WWW_INV1 returns the quantities of the passed item number

# in each of the inventory storage locations

$item = '0611077A70';

print "\nSAP Inventory Info for Item $item\n";

$i = 0;

$nstr = '';

$abap = 'Z_WWW_INV1'; # call this ABAP/4 program

$str = "xx$item"; # pass it this string

($inv_ret, $nstr) = &SAP_RFC::callrx1($abap, $str);

if ($inv_ret != 0) #call failed

{

print "SAP::callrx1($abap, $str) - FAILED !! \n";

}

else # call appears to be good

{

$inv_tot = 0;

$inv_nam[0] = substr($nstr,0,4);

$inv_dat[0] = substr($nstr,5, 15);

for ($i = 0; $i < 11; $i++)

{

$inv_dat[$i] = 0;

$inv_nam[$i] = ' ';

$na = ' ';

# the numbers are in the form of xx,xxx.xxx

# for a US SAP Setup

$inv_nam[$i] = substr($nstr, (22 * $i), 4);

$nb = substr($nstr, (22 * $i) + 5, 15);

$na = substr($nb, 0, 11); # strip off decimal and fractional

part

$na =~ y/0-9//cd; # get rid of non-numerics eg comma

$inv_dat[$i] = $na;

if (substr($inv_nam[$i], 0, 1) ne ' ')

{

$inv_tot += $inv_dat[$i];

}

}

for ($i = 0; $i < 11; $i++)

{

if (substr($inv_nam[$i], 0, 1) ne ' ')

{

print "$i - $inv_nam[$i] = $inv_dat[$i] \n";

}

}

print "Total Inventory = $inv_tot \n\n";

}

print "\n";

# another local function to show callrx2

# Do a call to the SAP ABAP/4 (Z_FIAM_ITEM) which returns

# -- in the first string -- (the second is for errors)

# a pair of of system parameters for the item number given to

# the ABAP/4 program.

print "\nCheck Item Number length Setting \n";

$inf_ret = 0;

$tstr = '';

$nstr = '';

$xnum = 0;

$xxnum = 0;

$xflg = '';

$abap = 'Z_FIAM_ITEM'; # call this ABAP/4

$str = '360611077A70'; # pass this string

($inf_ret, $tstr, $nstr) = &SAP_RFC::callrx2($abap, $str);

if ($inf_ret != 0) #call failed

{

print "SAP::callrx2($abap, $str) - FAILED !! \n";

}

else # call appears to be good

{

$xnum = substr($tstr, 0,4);

$xflg = substr($tstr, 5,1);

$xxnum = 0 + $xnum;

if ($xflg eq 'X')

{

print "The X flag is set and the item number length is $xxnum

\n";

}

else

{

print "The X flag is NOT set and the item number length is $xxnum

\n";

}

}

# OK we are done, close up shop

# Close the SAP connection and exit the program

$close_ret = &SAP_RFC::close();

if ($close_ret != 0) #call failed

{

print "SAP::close() - FAILED !! \n";

}

exit;

* Output from Sample program

The following is a slightly edited, to protect the innocent, output from an execution of the above script

SAP System Information

Character Set (SAP) = 1100

Contacted Host = c01name1

This System uses Daylight Savings Time

Database Host Name = c01db

Database System Type = ORACLE

Floating Point Format = IE3

Integer Format = BIG

Logical SAP Destination = c01name1_c01_00

Operating System = HP-UX

RFC Log Version = 011

SAP Basis Release = 21I

SAP Machine ID = 272

SAP System ID (DB) = c01

SAP System ID (RFC) = c01

TZ_Offset (secs) = -21600

SAP Inventory Info for Item 0611077A70

0 - 0069 = 0

1 - 0099 = 0

2 - 7600 = 0

3 - 7700 = 0

4 - MQE = 0

5 - WAHO = 5430

Total Inventory = 5430

Check Item Number length Setting

The X flag is set and the item number length is 10

NOTES

· The Author has a very limited hardware and operating system in which to test this extension. It has been built and tested with HPUX 10.20 and HPUX 9.04. Perl 5.004 is the only version of perl utilized.

· SAP R/3 version 2.2C is the only target SAP system this library has been tested against. (However, there is no reason to believe that other versions will perform differently.)

· The executable name was purposely made different than perl so that there would not be a confusion issue. Besides you may not always want to have the large executable floating around.

· The SAP libraries area apparently compiled with debugging flags set. You may wish to run strip before installing the executables.

· The SAP libraries are static only.

· RFC_SAP requires a sideinfo file in the ``local'' directory.

· Before you attempt to use the interface, make sure that the /etc/services file has the appropriate SAP specific entries.

· The tables functions of the SAP RFC libraries is not implemented.

· There is a 180 second "alarm clock" installed in case of SAP timeouts. The SAP_RFC::callrx1 (or 2 or x) function will return "TIME_OUT" (9) if the SAP system, after connection, does not respond in the 180 second window.

AUTHOR

Garth Kennedy

COPYRIGHT NOTICE

SAP_RFC Module Version 0.19 - 1 Aug 1997

Copyright 1996, 1997 Motorola Inc.;

Permission to use, copy modify and distribute without charge this software, documentation etc. is granted, provided that this comment and the author's name is retained. Neither the author or Motorola assume any responsibility for problems resulting from the use of this software. SAP and ABAP/4 are copyrights of SAP AG.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Russian ABAP Developer's Club Forum Index -> Connect to External system, Unix и Perl All times are GMT + 4 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


All product names are trademarks of their respective companies. SAPNET.RU websites are in no way affiliated with SAP AG.
SAP, SAP R/3, R/3 software, mySAP, ABAP, BAPI, xApps, SAP NetWeaver and any other are registered trademarks of SAP AG.
Every effort is made to ensure content integrity. Use information on this site at your own risk.