Skip to Content

Every scripting language needs a quick start demo that shows more than just “Hello world”. Based on the Flight registration demo from ABAP, I created the first part of a demo for PHP-SAP scripting using the Scripting in a Box v0.0.1. This flight registration demo contains a sample in PHP code for searching and displaying flight information from the SAP Web Application server. It uses the open source SAPRFC PHP extension to communicate with the SAP WAS system. The search page makes use of the PEAR Structures DataGrid package for displaying search results in a table and paging (displaying the result over multiple pages) the results. PEAR, PEAR – PHP Extension and Application Repository, is a framework and distribution system for reusable PHP components.

Overview

Search for flight information. Communication with the WAS is established via the RFC component. The following two remote function modules are used to retrieve the information:

  • BAPI_FLIGHT_GETDETAIL
  • BAPI_FLIGHT_GETLIST

Functional features of this demo are: Search through the available flights in the database of the SAP WAS system (BAPI_FLIGHT_GETLIST). Select a flight connection to check the seat availability and connection information like flight time, distance and airplane type (BAPI_FLIGHT_GETDETAIL).

image

PHP source code

index.php:
   

PHP & SAP – Flight Demo Part 1

Server
System number
Client
User
Password
Use PEAR packages

search.php:
     

Airline:
Depart. city:
Arrival city:

Logon first”; exit; } ?>
$_SESSION[‘Server’], “SYSNR”=>$_SESSION[‘Sysnum’], “CLIENT”=>$_SESSION[‘Client’], “USER”=>$_SESSION[‘User’], “PASSWD”=>$_SESSION[‘Pass’], “CODEPAGE”=>”1100”); //Try to connect to SAP using our Login array $rfc = saprfc_open ($LOGIN); if(!$rfc){ echo “The RFC connection has failed with the following error:”.saprfc_error(); exit; } //We must know if the function really exists $fce = saprfc_function_discover($rfc, “BAPI_FLIGHT_GETLIST”); if(!$fce){ echo “The function module has failed.”; echo $rfc; exit; } //Pass import parameters saprfc_import ($fce,”AIRLINE”, $airline); saprfc_import ($fce,”DESTINATION_FROM”,array(‘AIRPORTID’ => ”, ‘CITY’ => $cityfrom, ‘COUNTR’ => ”, ‘COUNTR_ISO’ => ”)); saprfc_import ($fce,”DESTINATION_TO”,array(‘AIRPORTID’ => ”, ‘CITY’ => $cityto, ‘COUNTR’ => ”, ‘COUNTR_ISO’ => ”)); //Pass table parameters saprfc_table_init ($fce,”FLIGHT_LIST”); saprfc_table_init ($fce,”RETURN”); //Call and execute the function $rc = saprfc_call_and_receive ($fce); if ($rfc_rc != SAPRFC_OK){ if ($rfc == SAPRFC_EXCEPTION ){ echo (“Exception raised: “.saprfc_exception($fce)); } else { echo (“Call error: “.saprfc_error($fce)); } exit; } //Fetch the data from the internal tables $data_row = saprfc_table_rows ($fce,”FLIGHT_LIST”); $field_row = saprfc_table_rows ($fce,”RETURN”); for ($i=0; $i<=$data_row-1; $i++){ $DATARow = saprfc_table_read ($fce,”FLIGHT_LIST”,$i+1); $data[$i] = $DATARow; } //realease the function and close the connection saprfc_function_free($fce); saprfc_close($rfc); require_once(‘Structures/DataGrid.php’); // Class for creating a link (in this case ‘Info’) for each row. class Printer { function printLink($params) { extract($params); $Airlineid = $record[‘AIRLINEID’]; $Connectid = $record[‘CONNECTID’]; $Arrdate = $record[‘ARRDATE’]; return “$label“; } } // Define New DataGrid with a limit of 3 records $dg =& new Structures_DataGrid(10); // Define DataGrid Color Attributes $dg->renderer->setTableHeaderAttributes(array(‘bgcolor’ => ‘#3399FF’)); $dg->renderer->setTableOddRowAttributes(array(‘bgcolor’ => ‘#CCCCCC’)); $dg->renderer->setTableEvenRowAttributes(array(‘bgcolor’ => ‘#EEEEEE’)); // Define DataGrid Table Attributes $dg->renderer->setTableAttribute(‘width’, ‘100%’); $dg->renderer->setTableAttribute(‘cellspacing’, ‘1’); $dg->renderer->setTableAttribute(‘cellpadding’, ‘4’); $dg->renderer->setTableAttribute(‘class’, ‘datagrid’); $dg->renderer->sortIconASC = “⇑”; $dg->renderer->sortIconDESC = “⇓”; // Set empty row table attributes $dg->renderer->allowEmptyRows(true, array(‘bgcolor’ => ‘#FFFFFF’)); // Define columns for the DataGrid $column = new Structures_DataGrid_Column(‘Airline’, ‘AIRLINEID’, null, array(‘width’ => ‘5%’)); $dg->addColumn($column); $column = new Structures_DataGrid_Column(‘Airline’, ‘AIRLINE’, null, array(‘width’ => ‘25%’)); $dg->addColumn($column); $column = new Structures_DataGrid_Column(‘Flight Number’, ‘CONNECTID’, null, array(‘width’ => ‘5%’)); $dg->addColumn($column); $column = new Structures_DataGrid_Column(‘Date’, ‘FLIGHTDATE’, null, array(‘width’ => ‘5%’)); $dg->addColumn($column); $column = new Structures_DataGrid_Column(‘Depart.city’, ‘CITYFROM’, null, array(‘width’ => ‘25%’)); $dg->addColumn($column); $column = new Structures_DataGrid_Column(‘Arrival city’, ‘CITYTO’, null, array(‘width’ => ‘25%’)); $dg->addColumn($column); $column = new Structures_DataGrid_Column(‘Departure’, ‘DEPTIME’, null, array(‘width’ => ‘5%’)); $dg->addColumn($column); $column = new Structures_DataGrid_Column(‘Arrival Time’, ‘ARRTIME’, null, array(‘width’ => ‘5%’)); $dg->addColumn($column); $column = new Structures_DataGrid_Column(‘Arrival date’, ‘ARRDATE’, null, array(‘width’ => ‘5%’)); $dg->addColumn($column); $column = new Structures_DataGrid_Column(”, null, null, array(‘align’ => ‘center’), null, ‘Printer::printLink($label=Info)’); $dg->addColumn($column); // Option #3 Bind directly to any data type $dg->bind($data); // Print the DataGrid $dg->render(); echo $dg->renderer->getPaging(); ?>

indexHTML.php:
     

Airline:
Depart. city:
Arrival city:

Logon first”; exit; } ?>
$_SESSION[‘Server’], “SYSNR”=>$_SESSION[‘Sysnum’], “CLIENT”=>$_SESSION[‘Client’], “USER”=>$_SESSION[‘User’], “PASSWD”=>$_SESSION[‘Pass’], “CODEPAGE”=>”1100”); //Try to connect to SAP using our Login array $rfc = saprfc_open ($LOGIN); if(!$rfc){ echo “The RFC connection has failed with the following error:”.saprfc_error(); exit; } //We must know if the function really exists $fce = saprfc_function_discover($rfc, “BAPI_FLIGHT_GETLIST”); if(!$fce){ echo “The function module has failed.”; echo $rfc; exit; } //Pass import parameters saprfc_import ($fce,”AIRLINE”, $airline); saprfc_import ($fce,”DESTINATION_FROM”,array(‘AIRPORTID’ => ”, ‘CITY’ => $cityfrom, ‘COUNTR’ => ”, ‘COUNTR_ISO’ => ”)); saprfc_import ($fce,”DESTINATION_TO”,array(‘AIRPORTID’ => ”, ‘CITY’ => $cityto, ‘COUNTR’ => ”, ‘COUNTR_ISO’ => ”)); //Pass table parameters saprfc_table_init ($fce,”FLIGHT_LIST”); saprfc_table_init ($fce,”RETURN”); //Call and execute the function $rc = saprfc_call_and_receive ($fce); if ($rfc_rc != SAPRFC_OK){ if ($rfc == SAPRFC_EXCEPTION ){ echo (“Exception raised: “.saprfc_exception($fce)); } else { echo (“Call error: “.saprfc_error($fce)); } exit; } //Fetch the data from the internal tables $data_row = saprfc_table_rows ($fce,”FLIGHT_LIST”); $field_row = saprfc_table_rows ($fce,”RETURN”); echo ”; echo ”; echo ”; echo ”; echo ”; echo ”; echo ”; echo ”; echo ”; echo ”; echo ”; echo ”; for ($i=1; $i<=$data_row; $i++){ $DATARow = saprfc_table_read ($fce,”FLIGHT_LIST”,$i); //Simple zebra algorithm $rem = $i % 2; if($rem == 0){ echo “”; } else { echo “”; } echo “”; echo “”; echo “”; echo “”; echo “”; echo “”; echo “”; echo “”; echo “”; echo “”; echo “”; } echo “

Airline Airline Flight Number Date Depart.city Arrival city Departure Arrival Time Arrival date
“.$DATARow[‘AIRLINEID’].” “.$DATARow[‘AIRLINE’].” “.$DATARow[‘CONNECTID’].” “.$DATARow[‘FLIGHTDATE’].” “.$DATARow[‘CITYFROM’].” “.$DATARow[‘CITYTO’].” “.$DATARow[‘DEPTIME’].” “.$DATARow[‘ARRTIME’].” “.$DATARow[‘ARRDATE’].” info

“; //realease the function and close the connection saprfc_function_free($fce); saprfc_close($rfc); ?>

flight_detail.php:
    Logon first”; exit(); } ?> go back

$_SESSION[‘Server’], “SYSNR”=>$_SESSION[‘Sysnum’], “CLIENT”=>$_SESSION[‘Client’], “USER”=>$_SESSION[‘User’], “PASSWD”=>$_SESSION[‘Pass’], “CODEPAGE”=>”1100”); //Try to connect to SAP using our Login array $rfc = saprfc_open ($LOGIN); if(!$rfc){ echo “The RFC connection has failed with the following error:”.saprfc_error(); exit; } //We must know if the function really exists $fce = saprfc_function_discover($rfc, “BAPI_FLIGHT_GETDETAIL”); if(!$fce){ echo “The function module has failed.”; echo $rfc; exit; } //Convert to uppercase the name of the table to show $Airlineid = $_GET[‘AIRLINEID’]; $Connectid = $_GET[‘CONNECTID’]; $Arrdate = $_GET[‘ARRDATE’]; //Pass import parameters saprfc_import ($fce,”AIRLINEID”, $Airlineid); saprfc_import ($fce,”CONNECTIONID”, $Connectid); saprfc_import ($fce,”FLIGHTDATE”, $Arrdate); //Pass table parameters saprfc_table_init ($fce,”RETURN”); //Call and execute the function $rc = saprfc_call_and_receive ($fce); if ($rfc_rc != SAPRFC_OK){ if ($rfc == SAPRFC_EXCEPTION ){ echo (“Exception raised: “.saprfc_exception($fce)); } else { echo (“Call error: “.saprfc_error($fce)); } exit; } $return_row = saprfc_table_rows ($fce,”RETURN”); $addInfo = saprfc_export ($fce,”ADDITIONAL_INFO”); $availInfo = saprfc_export ($fce,”AVAILIBILITY”); $data = array(); for ($i=0; $i<=$return_row-1; $i++){ $DATARow = saprfc_table_read ($fce,”RETURN”,$i+1); $data[$i] = $DATARow; } //realease the function and close the connection saprfc_function_free($fce); saprfc_close($rfc); if ($data[0][TYPE] == “E”) { echo “Error occurred : “.$data[0][MESSAGE]; exit; } echo “”; echo “”; echo “”; echo “”; echo “”; echo “”; echo “”; echo “

Flight Information
Flight time: “.$addInfo[‘FLIGHTTIME’].”
Distance: “.$addInfo[‘DISTANCE’].”
Plane type: “.$addInfo[‘PLANETYPE’].”
Availibility:
Economy class: “.$availInfo[‘ECONOFREE’].”/”.$availInfo[‘ECONOMAX’].”
Business class: “.$availInfo[‘BUSINFREE’].”/”.$availInfo[‘BUSINMAX’].”
Firest class: “.$availInfo[‘FIRSTFREE’].”/”.$availInfo[‘FIRSTMAX’].”

“; ?>

Installation

Create the above files and place them in a directory in the apache root directory or aliases directories. If your are using the ‘Scripting in a box’ copy the files to c:/development/htdocs/flightpart1/.

Run application: http://localhost:8080/flightpart1/

Install pear packages:

If you have Scripting in a Box v0.0.1 installed than all the Pear objects are already installed.

If you have ‘Scripting in a Box’ v0.0.1 installed or another web server environments. Goto c:/development/apache/php/ and run go-pear.bat. Once you have finished the base installment of PEAR. The next step is to install the ‘structure datagrid’ package and all dependable packages. Enter to following commands in the command line:

Run ‘go-pear.bat’ from the PHP home directory (for example c:/development/apache/php/).

cd c:developmentapachephp
pear install pear/Pager
pear install http://pear.php.net/get/HTML_Common-1.2.2.tgz
pear install http://pear.php.net/get/HTML_Table-1.6.1.tgz
pear install http://pear.php.net/get/Structures_DataGrid-0.6.3.tgz

To report this post you need to login first.

11 Comments

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

  1. Anton Wenzelhuemer
    And if someone wonders why he should use the RFC library if he’s using a built-in SOAP library to access any other service in this world, you could replace all the RFC stuff with (as an example for the getlist part) this:

    $login = “SAPUSER”;
    $password = “XXX”;
    $proxyhost = ‘12.12.12.12’;
    $wsdlurl = “http://your_host:50010/sap/bc/soap/wsdl11?services=BAPI_FLIGHT_GETLIST&sap-client=201&wsdl=1.1&sap-user=SAPUSER&sap-password=XXX&sap-language=DE“;

    function maketable($node) {
    // render table …
    return;
    }

    try {
    $client = new SoapClient($wsdlurl,
    array(
    ‘proxy_host’ => $proxyhost,
    ‘proxy_port’ => $proxyport,
    ‘login’ => $login,
    ‘password’ => $password,
    ‘trace’ => true,
    ‘exceptions’ => true));

    }
    catch(SoapFault $e) {
    echo ‘Caught an Constructor Error: [‘ . $e->faultcode . ‘] – ‘ . $e->faultstring;
    }

    if (isset($client)) {
    try {
    $ra = $client->BAPI_FLIGHT_GETLIST(array(
    ‘AIRLINE’ => ‘BEA’,
    ‘DATE_RANGE’ => ”,
    ‘EXTENSION_IN’ => ”,
    ‘EXTENSION_OUT’ => ”,
    ‘FLIGHT_LIST’ => ”,
    ‘RETURN’ => ”,
    ‘MAX_ROW’ => ”,
    ‘DESTINATION_FROM’ => ‘NEW YORK’,
    ‘DESTINATION_TO’ => ‘SAN FRANCISCO’
    ));

    maketable($ra->BAPISFLDAT);

    }
    catch (SoapFault $e) {
    echo ‘Caught an Error: [‘ . $e->faultcode . ‘] – ‘ . $e->faultstring;
    }
    }
    else { echo ‘
    No client object!’; }
    ?>

    regards,
    anton

    (0) 
      1. Alvaro Tejada Galindo
        Hey Anton…AFAIK you are a Bad Programmer writing dirty code…So we can’t blame for a litte mistake -;) Also, your WebServices approch are always a great inspiration -:)

        Greetings,

        Blag.

        (0) 
  2. Joris Bots
    hi all,

    I’ve installed SIAB v.0.0.1 and tried to connect to a remote sandbox (/H/sapgatea.wdf.sap.corp/S/3293/H/ from within SAP domain).

    When i try Onno’s demo or an example project from  Scripting in a box, all i get is errors like this:

    [code]
    Warning: RFC Error Info : Group : 102 Key : RFC_ERROR_COMMUNICATION Message : Connect to SAP gateway failed Connect_PM GWHOST=/H/sapgatea.wdf.sap.corp/S/3293/H/, GWSERV=sapgw77, ASHOST=/H/sapgatea.wdf.sap.corp/S/3293/H/, SYSNR=77 LOCATION SapRouter on sapgatei ERROR hostname ” unknown TIME Sat Apr 29 12:21:43 2006 RELEASE 40B COMPONENT NI (network interface) VERSION 31 RC -90 MODULE nixxi.c LINE 1588 DETAIL NiIHostToAddr COUNTER 70002 in C:\Development\htdocs\workspace\flight_demo\search.php on line 62
    The RFC connection has failed with the following error:RFC Error Info : Key : rfc system error raised Status : Message : for more information: see tracefile dev_rfc Internal:
    [/code]

    Is there a remote system that i CAN connect to to test this?
    Or is it something else i’m doing wrong?

    Thanks in advance,

    Joris Bots

    (0) 
  3. Joris Bots
    hi all,

    I’ve installed SIAB v.0.0.1 and tried to connect to a remote sandbox (/H/sapgatea.wdf.sap.corp/S/3293/H/ from within SAP domain).

    When i try Onno’s demo or an example project from  Scripting in a box, all i get is errors like this:

    [code]
    Warning: RFC Error Info : Group : 102 Key : RFC_ERROR_COMMUNICATION Message : Connect to SAP gateway failed Connect_PM GWHOST=/H/sapgatea.wdf.sap.corp/S/3293/H/, GWSERV=sapgw77, ASHOST=/H/sapgatea.wdf.sap.corp/S/3293/H/, SYSNR=77 LOCATION SapRouter on sapgatei ERROR hostname ” unknown TIME Sat Apr 29 12:21:43 2006 RELEASE 40B COMPONENT NI (network interface) VERSION 31 RC -90 MODULE nixxi.c LINE 1588 DETAIL NiIHostToAddr COUNTER 70002 in C:\Development\htdocs\workspace\flight_demo\search.php on line 62
    The RFC connection has failed with the following error:RFC Error Info : Key : rfc system error raised Status : Message : for more information: see tracefile dev_rfc Internal:
    [/code]

    Is there a remote system that i CAN connect to to test this?
    Or is it something else i’m doing wrong?

    Thanks in advance,

    Joris Bots

    (0) 
  4. Joris Bots
    Sorry guys, it was way to premature of me to post last message.
    I entered a sap router as a server, sorry.
    All runs smooth now.

    Joris Bots

    (0) 

Leave a Reply