Shopify:如何在php中测试webhooks

时间:2019-04-02 10:20:01

标签: shopify webhooks

我是shopify的新手。我在php中为shopify创建了一个应用程序。我已经使用管理API注册了webhooks。但是我不知道如何测试webhooks。我花了很多时间去弄清楚,但没有得到适当的答复。如何获得响应并在那里写东西?

像Apis吗?如何通知是否调用了网络钩子。

请帮助我。

1 个答案:

答案 0 :(得分:0)

与API不同,Webhook是事件驱动的(在任何事件(例如订单创建)上都会触发),并将JSON / XML格式的数据发送到特定的URL。

您可以按照以下步骤在Shopify商店中创建Webhook。

  1. 转到“设置”->“通知”->“ Webhooks”->“创建Webhook”
  2. 选择将在其上触发Webhook的事件数据格式和要将数据发送到的URL(https)。

现在,您的数据以JSON格式提供给您在URL字段中共享的服务器位置。您可以使用以下代码。

<?php

define('SHOPIFY_APP_SECRET', 'my_shared_secret');
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);
}

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

?>