XML parser using shopify webhooked line-items

℡╲_俬逩灬. 提交于 2019-12-06 13:36:05

You're doing a lot of redundant steps to parse your XML there. You don't need to save the data into a file before processing it, and you have a stray call to SimpleXML that you're not using. All you need is this:

$xmlString = file_get_contents('php://input');
$dom = new DomDocument();
$dom->loadXML($xmlString);

After that, your parsing logic looks fine but you are only ever running one SQL query, with one SKU in it.

Inside your foreach loop, you define the variables $sku and $qty, but you don't do anything with them inside the loop. So next time round the loop, you will over-write their values, and nothing will ever know about the old values.

There are few ways to do this:

  • run SQL inside the loop (not very efficient)
  • build up an array of SKUs and quantities ($sku[] = ...; $qty[] = ...;) and then build your SQL from these arrays
  • slightly tidier, build a single array with the SKU-quantity pairs as nested arrays ($sku_list[] = array('sku' => ..., 'qty' => ...))
  • build your SQL string progressively inside the loop ($sql .= '...') and execute it once at the end
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!