# \[WEBHOOK] Create a Webhook in PHP (ok)

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

```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
<?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

```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 インターネットへの接続](https://humix.com/redirect?url=https%3A%2F%2Fn-study.com%2Fhumix%2Fvideo%2Ff5c1754c69d88b6411d582bdf1cdcc3bc609ce48ff124dd8f867fc757e7cd8fe)

### [Create a Webhook in PHP](https://www.delftstack.com/howto/php/create-webhook-in-php/#create-a-webhook-in-php) <a href="#create-a-webhook-in-php" id="create-a-webhook-in-php"></a>

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:

```php
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:

```html
<!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
<?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](https://www.delftstack.com/img/PHP/create%20webhook%20php.gif)

#### [How Does the Code Work](https://www.delftstack.com/howto/php/create-webhook-in-php/#how-does-the-code-work) <a href="#how-does-the-code-work" id="how-does-the-code-work"></a>

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.

### [Print the Webhook File in PHP](https://www.delftstack.com/howto/php/create-webhook-in-php/#print-the-webhook-file-in-php) <a href="#print-the-webhook-file-in-php" id="print-the-webhook-file-in-php"></a>

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

```php
<?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.
