Example #1
0
    }
    //create random code
    $code = sha1_file($url);
    $code = substr($code, 0, 5);
    // create new shortUrl object
    $shortUrl = new ShortUrlDTO();
    $shortUrl->setUrl($url);
    $shortUrl->setCode($code);
    $sql = "INSERT INTO short_url (`url`,`code`) VALUES (:url,:code)";
    try {
        $database->beginTransaction();
        $stmt = $database->prepare($sql);
        $stmt->bindValue(':url', $shortUrl->url, PDO::PARAM_STR);
        $stmt->bindValue(':code', $shortUrl->code, PDO::PARAM_STR);
        $stmt->execute();
        $newId = $database->lastInsertId();
        $database->commit();
        $shortUrl->setId($newId);
    } catch (Exception $e) {
        error_log("An error occured while binding data");
        error_log($e->getMessage());
        $database->rollBack();
        return false;
    }
    $response = array();
    $newShortUrl = 'http://ggd.ly/' . $shortUrl->code;
    return $newShortUrl;
    //    if (!is_null($shortUrl->id)){
    //       echo '<div class="copyUrl">' . $newShortUrl . '</div>';
    //    }
}