Consuming SAP NetWeaver Gateway Services from PHP Applications
Copyright Copyright 2011 SAP AG. All rights reserved. SAP Library document classification: PUBLIC No part of this publication may be reproduced or transmitted in any form or for any purpose without the express permission of SAP AG. The information contained herein may be changed without prior notice. Some software products marketed by SAP AG and its distributors contain proprietary software components of other software vendors. Microsoft, Windows, Excel, Outlook, and PowerPoint are registered trademarks of Microsoft Corporation. IBM, DB2, DB2 Universal Database, System i, System i5, System p, System p5, System x, System z, System z10, System z9, z10, z9, iseries, pseries, xseries, zseries, eserver, z/vm, z/os, i5/os, S/390, OS/390, OS/400, AS/400, S/390 Parallel Enterprise Server, PowerVM, Power Architecture, POWER6+, POWER6, POWER5+, POWER5, POWER, OpenPower, PowerPC, BatchPipes, BladeCenter, System Storage, GPFS, HACMP, RETAIN, DB2 Connect, RACF, Redbooks, OS/2, Parallel Sysplex, MVS/ESA, AIX, Intelligent Miner, WebSphere, Netfinity, Tivoli and Informix are trademarks or registered trademarks of IBM Corporation. Linux is the registered trademark of Linus Torvalds in the U.S. and other countries. Adobe, the Adobe logo, Acrobat, PostScript, and Reader are either trademarks or registered trademarks of Adobe Systems Incorporated in the United States and/or other countries. Oracle is a registered trademark of Oracle Corporation. UNIX, X/Open, OSF/1, and Motif are registered trademarks of the Open Group. Citrix, ICA, Program Neighborhood, MetaFrame, WinFrame, VideoFrame, and MultiWin are trademarks or registered trademarks of Citrix Systems, Inc. HTML, XML, XHTML and W3C are trademarks or registered trademarks of W3C, World Wide Web Consortium, Massachusetts Institute of Technology. Java is a registered trademark of Sun Microsystems, Inc. JavaScript is a registered trademark of Sun Microsystems, Inc., used under license for technology invented and implemented by Netscape. SAP, R/3, SAP NetWeaver, Duet, PartnerEdge, ByDesign, SAP BusinessObjects Explorer, StreamWork, and other SAP products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of SAP AG in Germany and other countries. Business Objects and the Business Objects logo, BusinessObjects, Crystal Reports, Crystal Decisions, Web Intelligence, Xcelsius, and other Business Objects products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Business Objects Software Ltd. Business Objects is an SAP company. Sybase and Adaptive Server, ianywhere, Sybase 365, SQL Anywhere, and other Sybase products and services mentioned herein as well as their respective logos are trademarks or registered trademarks of Sybase, Inc. Sybase is an SAP company. All other product and service names mentioned are the trademarks of their respective companies. Data contained in this document serves informational purposes only. National product specifications may vary. These materials are subject to change without notice. These materials are provided by SAP AG and its affiliated companies ("SAP Group") for informational purposes only, without representation or warranty of any kind, and SAP Group shall not be liable for errors or omissions with respect to the materials. The only warranties for SAP Group products and services are those that are set forth in the express warranty statements accompanying such products and services, if any. Nothing herein should be construed as constituting an additional warranty.
Table of Contents Consuming SAP NetWeaver Gateway Services on PHP Applications... 4 Prerequisites... 5 Building a PHP Application... 6 Function Import Example... 6 Read Example... 9 Create Example... 10 Creation of Request Body for Create Method:... 12 Authentication Using X.509 Certificate... 13 Prerequisites... 13 Read Example... 14 Appendix 1 - RMTSAMPLEFLIGHT Service Metadata Description...16 Appendix 2 Code Snippets...22 Read - Get Carrier by Carrier ID... 22 Function Import Get available Flights... 23 Create - Booking a Flight... 24 Read - Get Carrier by Carrier ID Using X.509 Certificate Authentication... 25
CONSUMING SAP NETWEAVER GATEWAY SERVICES FROM PHP APPLICATIONS SAP NetWeaver Gateway technology provides a simple way to interact with SAP applications through variety of devices, environments and platforms based on market standards. The framework enables development of innovative, people-centric solutions that bring the power of SAP business software into new experiences, such as: social networking and collaboration environments; mobile and tablet devices; and rich internet applications. The framework supports rapid innovation while ensuring security, integrity, management and optimized maintenance of the core SAP systems. Completely flexible, the software offers connectivity to SAP applications using any programming language or model without the need for SAP knowledge by taking advantage of REST services and OData/ATOM protocols. This guide provides information on how to consume SAP NetWeaver Gateway services from PHP applications based on the following scenario: The user wants to fly from New York to San Francisco. He goes online and searches for the different flights available. He enters his search parameters: city of origin, destination city, departure date, and arrival date (Function Import). From the SAP NetWeaver Gateway, he receives the information for the option(s) that meet(s) his search parameters (Get Function). He can now select the one that suits him the most and book his flight (Create Function). The links to access the service document and metadata document are provided the in table below. Service Document Metadata Document http://<gateway_host>:<gateway_port>/sap/opu/sdata/iwfnd/rmtsampleflight/ http://<gateway_host>:<gateway_port>/sap/opu/sdata/iwfnd/rmtsampleflight/ $metadata?$format=xml Each service URL should contain the following information: Gateway host Port SAP Client no need to specify client if connecting to the default client. Example: http://<gateway_host>:<gateway_port>/sap/opu/sdata/iwfnd/rmtsampleflight?sap-client=100 To obtain information on the selected service (its properties, function imports, associations, etc.), refer to Appendix 1, were we show the Metadata description of the RMTSAMPLEFLIGHT service. To view the code snippets in a format that can be copied into new code, refer to Appendix 2 Code Snippets.
The Flight service contains the following collections: Collection FlightCollection (list of flights) CarrierCollection (list of carriers) BookingsCollection (list of Bookings) URL http://<gateway_host>:<gateway_port>/sap/opu/ sdata/iwfnd/rmtsampleflight/flightcollection http://<gateway_host>:<gateway_port>/sap/opu/sdata/ iwfnd/rmtsampleflight/carriercollection http://<gateway_host>:<gateway_port >/sap/opu/sdata/ iwfnd/rmtsampleflight/bookingcollection The operation GetAvailableFlights on FlightCollection is used to get a list of flights. The following is the URL for the same: http://<gateway_host>:<gateway_port>/sap/opu/sdata/iwfnd/rmtsampleflight/getavailableflights Each collection supports some or all of the operations listed below. The table below provides the list of operations supported and the methods that each of this operations map to respectively: Operation Create Read Update Delete Method HTTP POST GET PUT DELETE Prerequisites The following tools are required for developing a PHP application: Software SAP NetWeaver Gateway 2.0 Version PHP To download go to: http://www.php.net/ OData SDK for PHP Contact your system administrator for landscape details to connect to the SAP NetWeaver Gateway system.
Make sure you have performed the following procedures: Install and configure OData SDK as described in the OData SDK for PHP User Guide. You can find the user guide in the OData SDK folder, at: \doc\user_guide.htm. Add proxies for the relevant service to your PHP project as described in the OData SDK for PHP User Guide. Note: It is recommended to save the relevant service metadata into a local file and use it while generating the proxies. Require the generated proxy file. require_once "RMTSAMPLEFLIGHT.php"; Building a PHP Application Function Import Example To get a list of the available flights, based on filter criteria, trigger the GetAvailableFlights function import with cityfrom, cityto, fromdate and todate parameters. The response will contain a list of flights that meet the criteria. Note: For more information on Function Import (also called Service Operation), refer to OData documentation at http://www.odata.org/developers/protocols/uri-conventions under the Addressing Service Operations section. Refer to the code snippet below for details on how to do it from a PHP application: 1. Initialize the service proxy object, for example RMTSAMPLEFLIGHT, using the service URL. $proxy = new RMTSAMPLEFLIGHT ('http://<gateway_host>:<gateway_port>/sap/opu/sdata/iwfnd/rmtsampleflight'); 2. Enter the service credentials. $proxy->credential = new WindowsCredential('<Username>', '<Password>'); 3. Add the XMLHttpRequest http header needed for POST requests. $proxy->addheader('x-requested-with', 'XMLHttpRequest'); 4. Execute a query calling the GetAvailableFlights function import with the following parameters: flight departure date, arrival date, and cities. $city_from = 'NEW YORK'; $city_to = 'SAN FRANCISCO'; $from_date = '20110105'; $to_date = '20110728'; $flights = $proxy->execute("getavailableflights?cityfrom=$city_from&cityto=$city_to &fromdate=$from_date&todate=$to_date")->result;
5. Iterate all objects in the result obtained above. foreach ($flights as $flight) { echo "Flight Carrier: ". $flight->carrid. "<br/>"; echo "Flight Price: ". $flight->price. "<br/>"; echo "Flight Max. Seats: ". $flight->seatsmax. "<br/><br/>"; } Refer to the full code snippet found in Appendix 2. Response <?xml version="1.0" encoding="utf-8"?> - <atom:feed xml:base="<gateway_host>:<gateway_port>/sap/opu/sdata/iwfnd/rmtsampleflight/" xmlns:atom="http://www.w3.org/2005/atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:sap="http://www.sap.com/protocols/sapdata"> <atom:id>http://<host name>:<port>/sap/opu/sdata/iwfnd/rmtsampleflight/flightcollection</atom:id> <atom:link href="flightcollection" rel="self" type="application/atom+xml;type=feed" title="flightcollection" /> <atom:title>flightcollection</atom:title> <atom:updated>2011-09-27t06:32:42z</atom:updated> - <atom:entry> <atom:author /> - <atom:content type="application/xml"> - <m:properties> <d:carrid>aa</d:carrid> <d:connid>0017</d:connid> <d:fldate m:type="edm.datetime">2011-01-05t00:00:00</d:fldate> <d:price m:type="edm.decimal">889.00</d:price> <d:currency>usd</d:currency> <d:planetype>747-400</d:planetype> <d:seatsmax>385</d:seatsmax> <d:seatsocc>367</d:seatsocc> <d:paymentsum m:type="edm.decimal">185742.73</d:paymentsum> <d:seatsmax_b>31</d:seatsmax_b> <d:seatsocc_b>22</d:seatsocc_b> <d:seatsmax_f>21</d:seatsmax_f> <d:seatsocc_f>21</d:seatsocc_f> - <d:flightdetails> <d:countryfrom>us</d:countryfrom> <d:cityfrom>new york</d:cityfrom> <d:airportfrom>jfk</d:airportfrom> <d:countryto>us</d:countryto> <d:cityto>san FRANCISCO</d:cityTo> <d:airportto>sfo</d:airportto> <d:flighttime>361</d:flighttime> <d:departuretime m:type="edm.time">pt11h00m00s</d:departuretime> <d:arrivaltime m:type="edm.time">pt14h01m00s</d:arrivaltime> <d:distance m:type="edm.decimal">2574.0000</d:distance> <d:distanceunit>smi</d:distanceunit> <d:flighttype /> <d:period m:type="edm.byte">0</d:period> </d:flightdetails> </m:properties> </atom:content> <atom:id>http://<gateway_host>:<gateway_port>/sap/opu/sdata/iwfnd/rmtsampleflight/flightcollection(carrid='aa',conn id='0017',fldate='20110105')</atom:id> <atom:link href="flightcollection(carrid='aa',connid='0017',fldate='20110105')" rel="edit" type="application/atom+xml;type=entry" /> <atom:link href="flightcollection(carrid='aa',connid='0017',fldate='20110105')/flightbooking" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/flightbooking" type="application/atom+xml;type=entry" title="rmtsampleflight.bookingflight" /> <atom:link href="flightcollection(carrid='aa',connid='0017',fldate='20110105')/flightbookings" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/flightbookings" type="application/atom+xml;type=feed" title="rmtsampleflight.flightbookings" />
<atom:link href="flightcollection(carrid='aa',connid='0017',fldate='20110105')/flightcarrier" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/flightcarrier" type="application/atom+xml;type=entry" title="rmtsampleflight.carriertoflight" /> <atom:title>flight</atom:title> <atom:updated>2011-09-27t06:32:42z</atom:updated> </atom:entry> - <atom:entry> <atom:author /> - <atom:content type="application/xml"> - <m:properties> <d:carrid>aa</d:carrid> <d:connid>0017</d:connid> <d:fldate m:type="edm.datetime">2011-03-16t00:00:00</d:fldate> <d:price m:type="edm.decimal">422.94</d:price> <d:currency>usd</d:currency> <d:planetype>747-400</d:planetype> <d:seatsmax>385</d:seatsmax> <d:seatsocc>371</d:seatsocc> <d:paymentsum m:type="edm.decimal">192129.22</d:paymentsum> <d:seatsmax_b>31</d:seatsmax_b> <d:seatsocc_b>30</d:seatsocc_b> <d:seatsmax_f>21</d:seatsmax_f> <d:seatsocc_f>20</d:seatsocc_f> - <d:flightdetails> <d:countryfrom>us</d:countryfrom> <d:cityfrom>new york</d:cityfrom> <d:airportfrom>jfk</d:airportfrom> <d:countryto>us</d:countryto> <d:cityto>san FRANCISCO</d:cityTo> <d:airportto>sfo</d:airportto> <d:flighttime>361</d:flighttime> <d:departuretime m:type="edm.time">pt11h00m00s</d:departuretime> <d:arrivaltime m:type="edm.time">pt14h01m00s</d:arrivaltime> <d:distance m:type="edm.decimal">2574.0000</d:distance> <d:distanceunit>smi</d:distanceunit> <d:flighttype /> <d:period m:type="edm.byte">0</d:period> </d:flightdetails> </m:properties> </atom:content> <atom:id>http://<gateway_host>:<gateway_port>/sap/opu/sdata/iwfnd/rmtsampleflight/flightcollection(carrid='aa',conn id='0017',fldate='20110316')</atom:id> <atom:link href="flightcollection(carrid='aa',connid='0017',fldate='20110316')" rel="edit" type="application/atom+xml;type=entry" /> <atom:link href="flightcollection(carrid='aa',connid='0017',fldate='20110316')/flightbooking" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/flightbooking" type="application/atom+xml;type=entry" title="rmtsampleflight.bookingflight" /> <atom:link href="flightcollection(carrid='aa',connid='0017',fldate='20110316')/flightbookings" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/flightbookings" type="application/atom+xml;type=feed" title="rmtsampleflight.flightbookings" /> <atom:link href="flightcollection(carrid='aa',connid='0017',fldate='20110316')/flightcarrier" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/flightcarrier" type="application/atom+xml;type=entry" title="rmtsampleflight.carriertoflight" /> <atom:title>flight</atom:title> <atom:updated>2011-09-27t06:32:42z</atom:updated> </atom:entry>
Read Example To read the carrier details, you must call the CarrierCollection using the Carrier ID parameter. Refer to the code snippet below for details on how to do it from a PHP application: 1. Initialize the service proxy object, for example RMTSAMPLEFLIGHT using the service URL. $proxy = new RMTSAMPLEFLIGHT ('http://<gateway_host>:<gateway_port>/sap/opu/sdata/iwfnd/rmtsampleflight'); 2. Enter the service credentials. $proxy->credential = new WindowsCredential('<Username>', '<Password>'); 3. Specify the carrier ID parameter for which we want to receive information. $carrier_id = 'AA'; //American Airlines 4. Execute the query below on the service to retrieve the carrier and save the result. The result returned in the response is an array containing the specific Carrier object. $query = $proxy->carriercollection()->filter("carrid eq '$carrier_id'"); $carriers = $query->execute()->result; 5. Use the result to display the information obtained. echo "Carrier ID: ". $carriers[0]->carrid. "<br/>"; echo "Carrier Name: ". $carriers[0]->carrname. "<br/>"; echo "Carrier Code: ". $carriers[0]->currcode. "<br/>"; echo "Carrier URL: ". $carriers[0]->url. "<br/>"; Refer the full code snippet found in Appendix 2. Response <?xml version="1.0" encoding="utf-8"?> - <atom:entry xml:base="<gateway_host>:<gateway_port>/sap/opu/sdata/iwfnd/rmtsampleflight/" xmlns:atom="http://www.w3.org/2005/atom" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:sap="http://www.sap.com/protocols/sapdata"> <atom:author /> <atom:content src="http://www.aa.com" /> - <m:properties> <d:carrid>aa</d:carrid> <d:carrname>american Airlines</d:CARRNAME> <d:currcode>usd</d:currcode> <d:url>http://www.aa.com</d:url> <d:mimetype /> </m:properties> <atom:id><gateway_host>:<gateway_port>/sap/opu/sdata/iwfnd/rmtsampleflight/carriercollection(carrid='aa')</atom:i d> <atom:link href="carriercollection(carrid='aa')" rel="self" type="application/atom+xml;type=entry" /> <atom:link href="carriercollection(carrid='aa')/carrierflights" rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/carrierflights" type="application/atom+xml;type=feed" title="rmtsampleflight.carriertoflight" /> <atom:title>carrier</atom:title> <atom:updated>2011-09-27t10:00:38z</atom:updated> </atom:entry>
Create Example To book a flight, trigger the HTTP POST method on the Booking Collection (with the Booking details in the request body). Check for the HTTP response code. If the response code is 201, then the HTTP-POST is successful. The response contains the newly created Booking ID in the bookid field, as well as the entire information that was part of the request body. Refer to the code snippet below for details on how to invoke a create request from a PHP application: 1. Initialize the service proxy object, for example RMTSAMPLEFLIGHT, using the service URL. $proxy = new RMTSAMPLEFLIGHT ('http://<gateway_host>:<gateway_port>/sap/opu/sdata/iwfnd/rmtsampleflight'); 2. Enter the service credentials. $proxy->credential = new WindowsCredential('<Username>', '<Password>'); 3. Create the new Booking item. //Create a booking object with key properties: $carrid = 'AA'; $connid = '0017'; $fldate = '2011-12-21T00:00:00'; $bookid = ''; $booking = Booking::CreateBooking($carrid, $connid, $fldate, $bookid); //Define additional booking properties: $booking->customid = '00004617'; $booking->custtype = 'P'; $booking->wunit = 'KG'; $booking->luggweight = '14.4000'; $booking->class = 'Y'; $booking->forcuram = '879.82'; $booking->forcurkey = 'USD'; $booking->loccuram = '803.58'; $booking->loccurkey = 'USD'; $booking->order_date = '2011-05-22T00:00:00'; $booking->counter = '00000000'; $booking->agencynum = '00000325'; $booking->passname = 'Joe Smith'; $booking->passform = '1234567'; $booking->passbirth = '1990-10-10T00:00:00'; 4. Add the new Booking item to the BookingCollection. Note: The service is not aware of these changes until the SaveChanges method is called. $proxy->addtobookingcollection($booking); 5. Add the XMLHttpRequest http header needed for POST requests. $proxy->setentityheaders($booking, array('x-requested-with' => 'XMLHttpRequest'));
6. Save all changes made to the service (this will trigger the request). Note: Define the SaveChangesOptions property to send each change in a separate request, for services which do not support OData $batch function, as RMTSAMPLEFLIGHT. $proxy->setsavechangesoptions(savechangesoptions::none); $proxy->savechanges(); Note: The SaveChanges method may cause printing of some PHP warnings. To hide these warnings, set the error_reporting configuration using the following code: error_reporting(e_all & ~E_NOTICE); //Show all errors, except for notices and coding standards warnings Refer the full code snippet found in Appendix 2.
Creation of Request Body for Create Method: The request body can be easily created by looking at the tags in the collection s metadata. For example, the metadata for Booking Collection provides information about the various fields (tags) that are needed for creating a booking: <Property Name="carrid" Type="Edm.String" Nullable="false" MaxLength="3" sap:label="airline" <Property Name="connid" Type="Edm.String" Nullable="false" MaxLength="4" sap:label="flight Number" <Property Name="fldate" Type="Edm.DateTime" Nullable="false" Precision="10" sap:label="date" <Property Name="bookid" Type="Edm.String" Nullable="false" MaxLength="8" sap:label="booking number" <Property Name="CUSTOMID" Type="Edm.String" MaxLength="8" sap:label="customer Number" <Property Name="CUSTTYPE" Type="Edm.String" MaxLength="1" sap:label="b/p customer" <Property Name="SMOKER" Type="Edm.String" MaxLength="1" sap:label="smoker" <Property Name="WUNIT" Type="Edm.String" MaxLength="3" sap:label="unit of measure" <Property Name="LUGGWEIGHT" Type="Edm.Decimal" Precision="10" Scale="4" sap:label="luggage Weight" sap:filterable="false" sap:unit="wunit"/> <Property Name="INVOICE" Type="Edm.String" MaxLength="1" sap:label="invoice pty." <Property Name="CLASS" Type="Edm.String" MaxLength="1" sap:label="class" <Property Name="FORCURAM" Type="Edm.Decimal" Precision="20" Scale="2" sap:label="amount" <Property Name="FORCURKEY" Type="Edm.String" MaxLength="5" sap:label="paymnt currency" <Property Name="LOCCURAM" Type="Edm.Decimal" Precision="20" Scale="2" sap:label="amount" <Property Name="LOCCURKEY" Type="Edm.String" MaxLength="5" sap:label="airline Currency" <Property Name="ORDER_DATE" Type="Edm.DateTime" Precision="10" sap:label="booking date" <Property Name="COUNTER" Type="Edm.String" MaxLength="8" sap:label="sales office" <Property Name="AGENCYNUM" Type="Edm.String" MaxLength="8" sap:label="agency No." <Property Name="CANCELLED" Type="Edm.String" MaxLength="1" sap:label="cancelation flag" <Property Name="RESERVED" Type="Edm.String" MaxLength="1" sap:label="reserved" <Property Name="PASSNAME" Type="Edm.String" MaxLength="25" sap:label="passenger Name" <Property Name="PASSFORM" Type="Edm.String" MaxLength="15" sap:label="title" <Property Name="PASSBIRTH" Type="Edm.DateTime" Nullable="true" Precision="10" sap:label="dob of Passeng."
Authentication Using X.509 Certificate In the code snippets above, the requests sent to the service use Basic Authentication. If X.509 Certificate Authentication is needed, the X.509 Client Certificate should be attached to any request sent to the service. In addition, a CA Certificate of the SAP NetWeaver Gateway server should be used for server authentication, as part of the SSL Handshake process. Note: OData generated proxies do not support X.509 Certificate Authentication. Therefore the service requests should be sent using PHP libraries which support SSL and X.509 Certificate Authentication (as curl used in the code snippets below). In addition, the developer is responsible for parsing the service response according to the OData / SAP Data Protocols. Prerequisites The following tools are required for developing a PHP application which uses X.509 Certificate Authentication (in addition to the tools mentioned above as prerequisites for developing a PHP application): Software Version PHP curl Extension Note: OData SDK for PHP is not needed as a prerequisite since it does not support X.509 Certificate Authentication. Make sure you have performed the following procedures: Install and configure the curl library. In a Windows operating system, this can be done by enabling the php_curl.dll in php.ini configuration file: Search for 'extension=php_curl.dll' in the php.ini file and remove the semicolon (;) in front of it. Export the root CA Certificate of the SAP NetWeaver Gateway server into a local file, which is accessible from your PHP project. You may use any browser program for that. Make sure the certificate is saved in Base64-encoded format. Store the X.509 Client Certificate (which should be attached to each request sent to the service) into a local file accessible from your PHP project. Make sure the certificate is saved in PEM format. If the certificate is saved in a different format, you must convert it to PEM format using a converter tool, such as OpenSSL. For example, to convert a PKCS#12 file (.pfx.p12) containing a private key and certificates to PEM format, execute the following command: openssl pkcs12 -in <Original_Certificate_Path>.pfx -out <Converted_Certificate_ Path>.pem nodes
Read Example To read the carrier details, you must call the CarrierCollection using the Carrier ID parameter. Refer to the code snippet below for details on how to do it from a PHP application using X.509 Certificate Authentication: 1. Create a service request object using the curl library. $curlreq = curl_init(); 2. Specify the carrier ID parameter for which you want to receive information. $carrier_id = 'AA'; //American Airlines 3. Create a Get Specific Carrier query string and set it as the request URL. Note: Use an https protocol to trigger an SSL Handshake process which is needed for X.509 Certificate Authentication. $query_url = "https://<gateway_host>:<gateway_ssl_port>/sap/opu/sdata/iwfnd/ RMTSAMPLEFLIGHT/CarrierCollection(carrid='$carrier_id')"; curl_setopt($curlreq, CURLOPT_URL, $query_url); 4. Set the request general options to support redirection and response returned as a string. curl_setopt($curlreq, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlreq, CURLOPT_FOLLOWLOCATION, true); 5. Set request options for the server verification during the SSL Handshake, including defining the trusted CA Certificate location. curl_setopt($curlreq, CURLOPT_SSL_VERIFYHOST, 1); curl_setopt($curlreq, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($curlreq, CURLOPT_CAINFO, '<Gateway_CA_Certificate_Path>.cer'); 6. Attach the stored X.509 Client Certificate to the request. curl_setopt($curlreq, CURLOPT_SSLCERT, '<Client_Certificate_Path>.pem'); 7. Send the request and handle the service response. $curlres = curl_exec($curlreq); //Handle response: if(!curl_errno($curlreq)) { $info = curl_getinfo($curlreq); echo $curlres; } else { echo 'CURL error: '. curl_error($curlreq); }
Note: The service response should be parsed according to the OData/SAP Data Protocols to be used as demonstrated in the Basic Authentication Read Example. This parsing is not demonstrated in this code snippet, and should be implemented by the developer, if needed. 8. Close the request object. curl_close($curlreq); Refer the full code snippet found in Appendix 2.
Appendix 1 - RMTSAMPLEFLIGHT Service Metadata Description <?xml version="1.0" encoding="utf-8"?> <edmx:edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" xmlns:gp="http://www.sap.com/protocols/sapdata/genericplayer" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:sap="http://www.sap.com/protocols/sapdata"> <edmx:dataservices m:dataserviceversion="2.0"> <Schema Namespace="RMTSAMPLEFLIGHT" xmlns="http://schemas.microsoft.com/ado/2008/09/edm"> <EntityType Name="Subscription" sap:content-version="1"> <Key> <PropertyRef Name="ID"/> </Key> <Property Name="ID" Type="Edm.String" Nullable="false" MaxLength="32" sap:label="sub. ID" <Property Name="user" Type="Edm.String" MaxLength="12" sap:label="user Name" sap:filterable="false" m:fc_targetpath="syndicationauthorname" m:fc_keepincontent="false"/> <Property Name="updated" Type="Edm.DateTime" Precision="19" sap:label="time Stamp" sap:filterable="false" m:fc_targetpath="syndicationupdated" m:fc_keepincontent="false"/> <Property Name="title" Type="Edm.String" MaxLength="255" sap:label="title" sap:creatable="true" sap:filterable="false" m:fc_targetpath="syndicationtitle" m:fc_keepincontent="false"/> <Property Name="deliveryAddress" Type="Edm.String" sap:label="delivery Address" sap:creatable="true" <Property Name="collection" Type="Edm.String" MaxLength="40" sap:label="collection" sap:creatable="true" <Property Name="filter" Type="Edm.String" sap:label="filter" sap:creatable="true" <Property Name="select" Type="Edm.String" MaxLength="255" sap:label="select" sap:creatable="true" </EntityType> <EntityType Name="Notification" sap:content-version="1"> <Key> <PropertyRef Name="ID"/> </Key> <Property Name="ID" Type="Edm.String" Nullable="false" MaxLength="32" sap:label="notification ID" <Property Name="collection" Type="Edm.String" MaxLength="40" sap:label="collection" <Property Name="title" Type="Edm.String" sap:label="notif. Text" <Property Name="updated" Type="Edm.DateTime" Precision="19" sap:label="time Stamp" </EntityType> <EntityType Name="Flight" sap:content-version="1"> <Key> <PropertyRef Name="carrid"/> <PropertyRef Name="connid"/> <PropertyRef Name="fldate"/> </Key> <Property Name="carrid" Type="Edm.String" Nullable="false" MaxLength="3" sap:label="airline" <Property Name="connid" Type="Edm.String" Nullable="false" MaxLength="4" sap:label="flight Number" <Property Name="fldate" Type="Edm.DateTime" Nullable="false" Precision="10" sap:label="date"
<Property Name="PRICE" Type="Edm.Decimal" Precision="20" Scale="2" sap:label="airfare" <Property Name="CURRENCY" Type="Edm.String" MaxLength="5" sap:label="airline Currency" <Property Name="PLANETYPE" Type="Edm.String" MaxLength="10" sap:label="plane Type" <Property Name="SEATSMAX" Type="Edm.Int32" sap:label="max. capacity econ." <Property Name="SEATSOCC" Type="Edm.Int32" sap:label="occupied econ." <Property Name="PAYMENTSUM" Type="Edm.Decimal" Precision="22" Scale="2" sap:label="total" <Property Name="SEATSMAX_B" Type="Edm.Int32" sap:label="max. capacity bus." <Property Name="SEATSOCC_B" Type="Edm.Int32" sap:label="occupied bus." <Property Name="SEATSMAX_F" Type="Edm.Int32" sap:label="max. capacity 1st" <Property Name="SEATSOCC_F" Type="Edm.Int32" sap:label="occupied 1st" <Property Name="flightDetails" Type="RMTSAMPLEFLIGHT.FlightDetails" Nullable="false" sap:label="flightdetails" sap:creatable="true" sap:updatable="true" <NavigationProperty Name="flightbooking" Relationship="RMTSAMPLEFLIGHT.BookingFlight" FromRole="FromRole_Flight_Booking" ToRole="ToRole_Booking_Flight"/> <NavigationProperty Name="flightBookings" Relationship="RMTSAMPLEFLIGHT.FlightBookings" FromRole="FromRole_Flight_Booking" ToRole="ToRole_Booking_Flight"/> </EntityType> <EntityType Name="Booking" sap:content-version="1" sap:semantics="flight_booking"> <Key> <PropertyRef Name="carrid"/> <PropertyRef Name="connid"/> <PropertyRef Name="fldate"/> <PropertyRef Name="bookid"/> </Key> <Property Name="carrid" Type="Edm.String" Nullable="false" MaxLength="3" sap:label="airline" <Property Name="connid" Type="Edm.String" Nullable="false" MaxLength="4" sap:label="flight Number" <Property Name="fldate" Type="Edm.DateTime" Nullable="false" Precision="10" sap:label="date" <Property Name="bookid" Type="Edm.String" Nullable="false" MaxLength="8" sap:label="booking number" <Property Name="CUSTOMID" Type="Edm.String" MaxLength="8" sap:label="customer Number" <Property Name="CUSTTYPE" Type="Edm.String" MaxLength="1" sap:label="b/p customer" <Property Name="SMOKER" Type="Edm.String" MaxLength="1" sap:label="smoker" <Property Name="WUNIT" Type="Edm.String" MaxLength="3" sap:label="unit of measure" <Property Name="LUGGWEIGHT" Type="Edm.Decimal" Precision="10" Scale="4" sap:label="luggage Weight" sap:filterable="false" sap:unit="wunit"/> <Property Name="INVOICE" Type="Edm.String" MaxLength="1" sap:label="invoice pty." <Property Name="CLASS" Type="Edm.String" MaxLength="1" sap:label="class" <Property Name="FORCURAM" Type="Edm.Decimal" Precision="20" Scale="2" sap:label="amount" <Property Name="FORCURKEY" Type="Edm.String" MaxLength="5" sap:label="paymnt currency" <Property Name="LOCCURAM" Type="Edm.Decimal" Precision="20" Scale="2" sap:label="amount"
<Property Name="LOCCURKEY" Type="Edm.String" MaxLength="5" sap:label="airline Currency" <Property Name="ORDER_DATE" Type="Edm.DateTime" Precision="10" sap:label="booking date" <Property Name="COUNTER" Type="Edm.String" MaxLength="8" sap:label="sales office" <Property Name="AGENCYNUM" Type="Edm.String" MaxLength="8" sap:label="agency No." <Property Name="CANCELLED" Type="Edm.String" MaxLength="1" sap:label="cancelation flag" <Property Name="RESERVED" Type="Edm.String" MaxLength="1" sap:label="reserved" <Property Name="PASSNAME" Type="Edm.String" MaxLength="25" sap:label="passenger Name" <Property Name="PASSFORM" Type="Edm.String" MaxLength="15" sap:label="title" <Property Name="PASSBIRTH" Type="Edm.DateTime" Nullable="true" Precision="10" sap:label="dob of Passeng." <NavigationProperty Name="bookedFlight" Relationship="RMTSAMPLEFLIGHT.BookingFlight" FromRole="ToRole_Booking_Flight" ToRole="FromRole_Flight_Booking"/> </EntityType> <EntityType Name="Carrier" sap:content-version="1"> <Key> <PropertyRef Name="carrid"/> </Key> <Property Name="carrid" Type="Edm.String" Nullable="false" MaxLength="3" sap:label="airline" <Property Name="CARRNAME" Type="Edm.String" MaxLength="20" sap:label="airline" <Property Name="CURRCODE" Type="Edm.String" MaxLength="5" sap:label="airline Currency" <Property Name="URL" Type="Edm.String" MaxLength="255" sap:label="url" <NavigationProperty Name="carrierFlights" Relationship="RMTSAMPLEFLIGHT.CarrierToFlight" FromRole="FromRole_Carrier_Flight" ToRole="ToRole_Flight_Carrier"/> </EntityType> <EntityType Name="Travelagency" m:hasstream="true" sap:content-version="1"> <Key> <PropertyRef Name="agencynum"/> </Key> <Property Name="agencynum" Type="Edm.String" Nullable="false" MaxLength="8" sap:label="agency No." <Property Name="NAME" Type="Edm.String" MaxLength="25" sap:label="travel agency name" <Property Name="STREET" Type="Edm.String" MaxLength="30" sap:label="street" <Property Name="POSTBOX" Type="Edm.String" MaxLength="10" sap:label="po Box" <Property Name="POSTCODE" Type="Edm.String" MaxLength="10" sap:label="postal Code" <Property Name="CITY" Type="Edm.String" MaxLength="25" sap:label="city" <Property Name="COUNTRY" Type="Edm.String" MaxLength="3" sap:label="country" <Property Name="REGION" Type="Edm.String" MaxLength="3" sap:label="region" <Property Name="TELEPHONE" Type="Edm.String" MaxLength="30" sap:label="tel." <Property Name="URL" Type="Edm.String" MaxLength="255" sap:label="travel agency URL" <Property Name="LANGU" Type="Edm.String" MaxLength="2" sap:label="language"
<Property Name="CURRENCY" Type="Edm.String" MaxLength="5" sap:label="trav.agency.curr" <Property Name="mimeType" Type="Edm.String" MaxLength="128" sap:label="mime Type" </EntityType> <ComplexType Name="FlightDetails"> <Property Name="countryFrom" Type="Edm.String" MaxLength="3" sap:label="country" <Property Name="cityFrom" Type="Edm.String" MaxLength="20" sap:label="depart.city" <Property Name="airportFrom" Type="Edm.String" MaxLength="3" sap:label="dep. airport" <Property Name="countryTo" Type="Edm.String" MaxLength="3" sap:label="country" <Property Name="cityTo" Type="Edm.String" MaxLength="20" sap:label="arrival city" <Property Name="airportTo" Type="Edm.String" MaxLength="3" sap:label="dest. airport" <Property Name="flightTime" Type="Edm.Int32" sap:label="flight time" <Property Name="departureTime" Type="Edm.Time" Precision="8" sap:label="departure" <Property Name="arrivalTime" Type="Edm.Time" Precision="8" sap:label="arrival Time" <Property Name="distance" Type="Edm.Decimal" Precision="11" Scale="4" sap:label="distance" <Property Name="distanceUnit" Type="Edm.String" MaxLength="3" sap:label="distance in" <Property Name="flightType" Type="Edm.String" MaxLength="1" sap:label="charter" <Property Name="period" Type="Edm.Byte" sap:label="n day(s) later" </ComplexType> <ComplexType Name="FlightAvailability"> <Property Name="ECONOMAX" Type="Edm.Int32" sap:label="max. capacity econ." <Property Name="ECONOFREE" Type="Edm.Int32" sap:label="free seats" <Property Name="BUSINMAX" Type="Edm.Int32" sap:label="max. capacity bus." <Property Name="BUSINFREE" Type="Edm.Int32" sap:label="free seats" <Property Name="FIRSTMAX" Type="Edm.Int32" sap:label="max. capacity 1st" <Property Name="FIRSTFREE" Type="Edm.Int32" sap:label="free seats" </ComplexType> <ComplexType Name="FlightAvailability2" BaseType="RMTSAMPLEFLIGHT.FlightAvailability"/> <Association Name="FlightBookings" sap:content-version="1"> <End Type="RMTSAMPLEFLIGHT.Flight" Multiplicity="1" Role="FromRole_Flight_Booking"/> <End Type="RMTSAMPLEFLIGHT.Booking" Multiplicity="*" Role="ToRole_Booking_Flight"/> </Association> <Association Name="BookingFlight" sap:content-version="1"> <End Type="RMTSAMPLEFLIGHT.Flight" Multiplicity="1" Role="FromRole_Flight_Booking"/> <End Type="RMTSAMPLEFLIGHT.Booking" Multiplicity="1" Role="ToRole_Booking_Flight"/> <ReferentialConstraint> <Principal Role="FromRole_Flight_Booking"> <PropertyRef Name="carrid"/> <PropertyRef Name="connid"/> <PropertyRef Name="fldate"/> </Principal> <Dependent Role="ToRole_Booking_Flight"> <PropertyRef Name="carrid"/>
<PropertyRef Name="connid"/> <PropertyRef Name="fldate"/> </Dependent> </ReferentialConstraint> </Association> <Association Name="CarrierToFlight" sap:content-version="1"> <End Type="RMTSAMPLEFLIGHT.Carrier" Multiplicity="1" Role="FromRole_Carrier_Flight"/> <End Type="RMTSAMPLEFLIGHT.Flight" Multiplicity="*" Role="ToRole_Flight_Carrier"/> </Association> <EntityContainer Name="RMTSAMPLEFLIGHT" m:isdefaultentitycontainer="true"> <EntitySet Name="SubscriptionCollection" EntityType="RMTSAMPLEFLIGHT.Subscription" sap:content-version="1"/> <EntitySet Name="NotificationCollection" EntityType="RMTSAMPLEFLIGHT.Notification" sap:content-version="1"/> <EntitySet Name="FlightCollection" EntityType="RMTSAMPLEFLIGHT.Flight" sap:contentversion="1"/> <EntitySet Name="BookingCollection" EntityType="RMTSAMPLEFLIGHT.Booking" sap:contentversion="1" sap:semantics="flight_booking"/> <EntitySet Name="CarrierCollection" EntityType="RMTSAMPLEFLIGHT.Carrier" sap:contentversion="1" sap:creatable="false" sap:updatable="false" sap:deletable="false"/> <EntitySet Name="TravelagencyCollection" EntityType="RMTSAMPLEFLIGHT.Travelagency" sap:content-version="1" sap:searchable="true"/> <EntitySet Name="TravelAgencies" EntityType="RMTSAMPLEFLIGHT.Travelagency" sap:contentversion="1" sap:searchable="true"/> <AssociationSet Name="AssocSet_FlightBookings" Association="RMTSAMPLEFLIGHT.FlightBookings" sap:content-version="1"> <End EntitySet="FlightCollection" Role="FromRole_Flight_Booking"/> <End EntitySet="BookingCollection" Role="ToRole_Booking_Flight"/> </AssociationSet> <AssociationSet Name="AssocSet_BookingFlight" Association="RMTSAMPLEFLIGHT.BookingFlight" sap:content-version="1"> <End EntitySet="FlightCollection" Role="FromRole_Flight_Booking"/> <End EntitySet="BookingCollection" Role="ToRole_Booking_Flight"/> </AssociationSet> <AssociationSet Name="AssocSet_CarrierToFlight" Association="RMTSAMPLEFLIGHT.CarrierToFlight" sap:content-version="1" sap:creatable="false" sap:updatable="false" sap:deletable="false"> <End EntitySet="CarrierCollection" Role="FromRole_Carrier_Flight"/> <End EntitySet="FlightCollection" Role="ToRole_Flight_Carrier"/> </AssociationSet> <FunctionImport Name="CheckFlightAvailability" ReturnType="RMTSAMPLEFLIGHT.FlightAvailability" m:httpmethod="get" sap:actionfor="rmtsampleflight.flight"> <Parameter Name="airlineid" Type="Edm.String" Mode="In"> <Documentation> <Summary/> <LongDescription>Airline</LongDescription> </Documentation> </Parameter> <Parameter Name="connectionid" Type="Edm.String" Mode="In"> <Documentation> <Summary/> <LongDescription>Flight Number</LongDescription> </Documentation> </Parameter> <Parameter Name="flightdate" Type="Edm.DateTime" Mode="In"> <Documentation> <Summary/> <LongDescription>Date</LongDescription> </Documentation> </Parameter> </FunctionImport>
<FunctionImport Name="GetAvailableFlights" ReturnType="Collection(RMTSAMPLEFLIGHT.Flight)" EntitySet="FlightCollection" m:httpmethod="post"> <Parameter Name="fromdate" Type="Edm.DateTime" Mode="In"> <Documentation> <Summary/> <LongDescription>Date</LongDescription> </Documentation> </Parameter> <Parameter Name="todate" Type="Edm.DateTime" Mode="In"> <Documentation> <Summary/> <LongDescription>Date</LongDescription> </Documentation> </Parameter> <Parameter Name="cityfrom" Type="Edm.String" Mode="In"> <Documentation> <Summary/> <LongDescription>Depart.city</LongDescription> </Documentation> </Parameter> <Parameter Name="cityto" Type="Edm.String" Mode="In"> <Documentation> <Summary/> <LongDescription>Arrival city</longdescription> </Documentation> </Parameter> </FunctionImport> </EntityContainer> </Schema> </edmx:dataservices> </edmx:edmx>
Appendix 2 Code Snippets Read - Get Carrier by Carrier ID <?php require_once "RMTSAMPLEFLIGHT.php"; // Connect to service: $proxy = new RMTSAMPLEFLIGHT ('http://<gateway_host>:<gateway_port>/sap/opu/sdata/iwfnd/rmtsampleflight'); $proxy->credential = new WindowsCredential('<Username>', '<Password>'); // Get carrier by carrier ID: try { $carrier_id = 'AA'; //American Airlines $query = $proxy->carriercollection()->filter("carrid eq '$carrier_id'"); $carriers = $query->execute()->result; echo "Carrier ID: ". $carriers[0]->carrid. "<br/>"; echo "Carrier Name: ". $carriers[0]->carrname. "<br/>"; echo "Carrier Code: ". $carriers[0]->currcode. "<br/>"; echo "Carrier URL: ". $carriers[0]->url. "<br/>"; } catch(dataservicerequestexception $exception) { echo $exception->response->geterror(); }?>
Function Import Get available Flights <?php require_once "RMTSAMPLEFLIGHT.php"; // Connect to service: $proxy = new RMTSAMPLEFLIGHT ('http://<gateway_host>:<gateway_port>/sap/opu/sdata/iwfnd/rmtsampleflight'); $proxy->credential = new WindowsCredential('<Username>', '<Password>'); // Get available flights (function import): try { $proxy->addheader('x-requested-with', 'XMLHttpRequest'); $city_from = 'NEW YORK'; $city_to = 'SAN FRANCISCO'; $from_date = '20110105'; $to_date = '20110728'; $flights = $proxy->execute("getavailableflights?cityfrom=$city_from&cityto=$city_to &fromdate=$from_date&todate=$to_date")->result; foreach ($flights as $flight) { echo "Flight Carrier: ". $flight->carrid. "<br/>"; echo "Flight Price: ". $flight->price. "<br/>"; echo "Flight Max. Seats: ". $flight->seatsmax. "<br/><br/>"; } } catch(dataservicerequestexception $exception) { echo $exception->response->geterror(); }?>
Create - Booking a Flight <?php require_once "RMTSAMPLEFLIGHT.php"; // Connect to service: $proxy = new RMTSAMPLEFLIGHT ('http://<gateway_host>:<gateway_port>/sap/opu/sdata/iwfnd/rmtsampleflight'); $proxy->credential = new WindowsCredential('<Username>', '<Password>'); // Create flight booking: //Show all errors, except for notices and coding standards warnings //(may result from odata library) error_reporting(e_all & ~E_NOTICE); try { //Create a booking object with key properties: $carrid = 'AA'; $connid = '0017'; $fldate = '2011-12-21T00:00:00'; $bookid = ''; $booking = Booking::CreateBooking($carrid, $connid, $fldate, $bookid); //Define additional booking properties: $booking->customid = '00004617'; $booking->custtype = 'P'; $booking->wunit = 'KG'; $booking->luggweight = '14.4000'; $booking->class = 'Y'; $booking->forcuram = '879.82'; $booking->forcurkey = 'USD'; $booking->loccuram = '803.58'; $booking->loccurkey = 'USD'; $booking->order_date = '2011-05-22T00:00:00'; $booking->counter = '00000000'; $booking->agencynum = '00000325'; $booking->passname = 'Joe Smith'; $booking->passform = '1234567'; $booking->passbirth = '1990-10-10T00:00:00'; //Add the booking object to the booking collection //(Note: The service is not aware of these changes until 'SaveChanges' method is //called) $proxy->addtobookingcollection($booking); //Define save changes method to send each change in a separate request: $proxy->setsavechangesoptions(savechangesoptions::none); //Add custom headers to the POST request: $proxy->setentityheaders($booking, array('x-requested-with' => 'XMLHttpRequest')); //Execute the reuest: $proxy->savechanges(); } catch(odataserviceexception $exception) { echo $exception->geterror(); }?>
Read - Get Carrier by Carrier ID Using X.509 Certificate Authentication <?php $curlreq = curl_init(); //Create a Get Specific Carrier query: $carrier_id = 'AA'; //American Airlines $query_url = "https://<gateway_host>:<gateway_ssl_port>/sap/opu/sdata/iwfnd/ RMTSAMPLEFLIGHT/CarrierCollection(carrid='$carrier_id')"; curl_setopt($curlreq, CURLOPT_URL, $query_url); //Set request general options: curl_setopt($curlreq, CURLOPT_RETURNTRANSFER, true); curl_setopt($curlreq, CURLOPT_FOLLOWLOCATION, true); //Set request options for server verification during SSL Handshake: curl_setopt($curlreq, CURLOPT_SSL_VERIFYHOST, 1); curl_setopt($curlreq, CURLOPT_SSL_VERIFYPEER, true); curl_setopt($curlreq, CURLOPT_CAINFO, '<Gateway_CA_Certificate_Path>.cer'); //Attach the stored X.509 Client Certificate to the request: curl_setopt($curlreq, CURLOPT_SSLCERT, '<Client_Certificate_Path>.pem'); //Send request: $curlres = curl_exec($curlreq); //Handle response: if(!curl_errno($curlreq)) { $info = curl_getinfo($curlreq); echo $curlres; } else { echo 'CURL error: '. curl_error($curlreq); } curl_close($curlreq);?>