Нужна помощь с парсером на PHP

Обсуждение серверного программирования.

Модераторы: Duncon, Yurich

Ответить
artemka170691
Сообщения: 1
Зарегистрирован: 25 янв 2017, 09:58

25 янв 2017, 10:00

Помогите перенастроить парсер. Нужно что бы брало с сайта картинку и описание. Раньше прайс был другой. и было все нормально. прайсы теперь изменились. и парсер перестал работать. нужно чуть поправить. КТО МОЖЕТ ПОМОЧЬ?

Код: Выделить всё

namespace Supplier;
 
use Model\Supplier;
 
defined('PIMPORT') or die('Error access');
 
/**
* Class Kompl
* @package Supplier
*/
class Kompl extends \Infr\Supplier {
 
/** @var string */
protected $_host = 'http://www.fcenter.ru';
 
/** @var string */
protected $_productMask = 'http://www.fcenter.ru/product/goods/{PRODUCT_ID}';
 
/**
* @throws \Exception
* @return string
*/
protected function _getPrice() {
//проверяем подключен ли плагин и к нему поставщик
$class = 'Plugins\Suppliers\Supplier\Kompl';
if (class_exists($class)) {
/** @var \Plugins\Suppliers\Infr\Supplier $supplierItem */
$supplierItem = new $class;
$pricePath = $supplierItem->getPricePath();
if (file_exists($pricePath)) {
return file_get_contents($pricePath);
} else {
throw new \Exception('Прайс-лист для поставщика не загружен');
}
} else {
throw new \Exception('К плагину Suppliers не подключен нужный поставщик');
}
}
 
/**
* @param \Model\Supplier $supplier
* @param \Model\Supplier\Product $product
* @param array $params
* @return mixed|\Model\Supplier\Product
*/
public function getProduct(Supplier $supplier, Supplier\Product $product, array $params = array()) {
if ($product->url) {
$html = \Infr\Request::httpRequest($product->url);
 
if ($html) {
$dom = \Infr\str_get_html($html);
 
if ($dom) {
$pict = $dom->find('a.preview', 0);
if ($pict) {
$product->pictures = array((string) $pict->getAttribute('href'));
$pict->clear();
}
 
unset($dom);
}
}
unset($html);
}
 
return $product;
}
 
 
/**
* @param \Model\Supplier $supplier
* @param array $params
* @return array|mixed|\Model\Supplier\Product\Collection
* @throws \Exception
*/
public function getProducts(Supplier $supplier, array $params = array()) {
$collection = new Supplier\Product\Collection();
 
$xmlContent = $this->_getPrice();
if (empty($xmlContent)) {
return $collection;
}
 
$xml = simplexml_load_string($xmlContent);
if (!$xml) {
throw new \Exception('Из сайта ' . $this->_host . ' не загружен прайс');
}
 
if (!empty($xml->product)) {
$categoryCollection = $this->getCategories($supplier);
 
foreach ($xml->product as $product) {
$id = (string) $product->id;
$categoryId = (string) $product->categoryID;
 
$categoryItem = $categoryCollection->find($categoryId);
if (empty($categoryItem)) {
continue;
}
 
if (empty($id) || empty($categoryId)) {
continue;
}
 
$data = array(
'id' => $id,
'article' => $id,
'name' => (string) $product->name,
'shortDescription' => '',
'description' => (string) $product->desc,
'price' => (float) $product->wholesale,
'inStock' => 1,
'categoryId' => $categoryId,
'category' => $categoryItem,
'pictures' => array(),
'options' => array(),
'url' => str_replace('{PRODUCT_ID}', $id, $this->_productMask),
'params' => array('article' => $id),
);
 
$productItem = $collection->createElement($data);
$collection->add($productItem);
}
}
 
return $collection;
}
 
 
/**
* @param \Model\Supplier $supplier
* @return \Model\Supplier\Category\Collection
* @throws \Exception
*/
public function getCategories(Supplier $supplier) {
$categoryCollection = new Supplier\Category\Collection();
 
$xmlContent = $this->_getPrice();
$xml = simplexml_load_string($xmlContent);
 
if (!$xml) {
throw new \Exception('Из сайта ' . $this->_host . ' не загружен прайс');
}
 
if (!empty($xml->product)) {
$categoriesArray = array();
 
foreach ($xml->product as $product) {
$categoryId = (string) $product->categoryID;
$categoryName = (string) $product->category;
 
if (empty($categoryId)) {
continue;
}
 
$categoriesArray[$categoryId] = $categoryName;
}
 
foreach ($categoriesArray as $categoryId => $categoryName) {
if ($categoryCollection->find($categoryId)) {
continue;
}
 
$data = array(
'id' => $categoryId,
'parentId' => 0,
'name' => $categoryName,
'supplierId' => $supplier->id,
'url' => '#'
);
 
$categoryItem = $categoryCollection->createElement($data);
$categoryCollection->add($categoryItem);
}
}
 
return $categoryCollection;
}
 
} 
Ответить