<?php

header("Content-Type: application/json");

$API_KEY = "LUMINOUS_SECRET_KEY";

if ($_GET['key'] != $API_KEY) {
    echo json_encode(["status"=>"error","message"=>"Unauthorized"]);
    exit;
}

$data = json_decode(file_get_contents("php://input"), true);

$title = $data['title'];
$content = $data['content'];

/* create url slug */

$urlkey = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $title)));

$conn = new mysqli("localhost","ngiafon_blogapi","ApiBlogsngia12","ngiafon_blog");

if ($conn->connect_error) {
    die("Database connection failed");
}

/* INSERT INTO YOUR REAL BLOG TABLE */

$stmt = $conn->prepare(
"INSERT INTO p_articles (urlkey, cms_id, title, title2, content)
VALUES (?, 1, ?, '', ?)"
);

$stmt->bind_param("sss",$urlkey,$title,$content);

$stmt->execute();

echo json_encode([
"status"=>"success",
"message"=>"Article published to blog"
]);

?>
