【发布时间】:2016-10-27 22:10:02
【问题描述】:
我已连接到跟踪客户订单的 API。有些订单有一种产品,但有些订单在一个订单中有多种产品。当我尝试插入我的数据库时,我只得到第一个产品。我希望将所有产品插入多个产品的情况下。产品名称的 API 的 XML 是 ->orderItems->orderItem->productName ,我试图遍历所有这些,但我仍然只得到第一个产品。
foreach ($customer_orders as $customer_order) {
$chOrder = curl_init('https://' . $customer_order->reference);
curl_setopt($chOrder, CURLOPT_USERPWD, "username" . ":" . "password");
curl_setopt($chOrder, CURLOPT_HTTPHEADER, array('Content-Type: application/xml'));
curl_setopt($chOrder, CURLOPT_RETURNTRANSFER, true);
$orderResult = curl_exec($chOrder);
curl_close($chOrder);
$shouldContinue = true;
try {
$singleXML = new SimpleXMLElement($orderResult);
} catch (Exception $e) {
$shouldContinue = false;
error_log($e);
error_log('Failed to get history for ' . wp_get_current_user()->user_email . ' using refernece: ' . $customer_order->reference);
}
if ($shouldContinue) {
++$orderCount;
foreach ($singleXML->orderItems->orderItem as $item) {
$product = htmlentities($item->productName, ENT_QUOTES, 'UTF-8');
}
$con = mysqli_connect('localhost', 'root', 'password', 'database');
$query = "INSERT IGNORE INTO customer_product_list(`product`)VALUES('$product')";
mysqli_query($con, $query);
mysqli_close($con);
}
}
【问题讨论】:
-
这能回答你的问题吗? Insert into MySQL from array with mysqli