Hello community,

PHP is a server side scripting language, you can find more information here. PHP offers access to COM objects on Windows platforms, all you have to do is to add the line extension=php_com_dotnet.dll in the php.ini file. In this case you have the possibility to use COM Connector (CCo), which I presented here. So you can use NetWeaver RFC Library inside PHP.


Ping
Here a small example, to ping an SAP system.
At first the HTML file:

<!doctype html>

<html>
  <head>
    <title>
      Ping to an SAP system
    </title>
  </head>

  <body>
    <h1>
      Ping to an SAP system
    </h1>

    <form method="POST" action="0103_Ping.php">
      <table border="0">
        <tr>
          <td>Host:</td>
          <td><input type="text" name="ashost" size="20" value="ABAP"></td>
        </tr>
        <tr>
          <td>Systemnumber:</td>
          <td><input type="text" name="sysnr" size="20" value="00"></td>
        </tr>
        <tr>
          <td>Client:</td>
          <td><input type="text" name="client" size="20" value="001"></td>
        </tr>
        <tr>
          <td>Username:</td>
          <td><input type="text" name="user" size="20" value="bcuser"></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input type="password" name="password" size="20" value="minisap"></td>
        </tr>
      </table>
      <input type="submit" name="submit" value="Submit">
    </form>

  </body>    
</html>

/wp-content/uploads/2016/01/001_875977.jpg
And now the PHP file:

<!doctype html>

<html>
  <head>
    <title>
      Ping to an SAP system
    </title>
  </head>

  <body>
    <h1>
      Ping to an SAP system
    </h1>

<?php

  const RFC_OK = 0;

  $conn = "ASHOST=" . $_POST["ashost"] . ", ";
  $conn = $conn . "SYSNR=" . $_POST["sysnr"] . ", ";
  $conn = $conn . "CLIENT=". $_POST["client"] . ", ";
  $conn = $conn . "USER=" . $_POST["user"] . ", ";
  $conn = $conn . "PASSWD=" . $_POST["password"];

  $SAP = new COM("COMNWRFC") or die ("Can't create object");

  $SAP->ErrorMsgTarget = 1; //Necessary, in a normal case 2
  $SAP->DebugOutput = 1;

  $hRFC = $SAP->RfcOpenConnection($conn);
  if ($hRFC <> 0) {

    $hFuncDesc = $SAP->RfcGetFunctionDesc($hRFC, "RFC_PING");
    if ($hFuncDesc <> 0) {
      $hFunc = $SAP->RfcCreateFunction($hFuncDesc);
      if ($hFunc <> 0) {
        $rc = $SAP->RfcInvoke($hRFC, $hFunc);
        if ($rc == RFC_OK) {
          echo "Ping successful";
        }
        else {
          echo "Ping not successful";
        }
      $rc = $SAP->RfcDestroyFunction($hFunc);
      }
    }

    $rc = $SAP->RfcCloseConnection($hRFC);

  }
  $SAP = null;

?>

  </body>
</html>

/wp-content/uploads/2016/01/002_876032.jpg


System Info
Here now another example to get system information via RFC-enabled function module RFC_SYSTEM_INFO.

<!doctype html>

<html>

  <head>

    <title>
      System Information
    </title>

  </head>

  <body>

    <h1>
      System Information
    </h1>

    <form method="POST" action="0104_SysInfo.php">
      <table border="0">
        <tr>
          <td>Host:</td>
          <td><input type="text" name="ashost" size="20" value="ABAP"></td>
        </tr>
        <tr>
          <td>Systemnumber:</td>
          <td><input type="text" name="sysnr" size="20" value="00"></td>
        </tr>
        <tr>
          <td>Client:</td>
          <td><input type="text" name="client" size="20" value="001"></td>
        </tr>
        <tr>
          <td>Username:</td>
          <td><input type="text" name="user" size="20" value="bcuser"></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input type="password" name="password" size="20" value="minisap"></td>
        </tr>
      </table>
      <input type="submit" name="submit" value="Submit">
    </form>

  </body>	

</html>

And now the PHP file:

<!doctype html>

<html>
  <head>
    <title>
      System Information
    </title>
  </head>

  <body>
    <h1>
      System Information
    </h1>

<?php

  const RFC_OK = 0;

  $NumByRef = 0;
  $StrByRef = "";

  $conn = "ASHOST=" . $_POST["ashost"] . ", ";
  $conn = $conn . "SYSNR=" . $_POST["sysnr"] . ", ";
  $conn = $conn . "CLIENT=". $_POST["client"] . ", ";
  $conn = $conn . "USER=" . $_POST["user"] . ", ";
  $conn = $conn . "PASSWD=" . $_POST["password"];

  $SAP = new COM("COMNWRFC") or die ("Can't create object");

  $SAP->ErrorMsgTarget = 1; //Necessary, in a normal case 2

  $hRFC = $SAP->RfcOpenConnection($conn);
  if ($hRFC == 0) {
    echo "RfcOpenConnection not successful";
    $SAP = null;
    exit(1);
  }

  $hFuncDesc = $SAP->RfcGetFunctionDesc($hRFC, "RFC_SYSTEM_INFO");
  if ($hFuncDesc == 0) {
    echo "RfcGetFunctionDesc not successful";
    $rc = $SAP->RfcCloseConnection($hRFC);
    $SAP = null;
    exit(1);
  }

  $hFunc = $SAP->RfcCreateFunction($hFuncDesc);
  if ($hFunc == 0) {
    echo "RfcCreateFunction not successful";
    $rc = $SAP->RfcCloseConnection($hRFC);
    $SAP = null;
    exit(1);
  }

  $rc = $SAP->RfcInvoke($hRFC, $hFunc);
  if ($rc == RFC_OK) {
    $rc = $SAP->RfcGetStructure($hFunc, "RFCSI_EXPORT", $NumByRef);
    if ($rc == RFC_OK) {
      $hStruct = $SAP->lngByRef();
      $rc = $SAP->RfcGetChars($hStruct, "RFCHOST", $StrByRef, 8);
      $charBuffer = $SAP->strByRef();
      echo "Host: " . $charBuffer . "<br />";
      $rc = $SAP->RfcGetChars($hStruct, "RFCSYSID", $StrByRef, 8);
      $charBuffer = $SAP->strByRef();
      echo "SysID: " . $charBuffer . "<br />";
      $rc = $SAP->RfcGetChars($hStruct, "RFCDBHOST", $StrByRef, 32);
      $charBuffer = $SAP->strByRef();
      echo "DBHost: " . $charBuffer . "<br />";
      $rc = $SAP->RfcGetChars($hStruct, "RFCDBSYS", $StrByRef, 10);
      $charBuffer = $SAP->strByRef();
      echo "DBSys: " . $charBuffer . "<br />";
    }
  }

  $rc = $SAP->RfcDestroyFunction($hFunc);
  $rc = $SAP->RfcCloseConnection($hRFC);
  $SAP = null;

?>

  </body>
</html>

/wp-content/uploads/2016/01/003_876033.jpg


Read Table
Here now another example to read any table via RFC-enabled function module RFC_READ_TABLE. The only restriction is that the line of a table must be shorter than 512 characters.

<!doctype html>

<html>

  <head>

    <title>
      Read Table
    </title>

  </head>

  <body>

    <h1>
      Read Table
    </h1>

    <form method="POST" action="0105_ReadTable.php">
      <table border="0">
        <tr>
          <td>Host:</td>
          <td><input type="text" name="ashost" size="20" value="ABAP"></td>
        </tr>
        <tr>
          <td>Systemnumber:</td>
          <td><input type="text" name="sysnr" size="20" value="00"></td>
        </tr>
        <tr>
          <td>Client:</td>
          <td><input type="text" name="client" size="20" value="001"></td>
        </tr>
        <tr>
          <td>Username:</td>
          <td><input type="text" name="user" size="20" value="bcuser"></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input type="password" name="password" size="20" value="minisap"></td>
        </tr>
        <tr>
          <td>Tablename:</td>
          <td><input type="text" name="tablename" size="16" value="USR01"></td>
        </tr>
      </table>
      <input type="submit" name="submit" value="Submit">
    </form>

  </body>	

</html>

And now the PHP file:

<!doctype html>

<html>

  <head>

    <title>
      Read Table
    </title>

  </head>

  <body>

    <h1>
      Read Table
    </h1>

<?php

  const RFC_OK = 0;

  $NumByRef = 0;
  $StrByRef = "";

  $conn = "ASHOST=" . $_POST["ashost"] . ", ";
  $conn = $conn . "SYSNR=" . $_POST["sysnr"] . ", ";
  $conn = $conn . "CLIENT=". $_POST["client"] . ", ";
  $conn = $conn . "USER=" . $_POST["user"] . ", ";
  $conn = $conn . "PASSWD=" . $_POST["password"];

  $SAP = new COM("COMNWRFC") or die ("Can't create object");

  $SAP->ErrorMsgTarget = 1; //Necessary, in a normal case 2

  $hRFC = $SAP->RfcOpenConnection($conn);
  if ($hRFC == 0) {
    echo "RfcOpenConnection not successful";
    $SAP = null;
    exit(1);
  }

  $hFuncDesc = $SAP->RfcGetFunctionDesc($hRFC, "RFC_READ_TABLE");
  if ($hFuncDesc == 0) {
    echo "RfcGetFunctionDesc not successful";
    $rc = $SAP->RfcCloseConnection($hRFC);
    $SAP = null;
    exit(1);
  }

  $hFunc = $SAP->RfcCreateFunction($hFuncDesc);
  if ($hFunc == 0) {
    echo "RfcCreateFunction not successful";
    $rc = $SAP->RfcCloseConnection($hRFC);
    $SAP = null;
    exit(1);
  }

  $rc = $SAP->RfcSetChars($hFunc, "QUERY_TABLE", $_POST["tablename"]);
  $rc = $SAP->RfcSetChars($hFunc, "DELIMITER", "~");

  $rc = $SAP->RfcInvoke($hRFC, $hFunc);
  if ($rc == RFC_OK) {
    $rc = $SAP->RfcGetTable($hFunc, "DATA", $NumByRef);
    if ($rc == RFC_OK) {
      echo "<table border=\"1\">";
      $hTable = $SAP->lngByRef();
      $rc = $SAP->RfcGetRowCount($hTable, $NumByRef);
      $RowCount = $SAP->lngByRef();
      $rc = $SAP->RfcMoveToFirstRow($hTable);
      for ($i = 1; $i <= $RowCount; $i++){
        echo "<tr>";
        $hRow = $SAP->RfcGetCurrentRow($hTable);
        $rc = $SAP->RfcGetChars($hRow, "WA", $StrByRef, 512);
        $charBuffer = $SAP->strByRef();
        $fields = explode("~", $charBuffer);
        foreach ($fields as $field){
          echo "<td>". $field . "</td>";
        }
        if ($i < $RowCount) {
          $rc = $SAP->RfcMoveToNextRow($hTable);
        }
        echo "</tr>";
      }
      echo "</table>";
    }
  }

  $rc = $SAP->RfcDestroyFunction($hFunc);
  $rc = $SAP->RfcCloseConnection($hRFC);
  $SAP = null;

?>

  </body>

</html>


Code and Execute an ABAP Report
Here now another example to code an ABAP report inside a browser and to execute it via RFC-enabled function module RFC_ABAP_INSTALL_AND_RUN. The only restriction is that the length of a code line must be shorter or equal than 72 characters.

<!doctype html>

<html>

  <head>

    <title>
      ABAP Report
    </title>

  </head>

  <body>

    <h1>
      ABAP Report
    </h1>

    <form method="POST" action="0108_ABAPReport.php">
      <table border="0">
        <tr>
          <td>Host:</td>
          <td><input type="text" name="ashost" size="20" value="ABAP"></td>
        </tr>
        <tr>
          <td>Systemnumber:</td>
          <td><input type="text" name="sysnr" size="20" value="00"></td>
        </tr>
        <tr>
          <td>Client:</td>
          <td><input type="text" name="client" size="20" value="001"></td>
        </tr>
        <tr>
          <td>Username:</td>
          <td><input type="text" name="user" size="20" value="bcuser"></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input type="password" name="password" size="20" value="minisap"></td>
        </tr>
      </table>
      <br />
<textarea cols="73" rows="10" name="program">
Report zTest Line-Size 256.
Write: 'Hello World from'.
Write: sy-sysid.
</textarea>
      <br /><br />
      <input type="submit" name="submit" style="width:512px;" value="Submit">
    </form>

  </body>	

</html>

And now the PHP file:

<!doctype html>

<html>

  <head>

    <title>
      ABAP Report Result
    </title>

  </head>

  <body>

    <h1>
      ABAP Report Result
    </h1>

<?php

  //-Constants----------------------------------------------------------
    const RFC_OK = 0;

  //-Variables----------------------------------------------------------
    $NumByRef = 0;
    $StrByRef = "";

  //-Main---------------------------------------------------------------
    $conn = "ASHOST=" . $_POST["ashost"] . ", ";
    $conn = $conn . "SYSNR=" . $_POST["sysnr"] . ", ";
    $conn = $conn . "CLIENT=". $_POST["client"] . ", ";
    $conn = $conn . "USER=" . $_POST["user"] . ", ";
    $conn = $conn . "PASSWD=" . $_POST["password"];

    $SAP = new COM("COMNWRFC") or die ("Can't create object");

    $SAP->ErrorMsgTarget = 1; //In a normal case 2

    $hRFC = $SAP->RfcOpenConnection($conn);
    if ($hRFC == 0) {
      echo "RfcOpenConnection not successful";
      $SAP = null;
      exit(1);
    }

    $hFuncDesc = $SAP->RfcGetFunctionDesc($hRFC, 
      "RFC_ABAP_INSTALL_AND_RUN");
    if ($hFuncDesc == 0) {
      echo "RfcGetFunctionDesc not successful";
      $rc = $SAP->RfcCloseConnection($hRFC);
      $SAP = null;
      exit(1);
    }

    $hFunc = $SAP->RfcCreateFunction($hFuncDesc);
    if ($hFunc == 0) {
      echo "RfcCreateFunction not successful";
      $rc = $SAP->RfcCloseConnection($hRFC);
      $SAP = null;
      exit(1);
    }

    //-Writes the report into the PROGRAM table-------------------------
      $rc = $SAP->RfcGetTable($hFunc, "PROGRAM", $NumByRef);
      if ($rc == RFC_OK) {
        $hTable = $SAP->lngByRef();
        $program = explode("\r\n", $_POST["program"]);
        foreach ($program as $line) {
          $hRow = $SAP->RfcAppendNewRow($hTable);
          $rc = $SAP->RfcSetChars($hRow, "LINE", $line);
        }
      } else {
        echo "RfcGetTable not successful";
        $rc = $SAP->RfcDestroyFunction($hFunc);
        $rc = $SAP->RfcCloseConnection($hRFC);
        $SAP = null;
        exit(1);
      }

    $rc = $SAP->RfcInvoke($hRFC, $hFunc);
    if ($rc != RFC_OK) {
      echo "RfcInvoke not successful";
      $rc = $SAP->RfcDestroyFunction($hFunc);
      $rc = $SAP->RfcCloseConnection($hRFC);
      $SAP = null;
      exit(1);
    }

    //-Gets the result from the WRITES table----------------------------
      $rc = $SAP->RfcGetTable($hFunc, "WRITES", $NumByRef);
      if ($rc == RFC_OK) {
        $hTable = $SAP->lngByRef();
        $rc = $SAP->RfcGetRowCount($hTable, $NumByRef);
        $RowCount = $SAP->lngByRef();
        $rc = $SAP->RfcMoveToFirstRow($hTable);
        for ($i = 1; $i <= $RowCount; $i++){
          $hRow = $SAP->RfcGetCurrentRow($hTable);
          $rc = $SAP->RfcGetChars($hRow, "ZEILE", $StrByRef, 256);
          $charBuffer = $SAP->strByRef();
          echo $charBuffer . "<br />";
          if ($i < $RowCount) {
            $rc = $SAP->RfcMoveToNextRow($hTable);
          }
        }
      }

    $rc = $SAP->RfcDestroyFunction($hFunc);
    $rc = $SAP->RfcCloseConnection($hRFC);
    $SAP = null;

?>

  </body>

</html>


Conlusion
As you can see it is very easy to connect an SAP system from PHP via CCo.

2017/03/20: I check it successfully with the PHP release 7.1.3 x86 and x64.

2017/01/21: I check it successfully with the PHP release 7.1.1 x86 and x64.

2016/12/28; I check it successfully with the PHP release 7.1.0 x86 and x64.

Enjoy it.

Cheers
Stefan

To report this post you need to login first.

Be the first to leave a comment

You must be Logged on to comment or reply to a post.

Leave a Reply