Shopify : How to test webhooks in php

孤街醉人 提交于 2019-12-13 11:05:31

问题


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?

Is it like Apis? How to notify that webhooks are called or not.

Please help me.


回答1:


Unlike APIs, Webhook is event driven(triggered on any event e.g. Order Creation) and send data in JSON/XML format to particular URL.

You can create a Webhook in your Shopify store by following steps.

  1. Go to Settings -> Notification -> Webhooks -> Create Webhook
  2. Select Event on which your webhook will be triggered data Format and URL(https) to which you want to send your data.

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

?>


来源:https://stackoverflow.com/questions/55472375/shopify-how-to-test-webhooks-in-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!