Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/244.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shopify Order使用PHP创建插入Mysql的Webhook_Php_Mysql_Json_Shopify_Webhooks - Fatal编程技术网

Shopify Order使用PHP创建插入Mysql的Webhook

Shopify Order使用PHP创建插入Mysql的Webhook,php,mysql,json,shopify,webhooks,Php,Mysql,Json,Shopify,Webhooks,我一直在为我的shopify商店构建一个基于web的私人应用程序,它更能满足我们的业务需求。虽然我能够将“所有订单”或“所有产品”等转储到Mysql,但我还没有弄清楚如何执行shopify订单创建webhook,以便在shopify中创建新订单时将其插入Mysql数据库 相反,我需要每隔“x”次运行一次脚本,以查看是否有新的顺序(如果我没有错的话,这可能会导致在同时运行其他API调用时超过API限制) 我理解事件的过程,但我正在努力执行 1. New order created in Shop

我一直在为我的shopify商店构建一个基于web的私人应用程序,它更能满足我们的业务需求。虽然我能够将“所有订单”或“所有产品”等转储到Mysql,但我还没有弄清楚如何执行shopify订单创建webhook,以便在shopify中创建新订单时将其插入Mysql数据库

相反,我需要每隔“x”次运行一次脚本,以查看是否有新的顺序(如果我没有错的话,这可能会导致在同时运行其他API调用时超过API限制)

我理解事件的过程,但我正在努力执行

 1. New order created in Shopify by Customer &or Admin.
 2. Triggers webhook and sends Json to desired url i.e(https://mydomain//new-order.php). -> [Struggling]
 3. When this happens the Json is decoded. -> [Struggling]
 4. Assigned to a variable. -> [This i can do]
 5. Inserted into a Mysql database. -> [This i can do]

=> Question:
   How do you once you have created the webhook (in Shopify) get it to trigger your code to run thereafter and execute?
下面是我编写的代码,但当我发送一个测试挂钩时,数据库没有更新

[new orders.php]文件中的所有内容(分解以显示我的思路):

[1] 用于连接到Shopify商店的专用应用程序凭据

<?php
   $api_url = https://apikey:password@my-store.shopify.com';
   $shopify = $api_url . '/admin/webhooks.json';
[3] 解码webhook数据

    $webhook_content = '';
    $webhook = fopen('php://input' , 'rb');
    while(!feof($webhook)){ //loop through the input stream while the end of file is not reached
        $webhook_content .= fread($webhook, 4096); //append the content on the current iteration
    }
    fclose($webhook); //close the resource

    $orders = json_decode($webhook_content, true); //convert the json to array
[4] 将新订单添加到Mysql数据库表中

      // not sure if a foreach loop is necessary in this case?
      foreach($orders as $order){ 

        $servername = "mysql.servername.com";
        $database = "database_name";
        $username = "user_name";
        $password = "password";
        $sql = "mysql:host=$servername;dbname=$database;";


        // Create a new connection to the MySQL database using PDO, $my_Db_Connection is an object
        try { 
          $db = new PDO($sql, $username, $password);
          //echo "<p> DB Connect = Success.</p>";
        } catch (PDOException $error) {
          echo 'Connection error: ' . $error->getMessage();
        }
                $order_id = $order['id'];
                $order_number = $order['name'];
                $f_name = $order['billing_address']['name'];
                $payment_gateway = $order['gateway'];
                $financial_status = $order['financial_status'];
                $order_value = $order['total_price'];
                $order_status = $order['#'];
                $shipping_province = $order['shipping_address']['province'];
                $created_at = $order['created_at'];
                $updated_at = $order['updated_at'];
                $shipping_method = $order['shipping_lines'][0]['title'];

                $stmt = $db->query("INSERT INTO orders(order_id, order_number, cust_fname, payment_gateway, financial_status, order_value, order_status, ship_to, created_at, updated_at, shipping_method)
                                    VALUES ('$created_at', '$order_id', '$order_number', '$f_name', '$payment_gateway', '$financial_status', '$order_value', '$order_status', '$shipping_province', '$created_at', '$updated_at', '$shipping_method')");

            }

    ?>
//不确定在这种情况下是否需要foreach循环?
foreach($orders作为$order){
$servername=“mysql.servername.com”;
$database=“database\u name”;
$username=“user\u name”;
$password=“password”;
$sql=“mysql:host=$servername;dbname=$database;”;
//使用PDO创建到MySQL数据库的新连接,$my_Db_connection是一个对象
试试{
$db=新PDO($sql、$username、$password);
//echo“DB Connect=Success.

”; }捕获(异常$error){ 回显“连接错误:”。$error->getMessage(); } $order_id=$order['id']; $order_number=$order['name']; $f_name=$order['billing_address']['name']; $payment_gateway=$order['gateway']; $financial_status=$order['financial_status']; $order_值=$order[‘总价’]; $order_status=$order['#']; $shipping_province=$order['shipping_address']['province']; $created_at=$order['created_at']; $updated_at=$order['updated_at']; $shipping_method=$order['shipping_lines'][0]['title']; $stmt=$db->query(“插入订单(订单id、订单编号、客户名称、付款网关、财务状态、订单值、订单状态、发货地点、创建地点、更新地点、发货方式)” 值(“$created_at”、“$order_id”、“$order_number”、“$f_name”、“$payment_gateway”、“$financial_status”、“$order_value”、“$order_status”、“$shipping_province”、“$created_at”、“$updated_at”、“$shipping_method”); } ?>
任何帮助都将不胜感激,我希望我已经为我目前面临的问题提供了足够的背景。如果需要任何其他信息,我会尽力解释为什么我会这样做


关于,

更新,我设法解决了这一问题,对于那些可能与以下问题斗争的人,我就是这样解决的

这里有两个方面

 1. Setting up the webhook [Shopify -> Notifications -> webhooks].
 2. The php file that processes the webhook.

1. -> Create Webhook in shopify and point to where you php url [example.com/Process-webhook.php]
2. -> Process-webhook.php
php代码
 // Load variables
 $webhook_content = NULL;

 // Get webhook content from the POST
 $webhook = fopen('php://input' , 'rb');
 while (!feof($webhook)) {
 $webhook_content .= fread($webhook, 4096);
 }

 fclose($webhook);

 // Decode Shopify POST
 $webhook_content = json_decode($webhook_content, TRUE);

 $servername = "server_name";
 $database = "database";
 $username = "user_name";
 $password = "password";
 $sql = "mysql:host=$servername;dbname=$database;";

 // Create a new connection to the MySQL database using PDO, $my_Db_Connection is an object
 try { 
  $db = new PDO($sql, $username, $password);
  //echo "<p> DB Connect = Success.</p>";
 } catch (PDOException $error) {
  echo 'Connection error: ' . $error->getMessage();
 }

 //Assign to variable 
   $order_id = $webhook_content['id'];
   $order_number = $webhook_content['name'];
   $f_name = $webhook_content['billing_address']['name'];
   $payment_gateway = $webhook_content['gateway'];
   $financial_status = $webhook_content['financial_status'];
   $pick_status = $webhook_content['NULL']; 
   $pack_status = $webhook_content['NULL']; 
   $fulfill_status = $webhook_content['NULL']; 
   $order_value = $webhook_content['total_price'];
   $order_status = $webhook_content['NULL']; 
   $shipping_province = $webhook_content['shipping_address']['province'];

   // I wanted to insert the variant_id's and quantity as a string in one column. With this i can unserialise and use when needed 
   $items = [];
   foreach($webhook_content["line_items"] as $item) {
            $items[$item["variant_id"]]['quantity'] = $item["quantity"];
        }
        $items = serialize($items);

   $created_at = $webhook_content['created_at'];
   $updated_at = $webhook_content['updated_at'];
   $shipping_method = $webhook_content['shipping_lines'][0]['title'];

   $stmt = $db->query("INSERT INTO orders(order_id, 
   order_number, 
   cust_fname, 
   payment_gateway, 
   financial_status, 
   order_value, 
   order_status, 
   ship_to, 
   items,
   created_at, 
   updated_at, 
   shipping_method)

   VALUES ('$order_id', 
  '$order_number', 
  '$f_name', 
  '$payment_gateway', 
  '$financial_status', 
  '$order_value', 
  '$order_status', 
  '$shipping_province', 
  '$items',
  '$created_at', 
  '$updated_at', 
  '$shipping_method')");

 ?>
//加载变量
$webhook_content=NULL;
//从帖子中获取webhook内容
$webhook=fopen('php://input","rb",;
而(!feof($webhook)){
$webhook_content.=fread($webhook,4096);
}
fclose($webhook);
//解码Shopify POST
$webhook\u content=json\u decode($webhook\u content,TRUE);
$servername=“服务器名称”;
$database=“database”;
$username=“user\u name”;
$password=“password”;
$sql=“mysql:host=$servername;dbname=$database;”;
//使用PDO创建到MySQL数据库的新连接,$my_Db_connection是一个对象
试试{
$db=新PDO($sql、$username、$password);
//echo“DB Connect=Success.

”; }捕获(异常$error){ 回显“连接错误:”。$error->getMessage(); } //分配给变量 $order_id=$webhook_content['id']; $order_number=$webhook_content['name']; $f_name=$webhook_内容['billing_address']['name']; $payment_gateway=$webhook_content['gateway']; $financial_status=$webhook_content['financial_status']; $pick_status=$webhook_content['NULL']; $pack_status=$webhook_content['NULL']; $fulfill_status=$webhook_content['NULL']; $order_value=$webhook_content['总价']; $order_status=$webhook_content['NULL']; $shipping_province=$webhook_content['shipping_address']['province']; //我想在一列中插入变量id和数量作为字符串。有了它,我可以在需要时取消序列化并使用 $items=[]; foreach($webhook\u内容[“行项目”]作为$item){ $items[$item[“variant_id”]['quantity']=$item[“quantity”]; } $items=序列化($items); $created_at=$webhook_content['created_at']; $updated_at=$webhook_content['updated_at']; $shipping_method=$webhook_内容['shipping_lines'][0]['title']; $stmt=$db->query(“插入订单(订单id), 订单号, 客户名称, 支付网关, 财务状况, 订单价值, 订单状态, 船运至, 项目, 创建于, 更新地址:, 装运(单位法) 值(“$order_id”, “$订单号”, “$f_name”, “$payment_gateway”, “$财务状况”, “$order_value”, “$order_status”, “$shipping_province”, “$items”, “$created_at”, “$updated_at”, “$shipping_method”)”; ?>
首先将潜在错误记录到文件中。。。当远程服务器调用该脚本时,周围显然没有人“监视”这些调试输出。此外,实施适当的错误控制——例如,现在你甚至对最后的查询是否成功不感兴趣。@BRroe,谢谢你的提醒!我最近开始“开发”,所以你提到的很多东西我知道我需要做,但还没有准备好去做。我将集中讨论imple
 // Load variables
 $webhook_content = NULL;

 // Get webhook content from the POST
 $webhook = fopen('php://input' , 'rb');
 while (!feof($webhook)) {
 $webhook_content .= fread($webhook, 4096);
 }

 fclose($webhook);

 // Decode Shopify POST
 $webhook_content = json_decode($webhook_content, TRUE);

 $servername = "server_name";
 $database = "database";
 $username = "user_name";
 $password = "password";
 $sql = "mysql:host=$servername;dbname=$database;";

 // Create a new connection to the MySQL database using PDO, $my_Db_Connection is an object
 try { 
  $db = new PDO($sql, $username, $password);
  //echo "<p> DB Connect = Success.</p>";
 } catch (PDOException $error) {
  echo 'Connection error: ' . $error->getMessage();
 }

 //Assign to variable 
   $order_id = $webhook_content['id'];
   $order_number = $webhook_content['name'];
   $f_name = $webhook_content['billing_address']['name'];
   $payment_gateway = $webhook_content['gateway'];
   $financial_status = $webhook_content['financial_status'];
   $pick_status = $webhook_content['NULL']; 
   $pack_status = $webhook_content['NULL']; 
   $fulfill_status = $webhook_content['NULL']; 
   $order_value = $webhook_content['total_price'];
   $order_status = $webhook_content['NULL']; 
   $shipping_province = $webhook_content['shipping_address']['province'];

   // I wanted to insert the variant_id's and quantity as a string in one column. With this i can unserialise and use when needed 
   $items = [];
   foreach($webhook_content["line_items"] as $item) {
            $items[$item["variant_id"]]['quantity'] = $item["quantity"];
        }
        $items = serialize($items);

   $created_at = $webhook_content['created_at'];
   $updated_at = $webhook_content['updated_at'];
   $shipping_method = $webhook_content['shipping_lines'][0]['title'];

   $stmt = $db->query("INSERT INTO orders(order_id, 
   order_number, 
   cust_fname, 
   payment_gateway, 
   financial_status, 
   order_value, 
   order_status, 
   ship_to, 
   items,
   created_at, 
   updated_at, 
   shipping_method)

   VALUES ('$order_id', 
  '$order_number', 
  '$f_name', 
  '$payment_gateway', 
  '$financial_status', 
  '$order_value', 
  '$order_status', 
  '$shipping_province', 
  '$items',
  '$created_at', 
  '$updated_at', 
  '$shipping_method')");

 ?>