Campaign Monitor
From Shopify Wiki
via http://code.dunae.ca/subscribe_from_shopify/
Campaign Monitor “Subscribe” Web Hook for Shopify
Process a Web Hook from Shopify and subscribe the purchaser to a specific Campaign Monitor list. Installation
Download, edit CM_API_KEY, CM_LIST_ID and SHOPIFY_SHOP_ID and wire it up in your Shopify Email & Notifications preferences. Contact
Written by Alex Dunae (dunae.ca, e-mail 'code' at the same domain), 2008. Updates will be announced at http://twitter.com/TheCHANGELOG and on the Atom feed.
Distributed under the Apache License 2.0.
1. /*
2. Process a WebHook from Shopify and subscribe the
3. purchaser to a specific Campaign Monitor list
4.
5. - Alex Dunae (dunae.ca), 2008
6. */
7.
8. define('CM_API_KEY', 'xxxxxxxxxxxxxxxxxxxxxx');
9. define('CM_LIST_ID', '999999');
10. define('SHOPIFY_SHOP_ID', '999999');
11.
12. // Check that X-Shopify-Shop-ID matches SHOPIFY_SHOP_ID
13. $key = $_SERVER['HTTP_X_SHOPIFY_SHOP_ID'];
14.
15. if ($key != SHOPIFY_SHOP_ID) {
16. header('HTTP/1.0 403 Forbidden');
17. exit();
18. }
19.
20. // Read the XML data from Shopify
21. $xml_str = '';
22.
23. $xml_data = fopen('php://input' , 'rb');
24. while (!feof($xml_data)) { $xml_str .= fread($xml_data, 4096); }
25. fclose($xml_data);
26.
27. $req_xml = new SimpleXMLElement($xml_str);
28.
29. // Check for opt-in
30. if(strcasecmp($req_xml->{'buyer-accepts-marketing'}, 'true') != 0) {
31. header('HTTP/1.0 200 OK');
32. echo 'does not accept marketing';
33. exit(0);
34. }
35.
36. // Post to Campaign Monitor
37. $post_str = sprintf('ApiKey=%s&ListID=%s&name=%s&email=%s',
38. urlencode(CM_API_KEY),
39. urlencode(CM_LIST_ID),
40. urlencode(trim($req_xml->{'billing-address'}->name)),
41. urlencode(trim($req_xml->email))
42. );
43.
44. $ch = curl_init('http://app.campaignmonitor.com/api/api.asmx/Subscriber.AddAndResubscribe');
45.
46. curl_setopt($ch, CURLOPT_POST, 1);
47. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
48. curl_setopt($ch, CURLOPT_POSTFIELDS, $post_str);
49.
50. $post_result = curl_exec($ch);
51.
52. curl_close ($ch);
53.
54. // Check if Campaign Monitor returned 'success'
55. $res_xml = new SimpleXMLElement($post_result);
56.
57. if($res_xml->Code == '0') {
58. header('HTTP/1.0 200 OK');
59. print '200';
60. } else {
61. header('HTTP/1.0 400 Bad request');
62. print '400';
63. }
64.
65. exit(0);
For updates go to http://code.dunae.ca/subscribe_from_shopify/
