raw snippet

Google Earth uses a XML based file format for data exports, named with KML extension, which can be created easily with PHP. However, when icons and other data must be packed together to a KMZ file, the following snippet can create the ZIP-File, with KMZ extension using the PHP zip extension:

<?php
// Copyright Robert Eisele 2017
$kml = <<<KML
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
  <Document>
    <name>XARG.org Snippet</name>
    <Style id="msn">
        <IconStyle>
            <Icon>
                <href>files/bla.png</href>
            </Icon>
        </IconStyle>
    </Style>
    <Placemark>
        <name>This is a name</name>
            <description>This is a description.</description>
        <styleUrl>#msn</styleUrl>
        <Point>
            <coordinates>37.545734,14.159431,0</coordinates>
        </Point>
    </Placemark>
  </Document>
</kml>
KML;

$zip = new ZipArchive();
if ($zip->open('GoogleEarth.kmz', ZIPARCHIVE::CREATE)) {
    $zip->addEmptyDir('files');

    foreach (glob('icons/*') as $file) {
        $zip->addFile($file, 'files/' . basename($file));
    }
    $zip->addFromString('doc.kml', $kml);
    $zip->close();
}