<?php
$hostname = 'localhost';
$user = '<yours>';             // Your XF database username
$pass = '<yours>';               // Your XF database password
$database = '<yours>';         // Your XF database name
$db_connection = new PDO( "mysql:host=" . $hostname . ";dbname=" . $database, $user, $pass );
$query = "SELECT ld_id, url FROM phc_linkdirectory";
$results = $db_connection->query($query);
foreach ( $results as $row ) {
        $ogurl = null;
        if (!empty($row['url'])) {
                $doc = new DomDocument();
                libxml_use_internal_errors(true);
                $doc->loadHTML(file_get_contents($row['url']));
                $xpath = new DOMXPath($doc);
                $metas = $xpath->query("//*/meta[starts-with(@property, \'og:\')]");
                foreach ($metas as $meta) {
                        if ($meta->getAttribute('property') == 'og:image') {
                                $ogurl = $meta->getAttribute('content');
                        }
                }
                if (!empty($ogurl)) {
                        $path = floor($row['ld_id'] / 1000);
                        $destfile = $row['ld_id'] . ".jpg";
                        $ch = curl_init($ogurl);
                        $fp = fopen($path . $destfile, 'wb');
                        curl_setopt($ch, CURLOPT_FILE, $fp);
                        curl_setopt($ch, CURLOPT_HEADER, 0);
                        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
                        $ogresult = curl_exec($ch);
                        curl_close($ch);
                        fclose($fp);
                        echo $ogurl . " | " . $path . " | " . $destfile . "\n";
                }
        }
}
$db_connection = null;
?>