干杯,
问题: (1) 我无法从 Zoho Forms 获取有效的 JSON 负载。 (2) 我可能太无知了,没有意识到明显的问题。
原则上是根据参数配置zoho forms。内容类型是 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 forms 支持) 5- 写信给 Zoho Forms 支持并得到以下答复:“我们已经检查了我们的一端,数据已按预期从我们的一端发送”,这进一步巩固了我的印象,即我只是缺乏经验或愚蠢能够使这项工作。
感谢所有帮助。
如果我应该提供任何其他信息或我可以对当前设置进行任何改进也可以解决此问题,请告诉我。
问题:您是否看到此代码有任何明显的问题,可能会阻止有效负载被解析并发送到数据库?除了 url、内容类型之外,我是否遗漏了 zoho 表单配置的任何内容?
提前致谢。
edit0:错字 编辑1:日志 edit2:托管服务名称(Dreamhost共享托管)
+++++ 这是服务器的反馈:
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 Shared Hosting,我没有root权限
对于那些使用 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>
好的,所以这段代码基本上告诉网络服务器,如果它使用 Apache mod_rewrite 模块,它应该允许特定请求(称为 OPTIONS 请求),只有当它们来自主 zoho.com 网站或其任何子域(如表单)时.zoho.com)。如果请求符合这些条件,服务器将响应“200 OK”状态,这意味着一切都是绿色的,然后它不会做任何其他事情。当您想要控制某些请求的来源并确保它们来自受信任的来源时,这很有用。
¯_(o o)_/¯