Пример #1
0
 /**
  * Test add()
  *
  * @return void
  */
 public function testAdd()
 {
     foreach ($this->test_urls as $url => $result) {
         try {
             $info = $this->short_url->add($url);
             $this->assertEquals($url, $info["url"]);
             $this->assertEquals(5, strlen($info["id"]));
             $this->assertTrue($result);
         } catch (Exception $e) {
             $this->assertInstanceOf(ShortURLException::class, $e);
             $this->assertFalse($result);
         }
     }
 }
Пример #2
0
    if (config("https") && !is_https()) {
        throw new ShortURLException("HTTPSで通信してください。");
    }
    if ($_SERVER["REQUEST_METHOD"] !== "POST") {
        throw new ShortURLException("POSTで通信してください。");
    }
    if (empty($url) && !is_string($url)) {
        throw new ShortURLException("URLを送信してください。");
    }
    if (!is_url($url)) {
        throw new ShortURLException("URLはhttp://もしくはhttps://から入力してください。");
    }
    if (!is_active_url($url)) {
        throw new ShortURLException("有効なURLを送信して下さい。");
    }
    $ShortURL = new ShortURL(config("db"));
    $result = $ShortURL->add($url);
    $array = ["status" => "ok", "hash" => $result["id"], "url" => config("url") . "/" . $result["id"]];
} catch (PDOException $e) {
    header("HTTP/1.1 500 Internal Server Error");
    $message = config("debug") ? $e->getMessage() : "データベースエラーでエラーが発生しました。";
    $array = ["error" => ["message" => $message]];
} catch (RuntimeException $e) {
    header("HTTP/1.1 500 Internal Server Error");
    $message = $e->getMessage();
    $array = ["error" => ["message" => $message]];
}
if (isset($array) && is_array($array)) {
    header("Content-Type: application/json; charset=utf-8", true);
    echo json_encode($array, JSON_UNESCAPED_UNICODE);
}