Skip to content

Commit

Permalink
PHP Rest API
Browse files Browse the repository at this point in the history
  • Loading branch information
shahbaz17 committed Dec 6, 2020
0 parents commit 7987b13
Show file tree
Hide file tree
Showing 9 changed files with 503 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
MAGIC_PUBLISHABLE_KEY=
MAGIC_SECRET_KEY=

DB_HOST=localhost
DB_PORT=3306
DB_DATABASE=
DB_USERNAME=
DB_PASSWORD=
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/
.env
31 changes: 31 additions & 0 deletions api/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
require "../start.php";
use Src\Post;

header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: OPTIONS,GET,POST,PUT,DELETE");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");

$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$uri = explode( '/', $uri );

// all of our endpoints start with /post
// everything else results in a 404 Not Found
if ($uri[1] !== 'post') {
header("HTTP/1.1 404 Not Found");
exit();
}

// the user id is, of course, optional and must be a number:
$userId = null;
if (isset($uri[2])) {
$userId = (int) $uri[2];
}

$requestMethod = $_SERVER["REQUEST_METHOD"];

// pass the request method and user ID to the Post and process the HTTP request:
$controller = new Post($dbConnection, $requestMethod, $userId);
$controller->processRequest();
10 changes: 10 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"require": {
"vlucas/phpdotenv": "^2.4"
},
"autoload": {
"psr-4": {
"Src\\": "src/"
}
}
}
174 changes: 174 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions php-rest-api.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CREATE TABLE `posts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`body` text NOT NULL,
`author` varchar(255) NOT NULL,
`author_picture` varchar(255) NOT NULL,
`created_at` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`)
);

INSERT INTO `posts` (`title`, `body`, `author`, `author_picture`) VALUES
('What is Magic?', 'Magic is a developer SDK that you can integrate into your application to enable passwordless authentication using magic links - similar to Slack and Medium.', '[email protected]', 'https://www.gravatar.com/avatar/ac8b2ad0f9d1970ab22960ae04f35118.png'), ('Why Magic?', 'Magic leverages blockchain-based, standardized public-private key cryptography to achieve identity management. When a new user signs up to your service, a public-private key pair is generated for them. ', '[email protected]', 'https://www.gravatar.com/avatar/ac8b2ad0f9d1970ab22960ae04f35118.png');
31 changes: 31 additions & 0 deletions src/Database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
namespace Src;

class Database {

private $dbConnection = null;

public function __construct()
{
$host = getenv('DB_HOST');
$port = getenv('DB_PORT');
$db = getenv('DB_DATABASE');
$user = getenv('DB_USERNAME');
$pass = getenv('DB_PASSWORD');

try {
$this->dbConnection = new \PDO(
"mysql:host=$host;port=$port;dbname=$db",
$user,
$pass
);
} catch (\PDOException $e) {
exit($e->getMessage());
}
}

public function connet()
{
return $this->dbConnection;
}
}
Loading

0 comments on commit 7987b13

Please sign in to comment.