如何捕获 Shopify Webhook 发送的 HTTP POST 请求 [英] How to catch the HTTP POST request sent by a Shopify Webhook

查看:84
本文介绍了如何捕获 Shopify Webhook 发送的 HTTP POST 请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有点菜鸟,我不害怕承认这一点,我正在将这个项目作为学习经验,以便更好地处理 php 和服务器端脚本/ing 处理.

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.

我正在尝试想出一种使用 Shopify 的方法,并在每次从 Shopify 购物车完成订单时同时更新服务器外数据库.例如,有人从我的在线商店购买了一些东西,我希望它更新我的家庭数据库库存,以显示它现在少了一件.

I'm trying to come up with a way to use Shopify and simultaneously update an off server database every time an order is fulfilled from my Shopify cart. So for example, someone buys something from my online store, I want it to update my home databases inventory to show that it now has one less item.

我得出的结论是,最好的方法是设置一个webhook 通知 向我的服务器发送 HTTP POST 请求,然后我让我的服务器捕获 POST 并将其解析为 XML.然后我将通过将更新我的数据库的 php 脚本读取 XML.

I've come to the conclusion that the best way to do this would be to setup a webhook notification that sends an HTTP POST request to my server, then I'd have my server catch the POST and parse it into an XML. I will then read the XML via a php script that will update my database.

我的 php 没有问题,但我似乎无法弄清楚如何在我的服务器上捕获 webhook.Webhook 要求我提供一个 URL 来发送 POST 请求,我的问题是;网址是什么?

I dont have a problem with the php, but what I can't seem to figure out is how to catch the webhook on my server. Webhook asks me for a URL to send the POST request to, my question to you is; what is the url?

我进行了一些研究,发现您可以通过多种方式捕获 POST 请求,例如 html、php、Access-Control-Allow-Origin 等.但是,由于我对此还是个新手,我真的不明白如何做到这些.我尝试过使用 HTML 隐藏操作表单,但似乎无法让它捕获 XML.

I've done some research and found that you can catch the POST request a number of ways, through html, php, Access-Control-Allow-Origin, etc. However, since I'm still new to this, I don't really understand exactly how to do these. I've tried with an HTML hidden action form but couldn't seem to get it to catch the XML.

我想要做的就是让 webhook 发送其 POST 请求,并将其捕获为 .xml.这样我就可以在每天结束时读取 xml,并相应地更新数据库.

All I want to do is have the webhook send its POST request, and have it caught as a .xml. So that I can read the xml at the end of each day, and update the database accordingly.

如果您能想到更好或更简单的方法来做到这一点,请务必给我您的建议.我希望这是安全的,所以像 Access-Control-Allow-Origin 这样的方法是不可能的.

If you can think of a better or simpler way to do this, by all means please give me your suggestions. I'd like this to be secure, so methods like Access-Control-Allow-Origin are out of the question.

tl;博士:我必须在我的服务器上做什么才能捕获 webhook 通知?我的服务器上应该有什么脚本来提供给 webhook?回调脚本怎么写?

tl;dr: What do I have to do on my server to catch a webhook notification? What script should I have on my server to give to the webhook? How do I write the callback script?

推荐答案

http://example.com/whatever 上创建一个公共 URL.php,其中 example.com 是您的域名,whatever.php 是您可以编辑的 PHP 文件.

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.

然后把这段代码放到whatever.php中:

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);
?>

然后在 Shopify 后台中,您可以创建一个新的 webhook 并将其指向 http://example.com/whatever.php,当您单击 Shopify 后台中的测试 webhook"按钮时,Shopify 将 POST 到您上面的脚本,该脚本又会将 webhook 的正文写入您的 PHP 错误日志.

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.

这篇关于如何捕获 Shopify Webhook 发送的 HTTP POST 请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆