How to Create a Webhook in PHP

Sarwan Soomro Feb 02, 2024
  1. Create a Webhook in PHP
  2. Print the Webhook File in PHP
How to Create a Webhook in PHP

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

Create a Webhook in PHP

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:

Create Webhook PHP

How Does the Code Work

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.

Sarwan Soomro avatar Sarwan Soomro avatar

Sarwan Soomro is a freelance software engineer and an expert technical writer who loves writing and coding. He has 5 years of web development and 3 years of professional writing experience, and an MSs in computer science. In addition, he has numerous professional qualifications in the cloud, database, desktop, and online technologies. And has developed multi-technology programming guides for beginners and published many tech articles.

LinkedIn