问题描述
我是shopify的新手.我在 php 中为 shopify 创建了一个应用程序.我已经使用 admin apis 注册了 webhooks.但我不知道如何测试 webhooks.我花了很多时间来弄清楚,但没有得到任何适当的回应.如何获得回应并在那里写东西?
I am new in shopify. I have created one app in php for shopify. I have registered webhooks using admin apis. But i don't know how to test webhooks. I have spent lots of time to figure out but not getting any proper response. How to get response and write stuff over there?
它像Apis吗?如何通知 webhook 是否被调用.
Is it like Apis? How to notify that webhooks are called or not.
请帮帮我.
推荐答案
与 API 不同,Webhook 是事件驱动的(在任何事件上触发,例如订单创建)并将 JSON/XML 格式的数据发送到特定 URL.
Unlike APIs, Webhook is event driven(triggered on any event e.g. Order Creation) and send data in JSON/XML format to particular URL.
您可以按照以下步骤在 Shopify 商店中创建 Webhook.
You can create a Webhook in your Shopify store by following steps.
- 转到设置 -> 通知 -> Webhooks -> 创建 Webhook
- 选择将触发 Webhook 的事件数据格式和要将数据发送到的 URL (https).
现在您的数据以 JSON 格式提供给您在 URL 字段中共享的服务器位置.您可以使用以下代码.
Now your data is available in JSON format to server location you have shared in URL field. You can use following code.
<?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
?>
这篇关于Shopify:如何在 php 中测试 webhooks的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!