Skip to content

Commit 6b54aef

Browse files
authored
5.1.0
1 parent 9e28470 commit 6b54aef

File tree

1 file changed

+163
-0
lines changed

1 file changed

+163
-0
lines changed

src/AllegroScraper.php

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
<?php
2+
3+
namespace AllegroScraper;
4+
5+
/**
6+
* Extractor (scraper, crawler, parser) of products from Allegro.
7+
* It receives the search output of Allegro.pl, as well as detailed information about the products.
8+
*
9+
* @see https://www.parser.best/en/allegro-scraper-extractor.php
10+
* @author Telegram: @JWprogrammer
11+
* @copyright Telegram: @JWprogrammer
12+
*/
13+
14+
class AllegroScraper
15+
{
16+
private string $db_host;
17+
private string $db_name;
18+
private string $db_user;
19+
private string $db_password;
20+
21+
private bool $remote_access_exists;
22+
private string $remote_uri;
23+
private string $remote_api_key;
24+
25+
public function __construct(string $db_host, string $db_name, string $db_user, string $db_password, string $remote_uri = null, string $remote_api_key = null)
26+
{
27+
$this->db_host = $db_host;
28+
$this->db_name = $db_name;
29+
$this->db_user = $db_user;
30+
$this->db_password = $db_password;
31+
32+
if (!empty($remote_uri) && !empty($remote_api_key)){
33+
$this->remote_access_exists = true;
34+
$this->remote_uri = $remote_uri;
35+
$this->remote_api_key = $remote_api_key;
36+
}
37+
else {
38+
$this->remote_access_exists = false;
39+
}
40+
}
41+
42+
/**
43+
* Get search results from Allegro
44+
* @param array $parameters
45+
* @param bool $return_as_object
46+
* @return array|object
47+
*/
48+
public function search(array $parameters, bool $return_as_object = true)
49+
{
50+
if ($this->isOnThisServer()){
51+
return $this->localRequest(__FUNCTION__, $return_as_object, $parameters['language'] ?? '', $parameters['page'] ?? 1, $parameters['category'] ?? null, $parameters['query'] ?? null, $parameters['sort'] ?? null, $parameters['status'] ?? null, $parameters['make'] ?? null, $parameters['model'] ?? null, $parameters['type'] ?? null, $parameters['price_from'] ?? null, $parameters['price_to'] ?? null, $parameters['filters'] ?? [], $parameters['seller_id'] ?? '', $parameters['seller_name'] ?? '', $parameters['show_lokalnie'] ?? false, $parameters['show_filters'] ?? false, $parameters['filters_excluded'] ?? []);
52+
}
53+
else {
54+
return $this->remoteRequest(__FUNCTION__, $return_as_object, $parameters);
55+
}
56+
}
57+
58+
/**
59+
* Get detailed product information from Allegro
60+
* @param array $parameters
61+
* @param bool $return_as_object
62+
* @return array|object
63+
*/
64+
public function details(array $parameters, bool $return_as_object = true)
65+
{
66+
if ($this->isOnThisServer()){
67+
return $this->localRequest(__FUNCTION__, $return_as_object, $parameters['language'] ?? '', $parameters['product_id'] ?? null);
68+
}
69+
else {
70+
return $this->remoteRequest(__FUNCTION__, $return_as_object, $parameters);
71+
}
72+
}
73+
74+
/**
75+
* Get a list of categories from Allegro
76+
* @param array $parameters
77+
* @param bool $return_as_object
78+
* @return array|object
79+
*/
80+
public function categories(array $parameters, bool $return_as_object = true)
81+
{
82+
if ($this->isOnThisServer()){
83+
return $this->localRequest(__FUNCTION__, $return_as_object, $parameters['language'] ?? '', $parameters['category'] ?? null);
84+
}
85+
else {
86+
return $this->remoteRequest(__FUNCTION__, $return_as_object, $parameters);
87+
}
88+
}
89+
90+
private function remoteRequest(string $method, bool $return_as_object, array $parameters)
91+
{
92+
$parameters['api_key'] = $this->remote_api_key;
93+
$parameters['method'] = $method;
94+
$curl = curl_init();
95+
curl_setopt_array($curl, [
96+
CURLOPT_URL => $this->remote_uri . '?' . http_build_query($parameters),
97+
CURLOPT_RETURNTRANSFER => true,
98+
CURLOPT_ENCODING => '',
99+
CURLOPT_MAXREDIRS => 5,
100+
CURLOPT_TIMEOUT => 360,
101+
CURLOPT_FOLLOWLOCATION => true,
102+
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
103+
CURLOPT_USERAGENT => $_SERVER['HTTP_HOST'],
104+
CURLOPT_CUSTOMREQUEST => 'GET',
105+
CURLOPT_SSL_VERIFYHOST => false,
106+
CURLOPT_SSL_VERIFYPEER => false
107+
]);
108+
$response = curl_exec($curl);
109+
$error = curl_error($curl);
110+
curl_close($curl);
111+
if ($error){
112+
throw new \RuntimeException($error);
113+
}
114+
$result = json_decode($response, !$return_as_object);
115+
if ($result == null){
116+
throw new \RuntimeException('Error while request with response: "'. $response . '"');
117+
}
118+
return $result;
119+
}
120+
121+
private function localRequest(string $method, bool $return_as_object, $language, ...$parameters)
122+
{
123+
$timezone_old = ini_set('date.timezone', 'Europe/Kiev');
124+
$array = (new \AllegroScraper\AllegroV4($this->db_host, $this->db_name, $this->db_user, $this->db_password, $language))->{$method}(...$parameters);
125+
ini_set('date.timezone', $timezone_old ?? 'Europe/Kiev');
126+
if ($return_as_object){
127+
return self::responseToObject($array);
128+
}
129+
else return $array;
130+
}
131+
132+
private function isOnThisServer()
133+
{
134+
if (class_exists('\AllegroScraper\AllegroV4')) {
135+
return true;
136+
}
137+
elseif ($this->remote_access_exists){
138+
return false;
139+
}
140+
else {
141+
throw new \RuntimeException('The Allegro scraper is not installed on your server. Please contact: https://t.me/JWprogrammer');
142+
}
143+
}
144+
145+
private static function responseToObject(array &$array)
146+
{
147+
$result_obj = new \stdClass();
148+
$result_arr = [];
149+
$has_str_keys = false;
150+
foreach ($array as $k => &$v) {
151+
if (!$has_str_keys) {
152+
$has_str_keys = is_string($k);
153+
}
154+
$object = is_array($v) ? self::responseToObject($v) : $v;
155+
if (!$has_str_keys) {
156+
$result_arr[$k] = $object;
157+
}
158+
$result_obj->{$k} = $object;
159+
$v = null;
160+
}
161+
return ($has_str_keys) ? $result_obj : $result_arr;
162+
}
163+
}

0 commit comments

Comments
 (0)