What is a webhook? And how to connect it?
I really couldn't figure out what a webhook was for a while))
It turned out to be a completely simple technology, so no one really writes about it. Now I will describe how it works in conjunction with my database in PHP Mysql and in this case the site on tilde.
I think this applies to other constructors as well. I myself do not like constructors from the word AT ALL, because of the restrictions))), but due to circumstances in my work I have to work with constructors.
Question asked not only for me to answer it myself, but maybe there are more interesting solutions?
1 answers
There are two files on the server: connect.php and webhook.php
Connect.php
In the connect file, we specify the connection to the database.
<?php
header('Access-Control-Allow-Origin: *');
$server = "localhost"; /* имя хоста (уточняется у провайдера), если работаем на локальном сервере, то указываем localhost */
$username = "usernameBD"; /* Имя пользователя БД */
$password = "passwordBD"; /* Пароль пользователя, если у пользователя нет пароля то, оставляем пустым */
$database = "nameBD"; /* Имя базы данных, которую создали */
// Подключение к базе данный через MySQLi
$mysqli = new mysqli($server, $username, $password, $database);
//Для удобства, добавим здесь переменную, которая будет содержать название нашего сайта
$address_site = "https:// example.com ";
?>
Webhook.php Later, we will need to change it so that it sends data to the database.
Your site with the file: https://example.com/webhook.php (for the place of this domain "example.com" must be your domain) This link is a webhook, take this link and paste it on the site from where the data from the form is sent. First, we insert this test code to get a message about a successful test (for the place of my mail "[email protected]" we put our own)
<?php
header('Access-Control-Allow-Origin: *');
$headers = "From: [email protected]";
$message = print_r($_POST,true);
@mail('[email protected]', 'Tilda TEST', $message, $headers);
echo "ok";
?>
Webhook.php Modified webhook.
There are two actions in this script:
1) Sending an array of data by email.
2) Adding data to the database.
<?php
header('Access-Control-Allow-Origin: *');
$headers = "From: [email protected]";
$message = print_r($_POST,true);
@mail('[email protected]', 'Tilda TEST', $message, $headers);
echo "ok";
require_once("connect.php");
$result_query_insert = $mysqli->query("INSERT INTO `users` (full_name, login, email, password, avatar) VALUES ('".$_POST['full_name']."', '".$_POST['login']."', '".$_POST['email']."', '".$_POST['password']."', '".$_POST['avatar']."')");
$result_query_insert->close();
$mysqli->close();
?>
Creating a database one action!
We just type this command into the pcp-may-admin panel in the SQL section, and we do not need to manually enter all the fields and create users. sql:
CREATE TABLE `users` (
`id` int(10) NOT NULL,
`full_name` varchar(355) DEFAULT NULL,
`login` varchar(100) DEFAULT NULL,
`email` varchar(255) DEFAULT NULL,
`password` varchar(500) DEFAULT NULL,
`avatar` varchar(500) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;