1
0
Fork 0
mirror of synced 2025-04-06 22:53:36 +03:00

add image to imgur

This commit is contained in:
Dmitry Mamontov 2015-07-20 17:56:21 +03:00
parent c1d614688f
commit 6420c215f9

View file

@ -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);