libvirt_connect Get a connection to the Hypervisor
&reftitle.description; 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.

&reftitle.parameters;

url

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

readonly

  If &true; (default) connection is made readonly.

credentials

  Credentials to authenticate with. See authentication for more details.

&reftitle.returnvalues;

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

&reftitle.errors;

This function returns errors via PHP E_* error mechanism.

&reftitle.notes;

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.

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"/>
        </match>
    </match>

If you are connecting to the TCP socket, you need to provide credentials. These credentials must be set beforehand using SASL. See http://libvirt.org/auth.html#ACL_server_username 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:

      Array(VIR_CRED_AUTHNAME=>"fred",VIR_CRED_PASSPHRASE=>"fred");

&reftitle.examples;

`libvirt_connect` example

This example connects to the hypervisor in various ways.

    <?php
    //Anonymous readonly connection
    $res1=libvirt_connect("qemu:///system");
    print_r($res1); printf ("\n");

    //Anonymous read-write connection
    $res2=libvirt_connect("qemu:///system",false);
    print_r($res2); printf ("\n");

    //Read-write connection with authentication
    $res3=libvirt_connect("qemu:///system",false,Array(VIR_CRED_AUTHNAME=>"fred",VIR_CRED_PASSPHRASE=>"fred"));
    print_r($res3); printf ("\n");
    ?>

&example.outputs.similar;

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