为什么我的第二个函数总是 return false?另外,为什么我的 if 语句中的代码 运行 无论如何?

Why does my second function always return false? Also, why does code in my if statement run regardless?

已解决!!抱歉浪费您的时间。

问题: 第二个函数 "verify_webhook_2" 总是 return 为假。 if 语句中的代码运行是否测试 return 是否为真。

我复制并粘贴了第一个函数,然后进行了(我认为是)适当的更改,这样我就可以验证来自两个不同 Shopify 商店的 webhook。我敢肯定这很简单,我只是忘记了,因为我对这一切还很陌生。如果我将 $verify 更改为 $verify2 的秘密,那么从该商店收到的 webhook 将验证为真。

而且我终其一生都无法理解为什么即使两个要求都测试为假,if 语句中的代码也会运行。当从商店接收到与 $verify2 秘密相关的 webhook 时,我无法想到这两者都可以证明是真的。可能是菜鸟的错误?

$verify = "xxxxsecretxxxx";
$verify2 = "xxxxsecretxxxx";

define('SHOPIFY_APP_SECRET', $verify);
define('SHOPIFY_APP_SECRET_2', $verify2);

function verify_webhook($data, $hmac_header)
{
  $calculated_hmac = base64_encode(hash_hmac('sha256', $data, SHOPIFY_APP_SECRET, true));
  return hash_equals($hmac_header, $calculated_hmac);
}

function verify_webhook_2($data, $hmac_header)
{
  $calculated_hmac_2 = base64_encode(hash_hmac('sha256', $data, SHOPIFY_APP_SECRET_2, true));
  return hash_equals($hmac_header, $calculated_hmac_2);
}

$hmac_header = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$data = file_get_contents('php://input');
$verified = verify_webhook($data, $hmac_header);
$verified_2 = verify_webhook_2($data, $hmac_header);
error_log('Webhook verified: '.var_export($verified, true)); //check error.log to see the result

if ($verified == true || $verified_2 == true){
  header("HTTP/1.1 200 OK"); //respond with success
  http_response_code(201);  //respond with success
  file_put_contents('/var/www/html/temp/webhook.json', $data);


  $POST = json_decode(file_get_contents('/var/www/html/temp/webhook.json'), true);
  //$POST = $POST['id'];
  $report = "id: " . $POST['id'] . " - email: " . $POST['email'] . " - name: " . $POST['customer']['first_name'] . " " . $POST['customer']['last_name'] ;
}else{

}

当然,问题一发出,我就意识到自己的失败了。我只是为了第一个函数的比较而写入错误日志,所以当我在错误日志中不断看到 "webhook verified: false" 时,我假设这与我从哪个商店发送数据无关。

我补充了:

error_log('Webhook verified_2: '.var_export($verified_2, true)); //check error.log to see the result

就在第一个 error_log 调用下方,然后将另一个错误日志添加到我的 if 语句的 else 部分,一切正常,并正确响应。 我缺乏理解,导致我认为它没有正常工作,而事实上,一切都正常,但我缺少信息。