Ejemplo n.º 1
0
<?php

$jsonData = $_POST['jsonData'];
function storeJson($data, $fileName = 'json-file')
{
    if (empty($data)) {
        return false;
    }
    $fileName = time() . '-' . $fileName . '.json';
    $filePath = 'temp/' . $fileName;
    $handle = fopen($filePath, 'w');
    fwrite($handle, json_encode($data));
    fclose($handle);
    if (file_exists($filePath)) {
        return $filePath;
    } else {
        return false;
    }
}
$path = storeJson($jsonData);
if ($path !== false) {
    echo json_encode(['filePath' => $path]);
} else {
    echo json_encode(['filePath' => '']);
}
function findAnnounces($strDataDom)
{
    //for each page, there are up to 10 records, each of them marked with DOM class 'plus'
    foreach ($strDataDom->find('.plus') as $data) {
        $value = $data->href;
        //is it a valid URL for an announce?
        if (strrpos($value, "www.athome.lu/")) {
            //go into the announce
            $htmlContent = scraperWiki::scrape($value);
            //look for the start of the json record wich has all the information of the announce
            //and manually trim it to the correct json format
            $strStart = strpos($htmlContent, "initGoogleMap");
            $strEnd = strpos($htmlContent, "#containerGoogleMap");
            $strData = substr($htmlContent, $strStart, $strEnd - $strStart - 6);
            $strData = ltrim($strData, "initGoogleMap([");
            //is it UTF format? just in case we convert it
            $strDataUTF = iconv('UTF-8', 'ASCII//TRANSLIT', $strData);
            //the function will transfor the string into a json object and store it in the database
            storeJson($strDataUTF);
        }
    }
}