简体   繁体   中英

Try To store webhook response into my database

I'm trying to store a webhook response into my database table but it stores on array object, not value.

<?php
const WEBHOOK_SECRET = 'Secre_key';
function verifySignature ($body, $signature) {
    $digest = hash_hmac('sha1', $rawPost, WEBHOOK_SECRET);
    return $signature !== $digest ;
}
if (!verifySignature(file_get_contents('php://input'), $_SERVER['HTTP_X_TAWK_SIGNATURE'])) {
    // verification failed
} else {
// verification success
$servername = "localhost";
$username = "name";
$password = "password";
$db = "twakdata";

// Create connection
$conn = new mysqli($servername, $username, $password, $db);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}
$json_string = file_get_contents('php://input');
$stringLen = strlen($json_string);
$array_data = json_decode($json_string, true);
    $sql = 'INSERT INTO twak (message,len) VALUES ("'.$array_data.'","'.strlen($json_string).'")';
if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

}
?>

And the output is :

id | Message | len
1 | Array | 0
2 | Array | 0
2 | Array | 0

but I want the array value instead of the array object in the message column. Can anyone please help me.

Looking at the tawk webhook documentation you should need to change $array_data in the below line

$sql = 'INSERT INTO twak (message,len) VALUES ("'.$array_data.'","'.strlen($json_string).'")';

to

$array_data['message']['text']

This uses the text property on the message object in the webhook data to get the message text.

$array_data['message']['text']

this holds : Name : unknown
Phone : 88776654322
Email : y@gmail.com
Question : Hello Question

these attributes, how can I grab phone number from this??

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM