Additional Blogs by SAP
cancel
Showing results for 
Search instead for 
Did you mean: 
onno_bagijn
Advisor
Advisor
0 Kudos

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).

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 "
AirlineAirlineFlight NumberDateDepart.cityArrival cityDepartureArrival TimeArrival 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

11 Comments