我有一个通过php脚本呈现为HTML的XML文件。因为我要插入产品说明,所以要通过在线购物API。
在XML文件中,我有一个元素( $ product_short_description ),该元素可以包含多个文本段落。但是,当尝试在元素内创建换行符时,由于“ br /或p”标记,XML会引发错误。简而言之,我想在XML元素内添加一个换行符。以下编码的主要问题:
$product_short_description = preg_replace('#<br\s*/?>#i', "\n", $product_short_description);
下面是我的全部编码:
<?php
include '../db_config/db_conn.php';
$sql = 'SELECT c1.entity_id AS product_id, c1.sku, c2.value AS prdNm,
c3.value AS product_price,
c5.value AS category_name, c6.value AS weight, c7.value AS image,
c8.value
AS description,
c9.value AS price,c11.value AS shipment_type,c12.value AS
warranty,c13.value AS short_description
FROM catalog_product_entity c1
LEFT JOIN catalog_product_entity_varchar c2 ON c1.entity_id=c2.entity_id
AND c2.attribute_id=71
LEFT JOIN catalog_product_entity_decimal c3 ON c1.entity_id=c3.entity_id
AND c3.attribute_id=75
LEFT JOIN catalog_category_product c4 ON c1.entity_id=c4.product_id
LEFT JOIN catalog_category_entity_varchar c5 ON
c5.entity_id=c4.category_id AND c5.attribute_id=41
LEFT JOIN catalog_product_entity_decimal c6 ON c1.entity_id=c6.entity_id
AND c6.attribute_id=80
LEFT JOIN catalog_product_entity_varchar c7 ON c1.entity_id=c7.entity_id
AND c7.attribute_id=85
LEFT JOIN catalog_product_entity_text c8 ON c1.entity_id=c8.entity_id AND
c8.attribute_id=72
LEFT JOIN catalog_product_entity_decimal c9 ON c1.entity_id=c9.entity_id
AND c9.attribute_id=75
LEFT JOIN catalog_product_entity_int c11 ON c1.entity_id=c11.entity_id
AND
c11.attribute_id=127
LEFT JOIN catalog_product_entity_int c12 ON c1.entity_id=c12.entity_id
AND
c12.attribute_id=154
LEFT JOIN catalog_product_entity_text c13 ON c1.entity_id=c13.entity_id
AND c13.attribute_id=73
WHERE c1.entity_id = 200
';
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
$product_id = $row['product_id'];
$product_sku = $row['sku'];
$product_name = $row['prdNm'];
$product_price = $row['product_price'];
$product_category_name = $row['category_name'];
$product_weight = $row['weight'];
$product_image = $row['image'];
$product_description = $row['description'];
$product_price = $row['price'];
$product_shipment_type = $row['shipment_type'];
$product_warranty = $row['warranty'];
$product_short_description = $row['short_description'];
}
/I'm stuck in here/
$product_short_description = str_replace("#<br\s*/?>#i", "\n",
$product_short_description);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.11street.my/rest/prodservices/product",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "<?xml version=\"1.0\" encoding=\"UTF-8\"
standalone=\"yes\"?>
<Product>
<selMthdCd>01</selMthdCd>
<dispCtgrNo>4932</dispCtgrNo>
<prdTypCd>01</prdTypCd>
<prdNm>$product_name</prdNm>
<prdStatCd>01</prdStatCd>
<prdWght>$product_weight</prdWght>
<minorSelCnYn>Y</minorSelCnYn>
<prdImage01>$product_image</prdImage01>
<htmlDetail>$product_short_description</htmlDetail>
</Product>",
CURLOPT_HTTPHEADER => array(
"cache-control: no-cache",
"content-type: application/xml",
"openapikey: asdfsdafq2e212ewsadfd",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
echo $response;
错误显示在下面:
Product registration failed Detail Description is required.500
我尝试添加 echo 进行测试,并查看 product_short_description 详细信息
$product_short_description = preg_replace('#<br\s*/?>#i', "\n", $product_short_description);
echo "$product_short_description";
输出如下图所示:
我尝试将以下代码替换为
$product_short_description = str_replace("#<br\s*/?>#i", "\n",
$product_short_description);
更改此代码:
$product_short_description = strip_tags($product_short_description, '<br>');
输出显示给我:
The product has been successfully registered. Product No: 68932172200
尽管已成功注册,但我看到了我的在线购物网站,也无法换行。在线购物输出如下所示:
希望有人可以帮助我解决这个问题。谢谢。