朋友们,提前请你们不要把问题看得太严格,我是第一次写剧本。
有2个信息块:
- Infoblock (ID=2) 包含商品,我在那里收集了一个包含 WB 文章的数组;
- Infoblock (ID=7),我在其中添加评论本身(我通过 API 收集)。
脚本功能:使用 API,我从 WB 接收对某些产品的评论(通过 WB 文章,与产品一起列在信息块中),并将元素添加到包含评论的信息块中。
请告诉我如何确保在执行函数时检查是否存在此评论(即重新运行脚本时,不会添加已经存在的评论),WB 的每个评论都有一个唯一ID(例如:kzgXM4MB65w5GC7j5vjK),基于此元素属性信息块与评论(ID 7)并检查是否存在评论。
预先感谢大家的帮助!
<?php
// подключение функций пролога
require($_SERVER["DOCUMENT_ROOT"] . "/bitrix/modules/main/include/prolog_before.php");
set_time_limit(60);
CModule::IncludeModule("main"); // CFile
CModule::IncludeModule("iblock"); // CIBlockElement
$PRODUCT_IBLOCK = '2';
$REVIEWS_IBLOCK = '7';
function getRequestResult($request)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: ........';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $result;
}
function parseAndAdd($artnum, $PRODUCT_IBLOCK) {
$org_url = 'https://feedbacks-api.wildberries.ru/api/v1/feedbacks?isAnswered=true&nmId='. $artnum .'&take=5000&skip=0';
$html = getRequestResult($org_url);
$arr = json_decode($html, true);
$counter = 0;
foreach($arr['data']['feedbacks'] as $value){
//Свойства
$PROP = array();
$i = 0;
if (!empty($value['photoLinks'])) {
foreach ($value['photoLinks'] as $morePhoto) {
$arMorePhoto['n'.$i] = array("VALUE"=>CFile::MakeFileArray($morePhoto['fullSize']));
$i++;
}
}
$PROP['ART_WB'] = $value['productDetails']['nmId'];
$PROP['RAITING'] = $value['productValuation'];
$PROP['ISTOCHNIK'] = $value['state'];
$PROP['PHOTO'] = $arMorePhoto;
$PROP['REVIEWSID'] = $value['id'];
//$photolinks[] = $value['photoLinks'];
/*if (!empty($photolinks)) {
foreach($photolinks as $photo){
$photo = $photo['fullSize'];
}
}*/
$el = new CIBlockElement;
$fields = [
'ACTIVE' => "Y",
'IBLOCK_ID' => 7,
"IBLOCK_SECTION_ID" => false,
"NAME" => $value['userName'],
"PREVIEW_TEXT" => $value['text'],
"PREVIEW_PICTURE" => $imgUrl ? CFile::MakeFileArray($imgUrl) : CFile::MakeFileArray($_SERVER["DOCUMENT_ROOT"]."/local/api/wb/avatar.png"),
"PROPERTY_VALUES" => $PROP,
//'ACTIVE_FROM' => $date, // Начало активности
//'DATE_CREATE' => $date, // Дата создания
];
if ($PRODUCT_ID = $el->Add($fields)) {
echo 'Добавлен отзыв, ID: ' . $PRODUCT_ID . '<br />';
$counter++;
} else {
echo "Error[" . $PRODUCT_ID . "]: " . $el->LAST_ERROR . '<br />';
}
}
echo 'Отзывы по товару с артикулом '.$artnum . ' добавлены. Количество '. $counter. '<br>';
}
//print_r($photo);
//print_r(parseAndAdd(113509876, 6));
$places_ids = []; // Храню артикулы всех товаров, по которым будет забирать отзывы
$arSelect = ["NAME","PROPERTY_ART_WB"];
$arFilter = ["IBLOCK_ID" => $PRODUCT_IBLOCK,"ACTIVE" => "Y",];
$resp = CIBlockElement::GetList(Array(), $arFilter, false, false, $arSelect);
while($ob = $resp->GetNextElement()) {
$arFields = $ob->GetFields(); // Получаю значения полей элемента
$places_ids[] = $arFields['PROPERTY_ART_WB_VALUE']; // Заполняю массив с артикулами
}
foreach ($places_ids as $arrwb) {
if (!empty($arrwb)) {
parseAndAdd($arrwb, 7);
}
}
?>