php 从soho表单到mysql的Webhook不提供有效负载

2skhul33  于 2023-04-28  发布在  PHP
关注(0)|答案(1)|浏览(80)

干杯

问题:(1)我无法从Zoho Forms获取有效的JSON有效负载。(2)我可能太无知,意识不到明显的问题。

原则上zoho表单是根据参数配置的。内容类型为application/json
这是我根据Zoho Forms所期望的负载:

{
"DateTime" : "<DateTime>",
"Email" : "<Email>",
"Name.First" : "<Name.First>",
"Name.Last" : "<Name.Last>",
"PhoneNumber" : "<PhoneNumber>"
}

使用POST方法发送。
多个测试总是相同的:

测试结果:Webhook测试成功执行状态代码:200回复:没有收到有效载荷。

如果有关系的话:文件的权限是0644文件夹的权限是0755
我用的是Apache,没有。htaccess文件在我的设置.
我的代码-〉

<?php
require_once 'connectme.php';

// Create log file
$logFile = "webhook_log.txt";

// Check connection
if ($conn->connect_error) {
    $error_message = "Connection failed: " . $conn->connect_error . "\n";
    file_put_contents($logFile, $error_message, FILE_APPEND);
    die("Connection failed: " . $conn->connect_error);
} else {
    $success_message = "Connected successfully to the database.\n";
    file_put_contents($logFile, $success_message, FILE_APPEND);
}

// Get request body as JSON raw txt
if (!empty($_POST)) {
    $request_body = json_encode($_POST);
} else {
    $request_body = file_get_contents('php://input');
}

// // $request_data = $_POST;
// $request_data = array_merge($_POST, $_FILES);
// $request_body = file_get_contents('php://input');

// Log the received payload 
if (empty($request_body)) {
    $error_message = "Dude, there is othing to see here.\n";
    file_put_contents($logFile, $error_message, FILE_APPEND);
} else {
    $success_message = "Payload received: " . $request_body . "\n"; 
    file_put_contents($logFile, $success_message, FILE_APPEND);
}

// Check if payload is received
if (empty($request_body)) {
    $error_message = "No payload received from Zoho Forms.\n";
    file_put_contents($logFile, $error_message, FILE_APPEND);
    die("No payload received.");
} else {
    $success_message = "Payload received: " . $request_body . "\n";
    file_put_contents($logFile, $success_message, FILE_APPEND);
}

// Decode JSON payload
$json_data = json_decode($request_body, true);

// Check if payload is valid JSON
if (json_last_error() !== JSON_ERROR_NONE) {
    $error_message = "Received payload is not valid JSON format.\n";
    file_put_contents($logFile, $error_message, FILE_APPEND);
    die("Received payload is not valid JSON format.");
}

// Extract data from JSON payload
$NameFirst = isset($json_data['Name.First']) ? $json_data['Name.First'] : null;
$NameLast = isset($json_data['Name.Last']) ? $json_data['Name.Last'] : null;
$DateTime = isset($json_data['DateTime']) ? $json_data['DateTime'] : null;
$Email = isset($json_data['Email']) ? $json_data['Email'] : null;
$PhoneNumber = isset($json_data['PhoneNumber']) ? $json_data['PhoneNumber'] : null;

// Validate extracted data
if ($NameFirst === null || $NameLast === null || $DateTime === null || $Email === null || $PhoneNumber === null) {
    $error_message = "Invalid or missing parameter names in request: " . json_encode($json_data) . "\nRaw request body: " . $request_body . "\n";
    file_put_contents($logFile, $error_message, FILE_APPEND);
    die("Invalid or missing parameter names in request.");
} elseif (empty($NameFirst) || empty($NameLast) || empty($DateTime) || empty($Email) || empty($PhoneNumber)) {
    $error_message = "Parameters have empty values in request: " . json_encode($json_data) . "\nRaw request body: " . $request_body . "\n";
    file_put_contents($logFile, $error_message, FILE_APPEND);
    die("Parameters have empty values in request.");
}

// Insert data into the clients table
$stmt = $conn->prepare("INSERT INTO clients (NameFirst, NameLast, DateTime, Email, PhoneNumber) VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $NameFirst, $NameLast, $DateTime, $Email, $PhoneNumber);

if ($stmt->execute()) {
    $success_message = "New record created successfully.\n";
    file_put_contents($logFile, $success_message, FILE_APPEND);
} else {
    $error_message = "Error: " . $stmt->error . "\n";
    file_put_contents($logFile, $error_message, FILE_APPEND);
}

// Close database connection and statement
$stmt->close();
$conn->close();
?>

我尝试过的:

1-检查并重新检查Zoho Forms上的配置2-使用Postman测试webhook 3-执行多个测试4-询问聊天GPT(在几条消息建议联系zoho表单支持后)5-写信给Zoho Forms支持并得到以下答案:“我们已经检查了我们的结束和数据已发送从我们的结束作为打算”,这进一步巩固了我的印象,我只是没有经验或愚蠢的能够使这项工作。
我很感激任何帮助。
请让我知道,如果有任何其他信息,我应该提供或任何改进,我可以使我目前的设置太解决这个问题。
问题:您是否发现这段代码有任何明显的问题,可能会阻止有效负载被解析并发送到数据库?除了url、内容类型之外,我在zoho表单的配置中还缺少什么吗?
先谢谢你了。
edit0:typo edit1:log edit 2:主机服务名称(Dreamhost Sharing Hosting)
++++这是来自服务器的反馈:

Time: 2023-04-20 17:12:08
Request method: GET
Request headers: Array
(
    [Content-Length] => 0
    [Connection] => close
    [Host] => www.webhook.rsvphub.me
    [Content-Type] => application/x-www-form-urlencoded
    [User-Agent] => Zoho.com
    [Accept] => application/json
    [Req-Mi-Chain] => -1407411460:8080
)

_POST: Array
(
)

_SERVER: Array
(
    [PATH] => /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    [CONTENT_LENGTH] => 0
    [SCRIPT_NAME] => /tired.php
    [REQUEST_URI] => /tired.php
    [QUERY_STRING] => 
    [REQUEST_METHOD] => GET
    [SERVER_PROTOCOL] => HTTP/1.1
    [GATEWAY_INTERFACE] => CGI/1.1
    [REMOTE_PORT] => 60816
    [SCRIPT_FILENAME] => /home/captain_hook/webhook.rsvphub.me/tired.php
    [SERVER_ADMIN] => webmaster@webhook.rsvphub.me
    [CONTEXT_DOCUMENT_ROOT] => /home/captain_hook/webhook.rsvphub.me
    [CONTEXT_PREFIX] => 
    [REQUEST_SCHEME] => https
    [DOCUMENT_ROOT] => /home/captain_hook/webhook.rsvphub.me
    [REMOTE_ADDR] => 136.143.176.64
    [SERVER_PORT] => 443
    [SERVER_ADDR] => 173.236.170.13
    [SERVER_NAME] => www.webhook.rsvphub.me
    [SERVER_SOFTWARE] => Apache
    [SERVER_SIGNATURE] => 
    [HTTP_CONNECTION] => close
    [HTTP_HOST] => www.webhook.rsvphub.me
    [CONTENT_TYPE] => application/x-www-form-urlencoded
    [HTTP_USER_AGENT] => Zoho.com
    [HTTP_ACCEPT] => application/json
    [HTTP_REQ_MI_CHAIN] => -1407411460:8080
    [SSL_TLS_SNI] => www.webhook.rsvphub.me
    [HTTPS] => on
    [DH_USER] => captain_hook
    [ds_id_47565102] => 
    [dsid] => 47565102
    [SCRIPT_URI] => https://www.webhook.rsvphub.me/tired.php
    [SCRIPT_URL] => /tired.php
    [QS_ConnectionId] => 168202872854339796491526
    [QS_AllConn] => 7
    [QS_SrvConn] => 7
    [UNIQUE_ID] => ZEG4uKq--W0VG86HWLzo1AAAAGA
    [FCGI_ROLE] => RESPONDER
    [PHP_SELF] => /tired.php
    [REQUEST_TIME_FLOAT] => 1682028728.6623
    [REQUEST_TIME] => 1682028728
)

Missing required POST variables.

服务是Dreamhost共享主机,我没有root访问权限

u3r8eeie

u3r8eeie1#

对于那些使用Dreamhost的人来说,解决方案是修改你的。htaccess。
这就是我所做的:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_METHOD} OPTIONS
    RewriteCond %{HTTP_REFERER} ^https?://(www\.)?zoho\.com(/.*)?$ [NC,OR]
    RewriteCond %{HTTP_REFERER} ^https?://.*\.zoho\.com(/.*)?$ [NC]
    RewriteRule .* - [R=200,L]
</IfModule>

好的,这段代码基本上告诉Web服务器,如果它使用Apache mod_rewrite模块,它应该只允许来自主www. example的特定请求(称为OPTIONS请求) www.example.com 网站或其任何子域(如 www.example.com 请求符合这些条件,服务器将以“200 OK”状态响应,这意味着一切都是绿色的,然后它不会做任何其他事情。当您想要控制某些请求的来源并确保它们来自可信的来源时,这非常有用。
�_(o o)_/�

相关问题