Skip to content

Commit c24250d

Browse files
committed
Add Walmart Search Support
1 parent c08568b commit c24250d

File tree

4 files changed

+236
-0
lines changed

4 files changed

+236
-0
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,21 @@ Look at the source code of `src/LaravelScrapingBeeGoogleSearch.php` for the othe
9999

100100
[LaravelScrapingBeeGoogleSearch.php](https://github.com/ziming/laravel-scrapingbee/blob/main/src/LaravelScrapingBeeGoogleSearch.php)
101101

102+
### The Walmart Search ScrapingBee Client
103+
104+
```php
105+
$walmartSearchScrapingBeeClient = Ziming\LaravelScrapingBee\LaravelScrapingBeeWalmartSearch::make();
106+
107+
$response = $walmartSearchScrapingBeeClient
108+
->query('iPhone')
109+
->minPrice(100)
110+
->maxPrice(1000)
111+
->get();
112+
```
113+
Look at the source code of `src/LaravelScrapingBeeWalmartSearch.php` for the other methods (link below).
114+
115+
[LaravelScrapingBeeWalmartSearch.php](https://github.com/ziming/laravel-scrapingbee/blob/main/src/LaravelScrapingBeeWalmartSearch.php)
116+
102117
## Testing
103118

104119
Currently, there are no tests. But if there are tests in the future, you can run the command below to execute the testcases.

config/scrapingbee.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
'base_url' => env('SCRAPINGBEE_BASE_URL', 'https://app.scrapingbee.com/api/v1/'),
88
'timeout' => env('SCRAPINGBEE_TIMEOUT', 140),
99
'google_search_base_url' => env('SCRAPINGBEE_GOOGLE_SEARCH_BASE_URL', 'https://app.scrapingbee.com/api/v1/store/google'),
10+
'walmart_search_base_url' => env('SCRAPINGBEE_WALMART_SEARCH_BASE_URL', 'https://app.scrapingbee.com/api/v1/walmart/search'),
1011
];

src/LaravelScrapingBeeGoogleSearch.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Ziming\LaravelScrapingBee;
66

7+
use Illuminate\Http\Client\ConnectionException;
78
use Illuminate\Http\Client\Response;
89
use Illuminate\Support\Facades\Http;
910
use Illuminate\Support\Traits\Conditionable;
@@ -35,6 +36,9 @@ public function __construct(#[\SensitiveParameter] ?string $apiKey = null)
3536
);
3637
}
3738

39+
/**
40+
* @throws ConnectionException
41+
*/
3842
public function get(): Response
3943
{
4044
$this->params['api_key'] = $this->apiKey;
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Ziming\LaravelScrapingBee;
6+
7+
use Illuminate\Http\Client\ConnectionException;
8+
use Illuminate\Http\Client\Response;
9+
use Illuminate\Support\Facades\Http;
10+
use Illuminate\Support\Traits\Conditionable;
11+
12+
final class LaravelScrapingBeeWalmartSearch
13+
{
14+
use Conditionable;
15+
16+
private readonly string $baseUrl;
17+
private readonly string $apiKey;
18+
19+
private array $params = [];
20+
21+
public static function make(#[\SensitiveParameter] ?string $apiKey = null): self
22+
{
23+
return new self($apiKey);
24+
}
25+
26+
public function __construct(#[\SensitiveParameter] ?string $apiKey = null)
27+
{
28+
// If somebody pass '' into the constructor, we should use '' as the api key
29+
// even if it doesn't make sense.
30+
// If $apiKey is null, then we use the 1 in the config file.
31+
$this->apiKey = $apiKey ?? config('scrapingbee.api_key');
32+
33+
$this->baseUrl = config(
34+
'scrapingbee.walmart_search_base_url',
35+
'https://app.scrapingbee.com/api/v1/walmart/search'
36+
);
37+
}
38+
39+
/**
40+
* @throws ConnectionException
41+
*/
42+
public function get(): Response
43+
{
44+
$this->params['api_key'] = $this->apiKey;
45+
$response = Http::get($this->baseUrl, $this->params);
46+
$this->reset();
47+
48+
return $response;
49+
}
50+
51+
/**
52+
* https://www.scrapingbee.com/documentation/walmart/#light_request_WalmartAPISearch
53+
*/
54+
public function lightRequest(bool $lightRequest = true): self
55+
{
56+
$this->params['light_request'] = $lightRequest;
57+
58+
return $this;
59+
}
60+
61+
/**
62+
* https://www.scrapingbee.com/documentation/walmart/#query
63+
*/
64+
public function query(string $query): self
65+
{
66+
$this->params['query'] = $query;
67+
68+
return $this;
69+
}
70+
71+
/**
72+
* https://www.scrapingbee.com/documentation/walmart/#page
73+
*/
74+
public function page(int $page): self
75+
{
76+
$this->params['page'] = $page;
77+
78+
return $this;
79+
}
80+
81+
/**
82+
* https://www.scrapingbee.com/documentation/walmart/#min_price
83+
*/
84+
public function minPrice(int $minPrice): self
85+
{
86+
$this->params['min_price'] = $minPrice;
87+
88+
return $this;
89+
}
90+
91+
/**
92+
* https://www.scrapingbee.com/documentation/walmart/#max_price
93+
*/
94+
public function maxPrice(int $maxPrice): self
95+
{
96+
$this->params['max_price'] = $maxPrice;
97+
98+
return $this;
99+
}
100+
101+
/*
102+
* https://www.scrapingbee.com/documentation/walmart/#sort_by
103+
*/
104+
public function sortBy(string $sortBy): self
105+
{
106+
$this->params['sort_by'] = $sortBy;
107+
108+
return $this;
109+
}
110+
111+
/**
112+
* https://www.scrapingbee.com/documentation/walmart/#device_WalmartAPISearch
113+
*/
114+
public function device(string $device): self
115+
{
116+
$this->params['device'] = $device;
117+
118+
return $this;
119+
}
120+
121+
/**
122+
* https://www.scrapingbee.com/documentation/walmart/#domain_WalmartAPISearch
123+
*/
124+
public function domain(string $domain): self
125+
{
126+
$this->params['domain'] = $domain;
127+
128+
return $this;
129+
}
130+
131+
/*
132+
* https://www.scrapingbee.com/documentation/walmart/#domain_WalmartAPISearch
133+
*/
134+
public function fulfillmentSpeed(string $fulfillmentSpeed): self
135+
{
136+
$this->params['fulfillment_speed'] = $fulfillmentSpeed;
137+
138+
return $this;
139+
}
140+
141+
/**
142+
* https://www.scrapingbee.com/documentation/walmart/#fulfillment_type
143+
*/
144+
public function fulfillmentType(string $fulfillmentType): self
145+
{
146+
$this->params['fulfillment_type'] = $fulfillmentType;
147+
148+
return $this;
149+
}
150+
151+
/**
152+
* https://www.scrapingbee.com/documentation/walmart/#delivery_zip
153+
*/
154+
public function deliveryZip(string $deliveryZip): self
155+
{
156+
$this->params['delivery_zip'] = $deliveryZip;
157+
158+
return $this;
159+
}
160+
161+
/**
162+
* https://www.scrapingbee.com/documentation/walmart/#store_id
163+
*/
164+
public function storeId(string $storeId): self
165+
{
166+
$this->params['store_id'] = $storeId;
167+
168+
return $this;
169+
}
170+
171+
public function addHtml(): self
172+
{
173+
$this->params['add_html'] = true;
174+
175+
return $this;
176+
}
177+
178+
179+
/**
180+
* https://www.scrapingbee.com/documentation/google/#extra_params
181+
*/
182+
public function extraParams(array $extraParams): self
183+
{
184+
$this->params['extra_params'] = http_build_query($extraParams);
185+
186+
return $this;
187+
}
188+
189+
/**
190+
* https://www.scrapingbee.com/documentation/google/#add_html
191+
*/
192+
public function addHtml(): self
193+
{
194+
$this->params['add_html'] = true;
195+
196+
return $this;
197+
}
198+
199+
/*
200+
* If the API hasn't caught up and you need to support a new ScrapingBee parameter,
201+
* you can set it using this method.
202+
*/
203+
public function setParam(string $key, mixed $value): self
204+
{
205+
$this->params[$key] = $value;
206+
207+
return $this;
208+
}
209+
210+
private function reset(): self
211+
{
212+
$this->params = [];
213+
214+
return $this;
215+
}
216+
}

0 commit comments

Comments
 (0)