· 6 years ago · Mar 26, 2020, 07:10 AM
1<?php
2
3class Api
4{
5 private $storeId;
6
7 public function __construct($storeId)
8 {
9 $this->storeId = $storeId;
10 }
11
12 public function getCategories()
13 {
14 return $this->requestGet('https://lenta.com/api/v1/stores/' . $this->storeId . '/catalog');
15 }
16
17 public function getProducts($categoryId, $offset)
18 {
19 return $this->requestPostForProductList($categoryId, $offset);
20
21 }
22
23 public function getProductDetail($productId)
24 {
25 return $this->requestGet('https://lenta.com/api/v1/stores/' . $this->storeId . '/skus/' . $productId);
26 }
27 private function requestPostForProductList($categoryId, $offset){
28 $curl = curl_init();
29 curl_setopt_array($curl, array(
30 CURLOPT_URL => "https://lenta.com/api/v1/stores/0154/skus",
31 CURLOPT_RETURNTRANSFER => true,
32 CURLOPT_ENCODING => "",
33 CURLOPT_MAXREDIRS => 10,
34 CURLOPT_TIMEOUT => 0,
35 CURLOPT_FOLLOWLOCATION => true,
36 CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
37 CURLOPT_CUSTOMREQUEST => "POST",
38 CURLOPT_POSTFIELDS =>"{\n\t\"limit\": ".$offset.",\n \"nodeCode\": \"".$categoryId."\",\n \"offset\": 0,\n \"onlyDiscounts\": false\n}",
39 CURLOPT_HTTPHEADER => array(
40 "Content-Type: application/json"
41 ),
42 ));
43
44 $response = curl_exec($curl);
45
46 curl_close($curl);
47 return json_decode($response,1);
48 }
49
50 private function requestGet($url)
51 {
52 $ch = curl_init();
53 curl_setopt($ch, CURLOPT_URL, $url);
54 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
55 curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
56 curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
57 $response = curl_exec($ch);
58 $result = json_decode($response, 1);
59 curl_close($ch); // Close the connection
60 return $result;
61 }
62}
63
64class grabber
65{
66 private $storeId;
67 private $Api;
68 private $stack;
69 private $categoryResult = [];
70
71 public function __construct($storeId)
72 {
73 $this->storeId = $storeId;
74 $this->Api = new Api($this->storeId);
75 }
76
77 public function init()
78 {
79 $categoriesForParse = [];
80 $categoriesAll = $this->Api->getCategories();
81 //получаем и приводим в нормальный вид категории
82 foreach ($categoriesAll as $key => $category) {
83 $this->getChildArray($category);
84 }
85 //парсим уже продукты
86 foreach ($this->categoryResult as $key => $category) {
87 $productList = [];
88 for($i = 0; $i <= $category['count']; $i += 15) {
89 $productListResponse = $this->Api->getProducts($category['id'] , $i)['skus'];
90 foreach ($productListResponse as $key => $product) {
91 $product = [
92 'title' => $product['title'],
93 'code' => $product['code'],
94 'price' => $product['regularPrice'],
95 'categories' => $category['hierarchy']
96 ];
97 $productList[] = $product;
98 }
99 }
100 echo "<pre>";
101 var_dump($productList);
102 die;
103
104
105 }
106
107 }
108
109 private function getChildArray($category, $parrentCode = null)
110 {
111 $this->stack[] = $category['name'];
112 if (isset($category['categories'])) {
113 foreach ($category['categories'] as $key => $value) {
114 $this->getChildArray($value, $category['code']);
115 }
116 array_pop($this->stack);
117 } else {
118 if (isset($category['subcategories'])) {
119 foreach ($category['subcategories'] as $key => $value) {
120 $this->getChildArray($value, $category['code']);
121 }
122 array_pop($this->stack);
123 } else {
124 $result = [
125 "hierarchy" => $this->stack,
126 "id" => $category['code'],
127 "count" => $category['skuCount']
128 ];
129 $this->categoryResult[] = $result;
130 array_pop($this->stack);
131 }
132 }
133 }
134
135}
136
137$grabber = new grabber('0154');
138$grabber->init();
139?>