Given a Shopify order ID, pulls the order JSON from Shopify API and resends the order to an arbitrary webhook URL.
<?php
$settings = array(
'store-domain' => 'YOUR SHOPIFY DOMAIN HERE',
'api-key' => 'YOUR API KEY HERE',
'api-secret' => 'YOUR API SECRET HERE',
'webhook-url' => 'YOUR WEBHOOK URL HERE, INCLUDING "KEY" PARAMETER IF APPLICABLE',
);
ini_set('display_errors', 'On');
error_reporting(-1);
if (count($argv) !== 2) {
usage();
exit;
}
$orderId = filter_var($argv[1], FILTER_VALIDATE_INT);
if (!$orderId) {
usage();
exit;
}
$orderUrl = "https://{$settings['api-key']}:{$settings['api-secret']}@{$settings['store-domain']}/admin/orders/$orderId.json";
$orderJson = file_get_contents($orderUrl);
if ($orderJson === FALSE) {
echo "Failed to retrieve JSON of order $orderId.\n";
} else {
echo "JSON follows:\n$orderJson\n\n";
$response = json_decode($orderJson);
$innerJson = json_encode($response->order);
$response = post($innerJson, $settings['webhook-url']);
echo "HTTP response body 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)
{
global $settings;
$options = array('http' => array(
'method' => 'POST',
'content' => $body,
'header' => "X-Shopify-Shop-Domain: {$settings['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;
}
?>