Quantcast
Channel: Scripting Languages
Viewing all articles
Browse latest Browse all 42

How to use NetWeaver RFC Library with PHP and COM Connector (CCo)

$
0
0

Hello community,

 

PHP is a server side scripting language, you can find more information here. PHP offers access to COM objects on Win32 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. 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>

001.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->RfcCloseConnection($hRFC);  }  $SAP = null;

?>  </body></html>

 

002.jpg

 

Here now another example to get system information via RFC-enabled function module RFC_SYSTEM_INFO. Here only the PHP file, because the HTML file is nearly the same as in the example above.

<!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->RfcCloseConnection($hRFC);  $SAP = null;

?>  </body></html>

 

003.jpg

 

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

 

I check it successfully with PHP version 5.5.31, 5.6.17 and 7.0.2.

 

Enjoy it.

 

Cheers

Stefan


Viewing all articles
Browse latest Browse all 42

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>