How to catch the HTTP POST request sent by a Shopify Webhook

前端 未结 5 621
时光取名叫无心
时光取名叫无心 2021-01-31 05:50

I\'m somewhat of a noob, and not afraid to admit that, I\'m working on this project as a learning experience to get better with php and serverside script/ing handling.

I

相关标签:
5条回答
  • 2021-01-31 06:20

    Create a public URL at http://example.com/whatever.php, where example.com is your domain name and whatever.php is a PHP file that you can edit.

    Then put this code into whatever.php:

    <?php
    $webhookContent = "";
    
    $webhook = fopen('php://input' , 'rb');
    while (!feof($webhook)) {
        $webhookContent .= fread($webhook, 4096);
    }
    fclose($webhook);
    
    error_log($webhookContent);
    ?>
    

    Then in the Shopify admin you can create a new webhook and point it at http://example.com/whatever.php, and when you click the 'test webhook' button in the Shopify admin, Shopify will POST to your script above, which should in turn write the body of the webhook to your PHP error log.

    0 讨论(0)
  • 2021-01-31 06:26

    Sorry, I don't have enough reputation to post comments, but here is the contents of the dead link from Edward Ocampo-Gooding's answer:

    PHP Example w/ SimpleXML (PHP 5+)

    The script below shows you how to get the XML data in from Shopify into your script, archive the file, and send the proper headers ...

    Given that the new order subscription setup in the admin for the webhook is: http://example.com/some-script.php?key=123456789

    Contents of some-script.php on http://example.com/:

    <?     
    // Protect URL from rogue attacks/exploits/spiders
    // Grab from GET variable as given in Shopify admin URL
    // for the webhook
    //
    // NOTE: This is not necessary, just a simple verification
    //
    // A digital signature is also passed along from Shopify,
    // as is the shop's domain name, so you can use one or both of those
    // to ensure a random person isn't jacking with your script (or some
    // spider just randomly hitting it to see what's there).
    //
    // If $key doesn't matched what should be passed in from the
    // webhook url, the script simply exits
    $key = $_GET['key']; 
    
    if ($key != '123456789') {
      header('HTTP/1.0 403 Forbidden');
      exit();
    }
    
    // Variables used for processing/saving
    $xmlString = ;  // Used to get data from Shopify into script
    $name = ;  // Saves the billing address name to be used for later ...
    $email = ;  // Save the email address of the user to be used for later ...
    $productTitles = array();  // Saves all titles of products purchased to be used for later ... 
    
    // Get XML data and read it into a string for use with SimpleXML
    // Thanks to David Oxley (http://www.numeriq.co.uk) for help with this
    $xmlData = fopen('php://input' , 'rb'); 
    while (!feof($xmlData)) { $xmlString .= fread($xmlData, 4096); }
    fclose($xmlData);
    
    // Save order XML in file in orders directory
    // This creates a file, write the xml for archival purposes, and closes the file ...
    // If the file already exists, it appends the data ... this should create a separate
    // file for every order but if two orders are processed the same second, they'll both
    // be in the same file
    file_put_contents('orders/order' . date('m-d-y') . '-' . time() . '.xml', $xmlString, FILE_APPEND);
    
    // Use SimpleXML to get name, email, and product titles
    // SimpleXML allows you to use the $xml object to easily
    // retrieve the data ...
    // Please note, if hyphens are used in the xml node, you must
    // surround the call to that member with {'member-name'} as is
    // shown below when getting the billing-address name & the
    // line items
    $xml = new SimpleXMLElement($xmlString);
    
    $name = trim($xml->{'billing-address'}->name);
    $email = trim($xml->email);
    
    // Create productTitles array with titles from products
    foreach ($xml->{'line-items'}->{'line-item'} as $lineItem) {
      array_push($productTitles, trim($lineItem->title));
    }
    
    // You would then go on using $name, $email, $productTitles in your script
    // to do whatever the heck you please ...
    
    // Once you are done doing what you need to do, let Shopify know you have 
    // the data and all is well!
    header('HTTP/1.0 200 OK');
    exit();
    
    // If you want to tell Shopify to try sending the data again, i.e. something
    // failed with your processing and you want to try again later
    header('HTTP/1.0 400 Bad request');
    exit();
    ?>
    
    0 讨论(0)
  • 2021-01-31 06:31

    Shopify webhook doesn't passed data using the common GET or POST request method. You can use the fopen() method in PHP and pass in the php://input stream.

    Suppose you create a webhook for cart update and set URL http://example.com/cart_update_hook.php then put the following code in cart_update_hook.php to get the data sent by shopify webhook.

    <?php
    $webhookContent = "";
    $webhook = fopen('php://input' , 'rb');
    while (!feof($webhook)) {
        $webhookContent .= fread($webhook, 4096);
    }
    fclose($webhook);
    $data = json_decode($webhookContent, true);
    //do whatever you want with the data
    ?>
    
    0 讨论(0)
  • 2021-01-31 06:36

    To answer your question about the URL, this is the endpoint on your server that is going to handle receiving the webhook. This is pretty straightforward to set up with most web frameworks, it just needs to handle the post request and respond with a 200 OK.

    Once you have an endpoint set up on your server you can create the webhook on shopify with the URL being the endpoint on the webserver.

    0 讨论(0)
  • 2021-01-31 06:37

    It sounds like you’re most familiar with PHP, so I’ll answer based on that.

    You need a PHP script/page that’s accessible as a public URL that can take the data sent in the HTTP POST Shopify sends you and turn it into the form your database wants.

    Here’s an example of what that PHP script could look like: http://wiki.shopify.com/WebHook#PHP_Example_w.2F_SimpleXML_.28PHP_5.2B.29

    0 讨论(0)
提交回复
热议问题