Skip to Content
Author's profile photo Alan Indomenico

Comsume a SAP Web Service from PHP

Hi , this paper i will show you how to conect a SAP WS , with PHP , i’m going to use part of my blog to get faster to the point , this is the link

what we are going to do is a simple conection to a WS and then show data in a grid , this is the UI for the page


As you can see is a button , a label and a grid just to show some data

Now to see the code

The declaration

  1. <?php 
  2. require_once(“rpcl/”); 
  3. require(“rpcl/nusoap/nusoap.php”); 
  4. //Includes 
  5. use_unit(“”); 
  6. use_unit(“”); 
  7. use_unit(“”); 
  8. use_unit(“platinumgrid/”); 
  9. use_unit(“jqgrid/”); 
  10. use_unit(“”); 
  11. use_unit(“”); 

This is how i star the code , but to be honest , it’s auto generated by my php suite ,jejeje , the only thing that i write in this part was line number 3 , (trust me we are going to need it) Now i’ll show you the real code , when i click in the  button is gonna call the WS to sap and ask me about my credentials

  1. function Button1Click($sender, $params
  2.     { 
  5.   // mi codigo THOR 15/05/2013 
  8.     // crea el cliente 
  11.     $wsdl = true; 
  12.     $cliente = new nusoap_client(‘wsdltabla.wsdl’, $wsdl ); 
  13.     $cliente->soap_defencoding = ‘UTF-8’
  14.     $cliente->setCredentials(‘adm1’,‘Mjo2’,‘basic’)  ; 

First we have to start a cliente , this is what is going to connect to sap , line 11 are declaration where we say to the WS that we are using wsdl file or direccion , in this case a file , becase by a url didn’t work

line 12  the decoding uds for this , more declaration for the client

line 13 is where we put the credentials to be sent to sap , in this order is user , password , and mode of autentication

Now to finish the real part

// Check for an error
$err = $cliente->getError();
if ($err) {
    // Display the error
     $this->Label1->Caption = $err ;
    // At this point, you know the call that follows will fail

This lines just tells me if we have any error when we are trying to conect to the web service  or the credentials where not autenticated

Now the fun part

 // llamada al metodo
 $cliente->call('ZP1_MF_WS',array('ZP1_TABLA' => array()) ,'http://your server:8000/sap/bc/srt/rfc/sap/zp1_ws/200/zp1_ws/zp1_bd_ws?sap-client=200') ;
$resultado = $cliente->responseData;
$XmlArray   = new SimpleXMLElement($resultado);
$t = $XmlArray->children("soap-env", true)->Body->
        children("n0", true)->ZP1_MF_WSResponse->
$cont = 0;

in line 6 is the part where we call the ws from php , $cliente calls my fuction y the ws (ZP1_MF_WS) and transport an array (everything in this kind of programing is like an array) ,ZP1_TABLA is the table that i send empty and get back with data, and the last one is the direccion of the WS working , with then field MANDT , in case you have serveral mandts just put a listbox with all the mandts you have , but first work it like a string (just change where i have a 200 for the value you need)

Line 11 $resultado , is here where i got my data , but it would be to easy if it when plain text to read , nooooo , it wouldn’t be that “fun”,  i spend all day thinking why it didn’t work out if everything was OK , this is the answer , when it get back with data , it gets in XML, and when you are trying to read it you just read the tags and no the data, (in is much more easy, if you want just tell me and will make another like this in VB) , to convert the data we have to use line 14 , to translate from xml to an array

the last line of this part 19 , this is where we take the hearder to know what are my columns in my table

and the last part of the code is this

foreach ($t as $item) {
// Objeto ----> Array
        $a = $this->o2a($item);
// llenar el  grid bidimensional
        $grid[$cont] = $a;
// contado de posiciones
$this->ProgressBar1->Position = $cont * 25;
                     }     // fin de ciclo
    // Revisa errores
        if (!$cliente->getError()) {
// muestra resultados
      $this->Label1->Caption = "Ver tabla para los resultados";
// posteo los datos en el grid
          $this->JTPlatinumGrid1->CellData = $grid;
// Error
         $this->Label1->Caption = "Error: ".$cliente->getError();
// Object to Array
  function o2a($object) {
    $array = array();
    if (is_object($object)) {
        $array = get_object_vars($object);
    return $array;

in this we run across the array in every element of the hearders , basicly we are moving in the same row but moving in columns, the we pass it to the function o2a sad to get the actual value and put it into the cell of the grid.

So this is it , nos not that hard (now that i did it ,ejejje) but i hope it helps you to start working

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Andy Silvey
      Andy Silvey

      Hi Alan,

      thank you for the second part.

      In real life we wouldn't be able to put the User Credentials in the code, does PHP already have libraries for Single Sign On to SAP, for taking logged on User credentials and using those to logon to SAP as a trusted system ?

      What about Pooled connections, does PHP have the possibility to handle connection pooling ?

      What are the connection to SAP possibilities from PHP ?  Is there a list with the different possibilities to connect from a PHP web application to SAP  and best practices ?

      Best regards,


      Author's profile photo alan indomenico
      alan indomenico
      Blog Post Author

      In this case i use my credentials just to conect faste , but you are rigth , what i do in this cases , y crear anther web service conection to autenticate the user , and make my own object like a login in VB, in the conection pool i'm not sure but in sap you just send the request and the job gets done in the SAP syde , from our client side is just to show that request , i don't see pooling working in this matter , maybe web dynpros is more likely .

      The possibillities i think the same that any normal web page or web services consumer has but i'm not sure what you are asking me here .

      Best practices and so, is just litle because isn't that popular , just a few work like this because there are another languajes not that strong but much easier to work (i try VB and i must confese is a lot easier)

      I hope this anwser you some of the issues but let me know and i search a litler to help