PHP & SAP : Experimenting with SAPRfc
Jul
30
2005

A couple of years ago I started experimenting with the SAPRfc library but a busy schedule prevented me from doing anything more then execute the demo script.
So, a couple of years down the road, I last week again installed the SAPRfc library on my new work PC (running XP Pro). Although I have been pretty busy this week, I took the time to try out the demo script (reading the SAP username list within a SAP client) and I also tried to adapt that script to see if I could get it to do something else.
As I am currently doing a bit of MM (Materials Management) work I thought that it would be appropriate to do something around that module…nothing extravagant tough. I set my sights on the BAPI BAPI_MATERIALGROUP_GET_LIST (which is very simple - it reads the material groups within a SAP client).

I then proceeded to use transaction SE37 to read the import parameters and tables used by this BAPI.

Import parameters:
import parameters

Tables:
Tables

I also tested the BAPI (within SAP) to see what table was being filled with the information I wanted.

Still in SE37, test the module with the following parameters:

execution parameters

The data returned is in the tables as shown below:

Short descriptions

I found that the data I wanted to extract was in table SHORTDESCRIPTIONS.

Material groups

All I had to do now was to modify the code (the code below is not the complete code).

Call the BAPI and specify the parameters:
$result=$sap->callFunction("BAPI_MATERIALGROUP_GET_LIST", array( array("IMPORT","SHORTDESCRIPTIONFLAG","X"), array("IMPORT","LANGUAGE","E"), array("TABLE","SHORTDESCRIPTIONS",array())));

And output the data by using the SAP field name:
foreach ($result["SHORTDESCRIPTIONS"] as $matgroup) { echo "<_t_r><_t_d>" , $matgroup["MATL_GROUP"] ,"<_/t_d><_t_d>" , $matgroup["MG_DESCR_SHORT"] , "<_/t_d><_/t_r>"; }
NB: get rid of the (_) underscore in the tr and td tags.

Here is a screen dump of the result:
result

This was done using a SAP client in version 4.6c on a notebook running XP Pro with Apache 1.3.33 and PHP 4.3.10 as provided by the EasyPHP version 1.8 installer.

Tags technorati :  

8 Responses to “PHP & SAP : Experimenting with SAPRfc”

  1. Gravatar of Re-writing SAP with PHP (part 1) at renet@web Re-writing SAP with PHP (part 1) at renet@web

    [...] List material groups (You can find more information in this post) [...]

  2. Gravatar of Fabian Fabian

    Have you tried something with sales order module ?
    Do you have some full example of your tries to download ?
    Thanks

  3. Gravatar of Claudiomiro Claudiomiro

    Hi,

    I would like to receive more informations about how to connect to SAP from PHP, it’s possible?

    Regards,

    Claudiomiro Silva

  4. Gravatar of Pascal Pascal

    Hello Claudiomiro,
    If you want more information on SAP and php, I recommend you check out the following sites.
    SAPRFC Homepage and the SAP and PHP forum on the SAP Developper Network.
    They will have all the information you need to get started.

  5. Gravatar of abdou abdou

    we are starting with SAP, at a big industry company, and i want informations and exemples if possible of installing and using SAPRFC

    Best regards

  6. Gravatar of Pascal Pascal

    Hello Abdou,
    I recommend you follow the links I have indicated in my previous answer. These two sites offer plenty of information to get you started.
    All the best.

  7. Gravatar of Jairam Jairam

    Hi Pascal,
    I read ur post about using a BAPI in MM to get the data in PHP where u hv used the BAPI as it has been defined by SAP…What if I need to build a logic based on the data returned by various BAPIs in a program flow, and store it in a table to be retrieved later by using saprfc…Can we write a customized function like a BAPI and extract the data out in PHP…Ur help is highly appreciated…Thanks…

    Regds,
    Jairam

  8. Gravatar of Pascal Pascal

    Hey Jairam,
    Yes you can write your own functions and then use them using SAPrfc. Remember that BAPI’s are just Function Modules. So you can write your own functions and then use SAPrfc to extract the data.
    Good luck.

Leave a Reply