Skip to Content
Author's profile photo John Astill

Simple ABAP grep with PHP and Web Services


This weblog combines a simple implementation of grep written for ABAP programs with a PHP front end. The PHP UI will use Web Services to communicate with the RFC.

The program searches the programs listed in TRDIR for the text supplied.

The development environment used was the fantastic Scripting in a Box v0.1 from Craig Cmehill



The ABAP code is a little rough around the edges. It does a simple search of the ABAP reports in TRDIR. For it to be more robust you may want to consider adding more options as parameters and using a cursor to limit the search results to a reasonable size.


For the unlucky few that have not used unix, you may be unfamiliar with the grep tool. This tool will search files for a given pattern. It is wonderful for finding entries in source code, log files etc.


The abap code is implemented as a Remote Enabled Function Module with 2 importing parameters and one exporting parameter. The importing parameters represent the string to search for and the files to search. The exporting parameter in the example is simply a table of strings that identify the program, its line and the line of text.


iv_string type string (pass by value)
iv_rep type string (pass by value)


et_results type ztt_strings (pass by value)

ABAP Function Module Source

TYPE string.

” Simple check so that not everything is returned.

  IF iv_string IS INITIAL AND iv_rep IS INITIAL.



  • Check the report string

  IF NOT iv_rep CS ‘%’.

    CONCATENATE iv_rep ‘%’ INTO lv_repname.


    lv_repname = iv_rep.



  lv_text = iv_string.

  • Get the report names.

  SELECT * FROM trdir INTO TABLE lt_trdir WHERE name LIKE lv_repname.

  IF sy-subrc EQ 0.


    lv_text = iv_string.

  • Get the report names.

    SELECT * FROM trdir INTO TABLE lt_trdir WHERE name LIKE lv_repname .

    IF sy-subrc EQ 0.

      LOOP AT lt_trdir INTO ls_trdir.

  • Now read the report and search the text.

        READ REPORT ls_trdir-name INTO lt_text.

        LOOP AT lt_text ASSIGNING


The PHP client is very simple. It contains a form for submitting the search and a report area for the results. I would not normally combine both in this way, but it makes it easier for example purposes.

On receiving the post, the client is created using the supplied WSDL file.

$client = new SoapClient("grep.wsdl",
array( "login" => "username",
'trace' => 1,
"password" => "password") );

The Function Module has been remote enabled and the WSDL file can be retrieved using the BSP application “webservicebrowser”. For this example I placed the downloaded WSDL file in the same directory as the php source file.

WSDL Source

My wsdl is included as a reference only. unless you know how to edit one by hand and also know your service location you are better of using the one you can get from “webservicebrowser” application.

PHP Source

To reuse this code you will simply need to change the client creation to use your wsdl file, password and username.

Search String

Report Name

if (isset($_POST[“sstring”])) {

echo “Call the Web Service to get the document list
// Same call using the PHP Web Service
try {
$client = new SoapClient(“grep.wsdl”,
array( “login” => “username”,
‘trace’ => 1,
“password” => “password”) );

// Create the complex array to match the structure defined in the WSDL.
$res = array( “IV_STRING” => $sstring,
“IV_REP” => $repname,

$res = $client->zja_grep($res );
} catch (SoapFault $exception) {
echo “


echo $exception;
echo “

Request :

“, htmlspecialchars($client->__getLastRequest()), “

echo “

Response :

“, htmlspecialchars($client->__getLastResponse()), “


echo “
$results = $res->ET_RESULTS;

if (isset($results->item)) {
echo “”;
echo “”;
while (list($k,$v)= each($results->item) ) {
echo “”;
echo “

Program and entry




A repeated word of warning, the service is not limiting the number of results returned, so if you specify a vague search, be prepared for a long list of values in the results.

Assigned tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member
      Nice to read you again John! -:) Another great blog!



      Author's profile photo Former Member
      Former Member
      Just one remark:
      I don't think it's necessary (or even good practice) to download the WSDL physically.

      In the constructor of the SOAP client you can retrieve the WSDL from it's original location just by using it's URL:

      new SoapClient("[http:the_host]:the_port/sap/bc/srt/rfc/sap/[script_name]?[parameters]", ...)

      in case of a real web service, a slightly different URL if you use the generic RFC to WS capability of the WAS. As mentioned above the WSDL's URL can be retrieved from WEBSERVICEBROWSER as well as transaction WSADMIN or SICF.

      IMHO, this approach is better, since it allows the service owner to change the actual endpoint of the service (e.g. application server) without any special notice to the consumers (as long as the WSDL stays in place).


      Author's profile photo John Astill
      John Astill
      Blog Post Author
      Thanks Anton, that's a very good point.