Libvirt Functions
PHP Manual


(php-libvirt 0.1)

libvirt_connectGet a connection to the Hypervisor


resource libvirt_connect ([ string $url= NULL [, bool $readonly= true [, array $credentials= Array() ]]] )

This function should be called first to get a connection to the Hypervisor. If necessary, authentication will be performed using supplied credentials.



URL of the hypervisor to connect to. Can be for example qemu:///system or qemu+tcp:///system .


If TRUE (default) connection is made readonly.


Credentials to authenticate with. See authentication for more details.

Return Values

Returns FALSE on failure and connection resource on success. This connection resource must be used for all subsequent calls.


This function returns errors via PHP E_* error mechanism.


Note: Libvirt version
Version prior to 0.6.2 are not supported and using phplibvirt with libvirt prior to 0.6.2 will probably cause problems. libvirt_connect() will refuse to connect to libvirt daemon if the version is not at least 0.6.2.

Note: Authentication
You can authenticate to the libvirt daemon in several ways. If you are using policy kit and you are connecting locally, you can set it up to allow nonroot users. Just add to /etc/PolicyKit/PolicyKit.conf:

<match action="org.libvirt.unix.manage">
    <match user="httpduser">
      <return result="yes"/>

If you are connecting to the TCP socket, you need to provide credentials. These credentials must be set beforehand using SASL. See » for more details. You can You can specify the creentials as third argument. It is supposed to be an array in form of credential_type=>credential value. In example:



Example #1 libvirt_connect() example

This example connects to the hypervisor in various ways.

//Anonymous readonly connection
print_r($res1); printf ("\n");

//Anonymous read-write connection
print_r($res2); printf ("\n");

//Read-write connection with authentication
print_r($res3); printf ("\n");

The above example will output something similar to:

Resource id #4
Resource id #5
Resource id #6

Libvirt Functions
PHP Manual