Skip to content

Instantly share code, notes, and snippets.

@sandeepshetty
Created February 15, 2012 20:32
Show Gist options
  • Save sandeepshetty/1838803 to your computer and use it in GitHub Desktop.
Save sandeepshetty/1838803 to your computer and use it in GitHub Desktop.
Verify Shopify webhook in PHP
<?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 ($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
?>
@talha-presstigers
Copy link

Need clarification of this line : $data = file_get_contents('php://input');
what is the purpose of 'php://input', is it will remain same for every webhook request?

@peterchaula
Copy link

php://input is a stream .ie standard input (stdin). That is the request body

@ravi7194
Copy link

i have use this code but error are accur

@ravi7194
Copy link

Notice: Undefined index: HTTP_X_SHOPIFY_HMAC_SHA256 in E:\wamp\www\vs_dev.sareeo.clothing\seller\paypal\hook.php on line 18

Warning: hash_equals(): Expected known_string to be a string, null given in E:\wamp\www\vs_dev.sareeo.clothing\seller\paypal\hook.php on line 14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment