This post deals with using PHP to communicate with SAP, so you’ll need to have acces to a SAP system if you want to reproduce this.
All-right, some time ago I wrote a post in which I said that I wanted to use a BAPI (or other) to retrieve a list of material numbers from a SAP client and build a select list as opposed to hard coding it in my PHP script. At the time my other options might have been to upload a list of material numbers into an SQL database or hard code a list of materials numbers in my script : The downside here being that you could hardly call this dynamic. Anyway, thanks to Jako (Jopac) and his valuable contributions , success is the name of the game – nothing is going to stop me now! I’m kinda eager to post this , so you’ll have to wait a bit longer for the complete code where I:

1 – Make an RFC call to retrieve a list of materials,
2 – Retrieve the stock requirements list of the selected material,
3 – The cherry on the cake…more on that in another post.

Before I get going I have to give credit to the following people (unfortunately I cannot code to save my life – I just copy what others do, break it apart, put it together again and then pray that it works!)
– Jako (Jopac) for all his comments and for trying to paste the code in my “blob”. At my end the code came out all messed up, but the general feel of it came through
– Flavio Ciotola. He wrote an interesting post on the SAP SDN Network and it helped me to figure out the missing link in some of the code sent by Jako.

I should maybe also mention that these tests have been made using an HP (Compaq nc6220) laptop running on MS Windows XP Pro, the Apache server version 1.3.33 and the PHP scripting engine in version 4.3.10 and of course the SAPRfc library (the Apache/PHP install was that installed via the EasyPhp package). SAP side I was using SAP ERP ECC (ERP Central Component) 5.0 (which by the way has some very interesting new functionalities for logistics consultants).

So, let’s get cracking.
If we explore it via transaction SE37, some of the information that is interesting is found in the “Tables” tab.
We will use that in our data selection process
To get an idea of I am expecting to see later on in the browser, I’ll test the BAPI in SAP first.
I’m going to click on the Icon icon next to MATNRSELECTION and type the following:
In other words I want to see all materials whose numbers is included between (BT) P-900 and P-912.
If I execute the BAPI, this is what I get.
And what do you know…I get the same thing in my browser.
Ok, that wasn’t too bad. Let’s do a variation on it and give you a tip along the way.
I tried to use the script to retrieve material numbers that were included between “10” and “1000”. I got some very strange results out of it. For example I got a part number “100-100” that came out (huhh??).
That’s when I thought of coding the part number on 18 characters (like MARA-MATNR is). That came out much better –> So remember that you may need to add some leading zeroes (0) for numerical values.
So I tried to get all part numbers that were between “1” and “90” (not forgetting to add the leading zeroes).
Here is the bit of code that used to get that:

saprfc_table_append ($fce,"MATNRSELECTION", array (

As before it worked OK. But i thought that it would be more interesting if I could filter that list based on a plant code.
As earlier on, I used the PLANTSELECTION parameter to restrict the selection by the plant code.
Here is the bit of code that I added to my script:

saprfc_table_append ($fce,"PLANTSELECTION", array (

If I execute the BAPI again I get a filtered list of 7 materials that only exist in plant 1000 in both SAP and in my browser.
The result I see is contained in table MATNRLIST.
To output the result I use this code.

foreach ($MATNRLIST as $msg) {
$matname = $msg["MATERIAL"];
$matdescription = $msg["MATL_DESC"];
echo "Material". $matname ."
| Material Description" . $matdescription . "

I f you would like to have the complete code, then please ask for it via the comments. I’d also really like your thoughts and contributions, so don’t be afraid to comment!

Code is released under Creative Commons License:

This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 License.

Privacy Preference Center