PHP developers beware: Zorba now has a language binding for PHP 5!
Most PHP developments have to deal with XML and since PHP version 5, the support for XML has been greatly improved and developers can use various extensions that speak XML: SimpleXML, SAX, DOM, XPath and XSLT.
We strongly believe that XQuery was the missing piece in this set of tools and therefore we are very excited by this release.

How to get it ?

First you need Zorba (library and development headers) installed on your computer. Please see Zorba’s documentation for build/installation instructions. For Debian/Ubuntu users, there are binaries available.
You can access Zorba PHP Extension through PECL website.
Once you downloaded the package, you need the following options to build and install Zorba API for PHP.

$ phpize
$ ./configure –with-zorba_api=[include directory of zorba]
$ make
$ make install

Windows users: we’ll shortly provide the binary for this extension.

How to use it ?

This extension provides natively functional API but also Object-Oriented API with the zorba_api.php file.
The following example shows how to execute an XQuery Hello World in PHP userspace. First you need to include the zorba_api.php, this file will load zorba extension and declare all the classes you need.

//Include for the Object-Oriented API
require ‘zorba_api.php’;

Before executing a query, you need to initialize and shutdown the engine. Be careful, you these steps are really important in oder to avoid memory errors.
For initialization:

//Initialization of Zorba store
$store = InMemoryStore::getInstance();
//Initialization of Zorba
$zorba = Zorba::getInstance($store);

For shutdown:

//Shutdown of Zorba
$zorba->shutdown();
//Shutdown of Zorba store
InMemoryStore::shutdown($store);


Now let’s consider this simple query:

$xquery = <<< EOT
let $message := ‘Hello World!’
return
<results>
<message>{$message}</message>
</results>
EOT;


You can use Zorba to compile and run this query:

//Compile the query
$lQuery = $zorba->compileQuery($xquery);
//Run the query…
echo $lQuery->execute();
//…and destroy it
$lQuery->destroy();

The pieces put all together:

//Include for the Object-Oriented API
require ‘zorba_api.php’;

//Initialization of Zorba store
$store = InMemoryStore::getInstance();
//Initialization of Zorba
$zorba = Zorba::getInstance($store);

$xquery = <<< EOT
let $message := ‘Hello World!’
return
<results>
<message>{$message}</message>
</results>
EOT;

//Compile the query
$lQuery = $zorba->compileQuery($xquery);
//Run the query…
echo $lQuery->execute();

//…and destroy it
$lQuery->destroy();


//Shutdown of Zorba
$zorba->shutdown();
//Shutdown of Zorba store
InMemoryStore::shutdown($store);

XQuery updates are also availables:

$lDataManager = $zorba->getXmlDataManager();
$lDataManager->loadDocument(”books.xml”, “<books><book>Book 1</book><book>Book 2</book></books>”);
$lQuery1 = $aZorba->compileQuery(”insert node <book>Book 3</book> into doc(’books.xml’)/books”);
if($lQuery1->isUpdateQuery())
{
$lQuery1->applyUpdates();
}
$lQuery2 = $aZorba->compileQuery(”doc(’books.xml’)//book”);
echo $lQuery2->execute();
$lQuery1->destroy();
$lQuery2->destroy();

More examples are available on zorba svn repository.We hope you enjoy the extension!