PHP and Sabre

What is Sabre?

Sabre is a company that has data of all flight schedules, etc. And, using their system, you can book flights, hotels, rent cars, etc.

Getting PHP to talk with Sabre

I tried PHP Soap and nuSoap. They both didn’t work for me because of their limitations. So I ended up writing a small script of my own that talks with Sabre for me after I realized that the headers being sent by all the SOAP libraries I tried, were not compatible with Sabre’s web services.

Sabre checks the “Content-Type” header in your request. If that is not “text/xml”, you will always get an error, even if your xml is correct.

Here’s a little piece of code that should help with the transport part.  I will leave the generation of XML to you.

/**
 * Sabre class (for connecting to Sabre web services)
 * @author Moazzam Khan 
 * @version $Id$
 */
class Sabre {
	public $host;
	public $port;
	public $timeout;

	public function Sabre()
	{
		$this->timeout = 30;
	}
		
	public function makeRequest($body)
	{
		$header = "POST /websvc HTTP/1.1\r\n"
					. "Host: {$this->host}:{$this->port}
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Content-Type: text/xml
Content-Length: ".strlen($body)."
Pragma: no-cache
Cache-Control: no-cache";
		
		$fp = fsockopen("ssl://".$this->host, $this->port, $errno, $errstr, $this->timeout);
		if(!$fp){
			print "SOCKET ERROR $errstr ($errno)\n";
		} else {
				//send the request
				fputs($fp, $header."\r\n\r\n".$body, strlen($header."\r\n\r\n".$body));
				stream_set_timeout($fp, 30);
				stream_set_blocking ($fp, 1);
				while (!feof($fp)) {
					$line = fread($fp, 2048);
					if (trim(strlen($line)) < 10) continue;
					$output .= $line;
				}
				fclose($fp);
		}
		$ret = explode("\r\n\r\n", $output);
		$ret = explode("\r\n", $ret[1]);
		for ($i=0; $i

Posted

in

by

Comments

32 responses to “PHP and Sabre”

  1. Merijn Avatar
    Merijn

    Hi Maozzam,

    It read your article with great interest since I am trying to make a connection with PHP to Sabre (it seems they think that only .net and java exist). I got some errors, in your headers it says {$this->;port} which, I think, should be {$this->port} and further down there is a for loop ($i=0; $i< 5) which should be ($i=0; $i< 5;$i++). Although I get no PHP warnings or errors about the code anymore, the connection does not seem to work, I get this Warning:

    Warning: fsockopen(): php_network_getaddresses: getaddrinfo failed: Name or service not known in be_sabre.php on line 111

    Warning: fsockopen(): unable to connect to ssl://cert.webservices.sabre.com/tsts:443 (php_network_getaddresses: getaddrinfo failed: Name or service not known) in be_sabre.php on line 111
    SOCKET ERROR php_network_getaddresses: getaddrinfo failed: Name or service not known (0)

    Do you have any idea what goes wrong?

  2. Moazzam Avatar

    Hi Merijn,

    Sorry about the error in the code. It got truncated somehow. I corrected it so it should work now.

    From the error you pasted, it looks like you are trying to connect to cert.webservices.sabre.com/tsts. I don’t think that’s a valid page/service on Sabre’s server. Just leave the host as cert.webservices.sabre.com.

  3. Guilherme Avatar
    Guilherme

    Hi Maozzam,

    To Open a Session and consume some service ? how did you do that ?

    Regards,

    Guilherme

  4. Moazzam Avatar

    Hi Guilherme,

    My name is mOAzzam not mOAzzam 🙂

    It depends on how you want to implement it but generally speaking you would connect to SessionCreateRQ service and provide it your username and password to login. It will send you back a binary security token. Save that token somewhere because you will need it in all requests you make.

    Then, connect to (let’s say) AirAvailRQ service to get a list of all available flights for a specific date and time. For this request, you will provide the binary security token you saved earlier instead of providing your username and password.

  5. Guilherme Avatar
    Guilherme

    Hi,

    I need a JOB from you.

    Need to open a PNR by using the method OTA_TravelItineraryReadLLSRQ

    I’m a travel agency, i have Sabre WebServices credencials, and i do have a little project in C#.Net that do execute this method… but i want that in PHP.

    If you are interested, please, contact me:

    guilherme.viana #at# advantageit #dot# com #dot# br

    or msn: guilherme {at} suaviagem {dot} com

    I’m waitting.

    Guilherme

  6. Guilherme Avatar
    Guilherme

    Sorry, my msn is: suporte &at& suaviagem &dot& com

  7. Fahad Avatar

    hey hi Moazzam
    iam not sure in what Format $body will assigned…
    can you please tell me the format for $body variable I don;t have any idea how it will work
    and iam facing this problem

    Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://cert.webservices.sabre.com:443 (Unable to find the socket transport “ssl” – did you forget to enable it when you configured PHP?) in D:\wamp\www\sabre\index.php on line 28
    SOCKET ERROR Unable to find the socket transport “ssl” – did you forget to enable it when you configured PHP? (24)
    what this error about pls explain and must tell me where i gonna put username and password

  8. Moazzam Avatar

    Hi Fahad,

    The format for $body is XML. Each service has its own requirements in terms of what data it expects.
    You can find more information from the developer website for Sabre’s web services. It is located at: http://webservices.sabre.com/

    You need to have SSL support enabled in PHP for it all to work. You may want to look into that. Unfortunately, I don’t know that much about settings things up on a Windows box. If it doesn’t work and you need some affordable webhosting to develop your stuff, check out :
    http://moazzam-khan.com/blog/?page_id=505

  9. Jeremy Cook Avatar

    Hi Moazzam,

    I read your post with interest as I’m just doing some research into integrating with Sabre for a client in PHP. Are you saying that the PHP SOAP extension does not send SOAP requests with a content-type of text/xml? I just tried using PHP 5.3.3 with a different SOAP service and the content-type was set to text-xml. Maybe this is a problem on earlier versions of PHP?

    There may be another solution though to the problem. The SoapClient class allows you to pass a stream context as one of the array of config options to the constructor. If you use stream_context_create() to create an http stream context, setting the content-type header in the stream_context to text/xml (or whatever else you need) would that sort out the problem? This should force the SoapClient object to use the content-type header that you set in all requests. I haven’t been able to test this yet with Sabre but it looks like it could be a solution.

    HTH

  10. Moazzam Avatar

    Hi Jeremy,

    When I wrote that post, PHP 5.3.3 wasn’t out. I haven’t played with php 5.3, yet but if it is possible to pass in a custom Content-type then yeah it should work.

    PHP Soap clients generally work with SOAP services but it’s different with Sabre. If the content type is not set to text/xml (exactly that text with no variations) then their web service throws an error.

    Maybe someone (anyone who read the post and the comments) who has tried this can also shed some light on it.

  11. Aron Avatar
    Aron

    Hi Moazzam,

    Is it possible that you add some examples for $body to get started? Im currently developping the same sabre webservice and a simple example of SessionCreateRQ would help me alot…

  12. Aron Avatar
    Aron

    For anyone who is interested, it is working with SOAP under PHP (5.2.4)
    Problem is meanly because SOAPHeader need to be set correct.

  13. Moazzam Avatar

    Hi Aron,

    I am glad you were able to resolve your problem. Also, it should work with PHP 4 too.

  14. Mike Avatar
    Mike

    Hi Aron
    Did you manage to get this working with a SOAP implementation or with Moazzam’s script? Can you give us some pointers as to how you got it to work?
    Thanks
    Mark

  15. nstefan Avatar
    nstefan

    Hi Moazzam,

    Would you mind posting an example of SessionCreateRQ with your class please? I would really appreciate it since i have no clue on how to get started.

    thank you very much in advance!

  16. VeryLose Guy Avatar

    Hey men!!

    sss is there a way you can guide us to do the SessionCreateRQ? with you class

  17. peer Avatar
    peer

    curl fine works

  18. Jin Avatar
    Jin

    i am interesting about sabre web service
    did u already develop sabre web services?

  19. Satish Patil Avatar
    Satish Patil

    I also tried your code with custom xml with ipcc, username, password. But still getting error as “soap-env:Client.InvalidEbXmlMessageUnable to create envelope from given source: Error on line 1 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.javax.xml.soap.SOAPException: Unable to create envelope from given source: Error on line 1 of document : Content is not allowed in prolog. Nested exception: Content is not allowed in prolog.”

    Can you please suggest me whether I am on right direction or anything wrong?

    Please provide the sample xml compatible with your script.

  20. Moazzam Avatar

    Hi Satish,

    Paste the request you are sending on pastie.org and send me the link so I can take a look at it. Are you aware of the request parameters required by Sabre? If not, you should contact their support and ask for them.

  21. morrison23 Avatar
    morrison23

    Moazzam –

    Do you do any SABRE PHP consulting?

  22. Scott Avatar
    Scott

    So i have been following this thread on and off for some time now…
    is anyone having success getting SWS to work with PHP?

  23. Moazzam Avatar

    Hi Scott,

    Are you having trouble with Sabre? I wrote another post that gives you an overview of the workflow of Sabre. Maybe it will help you out.

    http://moazzam-khan.com/blog/?p=637

  24. Scott Avatar
    Scott

    Hi MOazzam,
    We currently have a .net solution, but we cannot support he code onsite which makes updates etc and upgrades very timely and costly…When we frist started this project it didnt seem like there was a way to use PHP to connect to the sabre web services.
    I read your workflow and it is similar to what we have.
    We are currently using Sabre to book hotels only, but are thinking of adding flight functionality.
    Have you been successful in connecting with PHP?

  25. Jane Avatar
    Jane

    hi Moazzam.

    Could you please give an example of this implementation. Im not sure if what Im trying is right.

    Thanks & Regards,
    Jane

  26. Andres Avatar
    Andres

    hi Moazzam.
    Could you please give an full example how to make the request for hotels list

    thanks

  27. Dmitriy Avatar
    Dmitriy

    Hi MOazzam,

    Do you have an example php of finding a flights?

    thanks

  28. Alfred Ayache Avatar

    Hi Moazzam:

    Thank you for posting this; I don’t think I would have stumbled across the fsockopen, or the ssl:// scheme, on my own. If you can point me at a book or article which explains how to figure this stuff out, I’d be grateful.

    I do have a question about why you’re tossing away any chunk less than 10 characters long?

    Thanks,
    – AAA

  29. Alfred Ayache Avatar

    Hi again. Just to let you know I wrote my own post about accessing Sabre’s Web Services using PHP. I used your class as the basis, with some minor tweaks, and of course, gave you full credit, with a link back here. I’m interested to hear what you think.

  30. fperich Avatar
    fperich

    Hi Moazzam, we found a way to connect to Sabre with file_get_contents

    class Sabre {
    public $timeout;

    public function Sabre()
    {
    $this->timeout = 30;
    }

    public function makeRequest($body)
    {
    $header = [“Content-Type: text/xml”,
    “Content-Length: “.strlen($body),
    “Pragma: no-cache”,
    “Cache-Control: no-cache”];

    $opts = array(‘http’ =>
    array(
    ‘method’ => ‘POST’,
    ‘header’ => $header,
    ‘content’ => $body
    )
    );

    $context = stream_context_create($opts);

    $result = file_get_contents(‘https://webservices.sabre.com/websvc’, false, $context);

    return $result;
    }
    }

    and thanks for your code

  31. fperich Avatar
    fperich

    another thing, Sabre made a change in the infrastructure to connect their web service, so they will recommend to change the Connection: keep-alive to Connection: close, and to remove the Keep-Alive: 300. Because those things actually seem to conflict the while running as it is expecting an end of file: while (!feof($fp)).

  32. sana Avatar
    sana

    Hi MOazzam,
    i am new to sabre web-service , can u please guide me how can i consume sabre web-services and use its proxy classes in java code. just tell me how can SessionCreateRQ work with java?? i found your tutorial helpful but unfortunately its for php users..
    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *