Skip to content

Instantly share code, notes, and snippets.

@bdunlap
Created May 11, 2012 13:56
Show Gist options
  • Save bdunlap/2659824 to your computer and use it in GitHub Desktop.
Save bdunlap/2659824 to your computer and use it in GitHub Desktop.
Resends a Shopify order to a webhook target
<?php
define('API_KEY', 'YOUR API KEY HERE');
define('SECRET', 'YOUR SECRET HERE');
define('STORE_DOMAIN', 'YOUR STORE DOMAIN HERE');
define('WEBHOOK_TARGET', 'your webhook URL here');
ini_set('display_errors', 'On');
ini_set('html_errors', 'Off');
error_reporting(-1);
if (count($argv) !== 2) {
usage();
exit;
}
$orderId = filter_var($argv[1], FILTER_VALIDATE_INT);
if (!$orderId) {
usage();
exit;
}
$orderUrl = 'https://'.API_KEY.':'.SECRET.'@'.STORE_DOMAIN."/admin/orders/$orderId.json";
$orderJson = file_get_contents($orderUrl);
if ($orderJson === FALSE) {
die("Failed to retrieve JSON of order $orderId.\n");
}
$response = json_decode($orderJson);
$innerJson = json_encode($response->order);
$response = post($innerJson, WEBHOOK_TARGET);
echo "HTTP response from webhook target follows:\n$response";
function usage()
{
global $argv;
echo "Usage: {$argv[0]} <Shopify order ID>\n";
}
/**
* Lifted from http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/
*/
function post($body, $url)
{
$options = array('http' => array(
'method' => 'POST',
'content' => $body,
'header' => "X-Shopify-Shop-Domain: ".STORE_DOMAIN,
)
);
$ctx = stream_context_create($options);
$fp = fopen($url, 'rb', FALSE, $ctx);
if (!$fp) {
throw new Exception("Could not open $url with fopen()");
}
$response = stream_get_contents($fp);
if ($response === FALSE) {
throw new Exception("Could not read data from $url: $php_errormsg");
}
return $response;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment