[WEBHOOK] Create a Webhook in PHP (ok)

https://www.delftstack.com/howto/php/create-webhook-in-php/

C:\xampp824\htdocs\phongkhamnet\index.php

<!DOCTYPE html>
<body>
  <form action="code.php" method="post" align="center">
    <input type="submit" value="How do I create a webhook?" name="jsonfile" />
  </form>
</body>
</html>

C:\xampp824\htdocs\phongkhamnet\code.php

<?php
if (isset($_POST['jsonfile'])) {
  $get = file_get_contents('example.json');
  // example.json is for the demo, it can be any of your source data
  $dump = print_r($get, true);
  $create_webhookfile = file_put_contents('webhook.log', $dump);
}

C:\xampp824\htdocs\phongkhamnet\example.json

{
  "userId": 1,
  "id": 1,
  "title": "delectus aut autem",
  "completed": false
}

Webhooks are handled with JSON and XML file formats and usually contain textual data. Users can use PHP functions to deal with these files.

[手を動かしながら学ぶIP�...PauseUnmuteLoaded: 6.12%Remaining Time -26:33Fullscreen[手を動かしながら学ぶIPネットワーク] #6 インターネットへの接続

Webhooks are usually in JSON/TEXT/XML file format. You would want these files to control with a PHP script.

You should make note of the following functions first:

json_decode(file_get_contents("YOUR FILE PATH"), TRUE))

We have used json_decode() and passed two parameters inside it, a PHP function file_get_contents() and a boolean value. Although it depends on what users want to do with these files, we will show you how to deal with them in PHP.

You can create, decode or encode these files or later store them in the database.

HTML code:

<!DOCTYPE html>
<body>
<form action="code.php" method="post" align="center">
<input type="submit" value="How do I create a webhook?" name="jsonfile" />
</form>
</body>
</html>

PHP code:

<?php
 if(isset($_POST['jsonfile'])){
$get = file_get_contents('example.json'); 
// example.json is for the demo, it can be any of your source data
$dump = print_r( $get, true );
$create_webhookfile = file_put_contents( 'webhook.log', $dump );
}
?>

Output:

We got our JSON demo file with the file_get_contents() function and then we stored it in $get variable.

We used the print_r() function that takes two parameters. In this case, it is $get and boolean TRUE.

We used the file_put_contents() function to dump the array results and a webhook.log file string.

This function also takes a minimum of two parameters.

After the webhook file is created, you can easily print it with the following PHP script:

<?php
if($webhook = json_decode(file_get_contents("webhook.log"), true)){
$response = $webhook; 
 }
   echo "<pre>";
   print_r($response);
   echo "</pre>";
?>

Output:

Array
(
    [Name] => ANY NAME
    [Surname] => ANY SURNAME
    [Sex] => male
    [Age] => 30
    [Location] => Array
        (
            [State] => Any State
            [City] => Any City
            [Street] => Any Street
            [Postal Code] => 278332766
        )
    [Contact] => Array
        (
            [0] => Array
                (
                    [type] => Office
                    [Number] => 286326832636
                )
        )
)

We have demonstrated how to create a webhook file using JSON data.

The data source you will be using can be online or any XML, text, and JSON file.

The above PHP code will be sufficient to create your webhook files while printing them or storing them in the SQL database, depending on your particular requirements.

Last updated