2

How to send webhook in Laravel 8

 1 year ago
source link: https://www.laravelcode.com/post/how-to-send-webhook-in-laravel-8
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to send webhook in Laravel 8

  61353 views

  1 year ago

Laravel

In this article, i will share with you how to send a webhook from your laravel application to any other application for example.

What is a webhook?

First, we need to know what is webhook? webhook is one type of notification that we want to send to any third-party application after done some tasks in our system. like, after payment our system wants to send POST data notifications to the merchant application or server, so they know the payment process is done.

In, another way webhook also use one backup system notification.

In this article, we will see how to send a webhook from laravel application with an example. if you never work with webhook then don't worry here I share with you a very simple example.

Please open 'https://webhook.site/' for testing your webhook here. it's the best side for webhook testing.

Step - 1 : Create Route

Route::get('send-test-webhook', [SendWebhookController::class, 'send'])->name('send-test-webhook');

Step - 2 : Create Controller

Now, we need to make one 'SendWebhookController.php' file in 'app/Http/Controllers' and simply write in this controller your send webhook logic and function.

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class SendWebhookController extends Controller
{
    public function send(Request $request)
    {
        $url = 'https://webhook.site/2d5153b6-5cb3-4c5a-8d05-7c680b17924d';
        $data = [
            'status_code' => 200, 
            'status' => 'success',
            'message' => 'webhook send successfully',
            'extra_data' => [
                'first_name' => 'Harsukh',
                'last_name' => 'Makwana',
            ],
        ];
    	$json_array = json_encode($data);
        $curl = curl_init();
        $headers = ['Content-Type: application/json'];

        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $json_array);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_HEADER, 1);
        curl_setopt($curl, CURLOPT_TIMEOUT, 30);

        $response = curl_exec($curl);
        $http_code = curl_getinfo($curl, CURLINFO_HTTP_CODE);

        curl_close($curl);

        if ($http_code >= 200 && $http_code < 300) {
            echo "webhook send successfully.";
        } else {
            echo "webhook failed.";
        }
    }
}
send-webhook-img.png

i hope you like this article.

Author : Harsukh Makwana
Harsukh Makwana

Hi, My name is Harsukh Makwana. i have been work with many programming language like php, python, javascript, node, react, anguler, etc.. since last 5 year. if you have any issue or want me hire then contact me on [email protected]


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK