From 6420c215f9419a4bd1917a4c7a0435d90a1eac25 Mon Sep 17 00:00:00 2001 From: Dmitry Mamontov Date: Mon, 20 Jul 2015 17:56:21 +0300 Subject: [PATCH] add image to imgur --- MoySkladICMLParser.php | 95 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 78 insertions(+), 17 deletions(-) diff --git a/MoySkladICMLParser.php b/MoySkladICMLParser.php index bbec8d5..07eccb9 100644 --- a/MoySkladICMLParser.php +++ b/MoySkladICMLParser.php @@ -7,6 +7,16 @@ class MoySkladICMLParser */ const BASE_URL = 'https://online.moysklad.ru/exchange/rest/ms/xml'; + /** + * imgur url + */ + const IMGUR_URL = 'https://api.imgur.com/3/image.json'; + + /** + * imgur client id + */ + const IMGUR_CLIENT_ID = '042dcb6bf65b6fd'; + /** * Таймаут в секундах */ @@ -138,11 +148,62 @@ class MoySkladICMLParser $products = $this->parseProducts($categories, $vendors); + if (isset($this->options['image']) && $this->options['image'] === true) { + $products = $this->uploadImage($products); + } + $icml = $this->createICML($products, $categories); $icml->asXML($this->getFilePath()); } + /** + * Загружаем изображения + * + * @param array $products + * @return array + */ + protected function uploadImage($products) + { + $uploaded = array(); + if (file_exists(__DIR__ . "/images.json")) { + $uploaded = json_decode(file_get_contents(__DIR__ . "/images.json"), true); + } + + foreach ($products as $id => $product) { + if (isset($product['tmpPicture'])) { + if (isset($uploaded[$this->shop]) && isset($uploaded[$this->shop][$product['tmpPicture']['uuid']])) { + $products[$id]['picture'] = $uploaded[$this->shop][$product['tmpPicture']['uuid']]; + continue; + } + + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, self::IMGUR_URL); + curl_setopt($ch, CURLOPT_TIMEOUT, 30); + curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . self::IMGUR_CLIENT_ID)); + curl_setopt($ch, CURLOPT_POST, true); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => $product['tmpPicture']['contents'])); + $result = curl_exec($ch); + curl_close($ch); + $result = json_decode($result, true); + + if (isset($result['success']) && $result['success'] == true) { + $products[$id]['picture'] = $result['data']['link']; + $uploaded[$this->shop][$product['tmpPicture']['uuid']] = $result['data']['link']; + } + unset($product['tmpPicture']); + } + } + + if (count($uploaded) > 0) { + file_put_contents(__DIR__ . "/images.json", json_encode($uploaded)); + } + + return $products; + } + /** * Парсим товарные группы * @@ -262,7 +323,11 @@ class MoySkladICMLParser $start = 0; $total = 0; do { - $xml = $this->requestXml(self::PRODUCT_LIST_URL.'?'.http_build_query(array('start' => $start))); + $query = array('start' => $start); + if (isset($this->options['image']) && $this->options['image'] === true) { + $query['fileContent'] = 'true'; + } + $xml = $this->requestXml(self::PRODUCT_LIST_URL.'?'.http_build_query($query)); if ($xml) { $total = $xml[0]['total']; @@ -290,23 +355,19 @@ class MoySkladICMLParser 'offers' => array(), ); - // Добавление изображений и url из кастомных свойств - if (isset($v->attribute)) { - foreach ($v->attribute as $attr) { - if (isset($attr['valueString']) && stripos($attr['valueString'], 'http') !== false) { - if ( - stripos($attr['valueString'], '.jpg', 1) !== false || - stripos($attr['valueString'], '.jpeg', 1) !== false || - stripos($attr['valueString'], '.gif', 1) !== false || - stripos($attr['valueString'], '.png', 1) !== false - ) { - $products[$uuid]['picture'] = (string) $attr['valueString']; - } else { - $products[$uuid]['url'] = (string) $attr['valueString']; - } - } - } + if ( + isset($this->options['image']) && + $this->options['image'] === true && + isset($v->images) && + isset($v->images->image) && + isset($v->images->image->contents) + ) { + $products[$uuid]['tmpPicture'] = array( + 'uuid' => (string) $v->images->image->uuid, + 'contents' => (string) $v->images->image->contents + ); } + } } else { throw new RuntimeException('No xml - ' . $this->shop);