Categories
PHP

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