Application Development Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
elalan
Participant
8,267

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

http://scn.sap.com/community/abap/blog/2013/10/01/first-conection-with-sap-and-php

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/rpcl.inc.php"); 
  3. require("rpcl/nusoap/nusoap.php"); 
  4. //Includes 
  5. use_unit("forms.inc.php"); 
  6. use_unit("extctrls.inc.php"); 
  7. use_unit("stdctrls.inc.php"); 
  8. use_unit("platinumgrid/jtplatinumgridlite.inc.php"); 
  9. use_unit("jqgrid/jqgrid.inc.php"); 
  10. use_unit("comctrls.inc.php"); 
  11. use_unit("styles.inc.php"); 

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.     { 
  3.  
  4.  
  5.   // mi codigo THOR 15/05/2013 
  6.  
  7.  
  8.     // crea el cliente 
  9.  
  10.  
  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->
        children()->ZP1_TABLA->item;
$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 VB.net 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
        $cont++;
$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
        else
{
         $this->Label1->Caption = "Error: ".$cliente->getError();
      }
// FIN DE MI CODIGO
    }
// 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

2 Comments
Labels in this area