简体   繁体   English

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

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

SOLVED!! 解决了!! Sorry for wasting your time. 很抱歉浪费您的时间。

Problems: Second function "verify_webhook_2" always returns false. 问题:第二个函数“ verify_webhook_2”始终返回false。 Code in if statement runs whether tests return true or not. if语句中运行的代码是否测试返回true。

I copied and pasted the first function, then made (what I would think to be) appropriate changes, so I can verify webhooks coming from two different Shopify stores. 我复制并粘贴了第一个函数,然后进行了(我认为是)适当的更改,因此我可以验证来自两个不同Shopify商店的webhooks。 I'm sure it's something simple that I am just oblivious to, as I'm still fairly new to all of this. 我敢肯定,这只是我遗忘的简单事物,因为我对所有这些还相当陌生。 If I change $verify to the secret for $verify2 then webhooks received from that shop will verify true. 如果我将$verify更改为$verify2的机密,则从该商店收到的webhooks将被验证为是。

And I cannot for the life of me understand why the code in the if statement runs even when both requirements test false. 而且我无法终生理解为什么即使两个要求都测试为false时,if语句中的代码仍会运行。 There's no way I can think of that either could prove true when the receiving a webhook from the shop related to the $verify2 secret. 当从商店收到与$verify2机密有关的$verify2时,我无法想到这两种方法都可以证明是正确的。 Probably a rookie mistake? 可能是菜鸟的错误?

$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{

}

Of course, right after posting the question, I realized my failure. 当然,在发布问题后,我立即意识到自己的失败。 I had only written to the error log for the first function's comparison, so when I kept seeing "webhook verified: false" in the error logs, I assumed that was regardless of the shop I was sending data from. 我只写了错误日志以进行第一个功能的比较,因此当我在错误日志中不断看到“ webhook verify:false”时,我认为这与我发送数据的工厂无关。

I added: 我补充说:

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

just below the first error_log call, then added another error log into the else section of my if statement, and all is working correctly, and responding correctly. 在第一个error_log调用下面,然后将另一个错误日志添加到我的if语句的else部分中,所有命令均正常运行,并且响应正确。 It was a lack of understanding on my part that led to me believing it was not working correctly, when in fact, everything was, but I was missing information. 我的这种缺乏了解导致我认为它无法正常工作,而实际上一切都正常,但是我缺少信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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