Webhook PHP Implementation
$webhookUrl = 'https://example.com/webhook'; // Set your webhook URL
$payload = json_encode([
'event' => 'message.received',
'data' => [
'message' => 'Hello, World!',
'sender' => 'SenderName',
]
]);
$options = [
'http' => [
'header' => "Content-type: application/json\r\n",
'method' => 'POST',
'content' => $payload,
],
];
$context = stream_context_create($options);
$result = file_get_contents($webhookUrl, false, $context);
if ($result === FALSE) {
// Handle error
}
echo $result;
Webhook Node.js Implementation
const https = require('https');
const data = JSON.stringify({
event: 'message.received',
data: {
message: 'Hello, World!',
sender: 'SenderName',
}
});
const options = {
hostname: 'example.com',
port: 443,
path: '/webhook',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': data.length
}
};
const req = https.request(options, (res) => {
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
console.log(body);
});
});
req.on('error', (e) => {
console.error(e);
});
// Write data to request body
req.write(data);
req.end();