Example #1
0
        $db->insertIPLocation($url, $out);
        $result = $db->getCachedURL($url);
    }
    echo json_encode($result);
});
/**
 * Post-request to /api/traceroute/ performs a traceroute-command to the given url.
 *
 * The traceroute command output will be saved to a file.
 *
 * @return {int}  Returns id of the traceroute request.
 */
$app->post('/api/traceroute/', function () use($app) {
    $url = $app->request->post('url');
    $db = new Database();
    $insertID = $db->insertURL($url);
    // Define parameters and execute traceroute command
    $cmd = 'traceroute -I ' . $url;
    $outputfile = 'traceroutes/' . $insertID . '.txt';
    $pidfile = 'traceroutes/' . $insertID . '.pid';
    exec(sprintf("%s > %s 2>&1 & echo \$! >> %s", $cmd, $outputfile, $pidfile));
    $out = array();
    $out['id'] = $insertID;
    echo json_encode($out);
});
/**
 * Get-request to /api/traceroute/:id gets the traceroute-data for a given id
 * from the file or DB.
 *
 * @param  {int}  $id   The id to get the location for.
 * @return {json}       Returns the traceroute-data in a json-object.