123456789101112131415161718192021222324252627282930313233343536 |
- <?php
- include dirname(__FILE__)."/config.php";
- include dirname(__FILE__)."/ShortUrl.php";
- try {
- $pdo = new PDO(DB_PDODRIVER . ":host=" . DB_HOST .
- ";dbname=" . DB_DATABASE,
- DB_USERNAME, DB_PASSWORD);
- }
- catch (PDOException $e) {
- trigger_error("Error: Failed to establish connection to database.");
- exit;
- }
-
- $shortUrl = new ShortUrl($pdo);
- try {
- $code = $shortUrl->urlToShortCode($_POST["url"]);
- $http = isset($_SERVER['HTTPS'])?'https':'http';
- // printf('<p><strong>Short URL:</strong> <a href="%s/%2$s">%2$s</a></p>',$http.'://'.$_SERVER['HTTP_HOST'], SHORTURL_PREFIX . $code);
- $arr = array(
- 'status' => 1,
- 'url' => $http.'://'.$_SERVER['HTTP_HOST']."/".$code,
- );
- echo json_encode($arr);
- exit;
- }
- catch (Exception $e) {
- // log exception and then redirect to error page.
- // header("Location: index.php");
- $arr = array(
- 'status' => 0,
- 'msg' => "地址有误",
- );
- echo json_encode($arr);
- exit;
- }
|