Skip to content

Commit e7a88ec

Browse files
committed
Add Walmart Product Support
1 parent 7ac40f7 commit e7a88ec

File tree

6 files changed

+164
-7
lines changed

6 files changed

+164
-7
lines changed

README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,35 @@ $response = $walmartSearchScrapingBeeClient
111111
->query('iPhone')
112112
->minPrice(100)
113113
->maxPrice(1000)
114+
->fulfillmentType('in_store')
115+
->fulfillmentSpeed('today')
116+
->domain('walmart.ca')
117+
->sortBy('price_low')
118+
->page(2)
114119
->get();
115120
```
116121
Look at the source code of `src/LaravelScrapingBeeWalmartSearch.php` for the other methods (link below).
117122

118123
[LaravelScrapingBeeWalmartSearch.php](https://github.com/ziming/laravel-scrapingbee/blob/main/src/LaravelScrapingBeeWalmartSearch.php)
119124

125+
126+
### The Walmart Product ScrapingBee Client
127+
128+
```php
129+
$walmartProductScrapingBeeClient = Ziming\LaravelScrapingBee\LaravelScrapingBeeWalmartProduct::make();
130+
131+
$response = $walmartProductScrapingBeeClient
132+
->productId('5491199371')
133+
->domain('walmart.ca')
134+
->deliveryZip('10001')
135+
->storeId('3520')
136+
->get();
137+
```
138+
Look at the source code of `src/LaravelScrapingBeeWalmartProduct.php` for the other methods (link below).
139+
140+
[LaravelScrapingBeeWalmartProduct.php](https://github.com/ziming/laravel-scrapingbee/blob/main/src/LaravelScrapingBeeWalmartProduct.php)
141+
142+
120143
## Testing
121144

122145
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
'api_key' => env('SCRAPINGBEE_API_KEY'),
77
'base_url' => env('SCRAPINGBEE_BASE_URL', 'https://app.scrapingbee.com/api/v1/'),
88
'timeout' => env('SCRAPINGBEE_TIMEOUT', 140),
9+
910
'google_search_base_url' => env('SCRAPINGBEE_GOOGLE_SEARCH_BASE_URL', 'https://app.scrapingbee.com/api/v1/store/google'),
11+
1012
'walmart_search_base_url' => env('SCRAPINGBEE_WALMART_SEARCH_BASE_URL', 'https://app.scrapingbee.com/api/v1/walmart/search'),
13+
'walmart_product_base_url' => env('SCRAPINGBEE_WALMART_PRODUCT_BASE_URL', 'https://app.scrapingbee.com/api/v1/walmart/product'),
1114
];

src/LaravelScrapingBee.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
namespace Ziming\LaravelScrapingBee;
64

75
use Illuminate\Http\Client\ConnectionException;

src/LaravelScrapingBeeGoogleSearch.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
namespace Ziming\LaravelScrapingBee;
64

75
use Illuminate\Http\Client\ConnectionException;
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
3+
namespace Ziming\LaravelScrapingBee;
4+
5+
use Illuminate\Http\Client\ConnectionException;
6+
use Illuminate\Http\Client\Response;
7+
use Illuminate\Support\Facades\Http;
8+
use Illuminate\Support\Traits\Conditionable;
9+
10+
final class LaravelScrapingBeeWalmartProduct
11+
{
12+
use Conditionable;
13+
14+
private readonly string $baseUrl;
15+
private readonly string $apiKey;
16+
17+
private array $params = [];
18+
19+
public static function make(#[\SensitiveParameter] ?string $apiKey = null): self
20+
{
21+
return new self($apiKey);
22+
}
23+
24+
public function __construct(#[\SensitiveParameter] ?string $apiKey = null)
25+
{
26+
// If somebody pass '' into the constructor, we should use '' as the api key
27+
// even if it doesn't make sense.
28+
// If $apiKey is null, then we use the 1 in the config file.
29+
$this->apiKey = $apiKey ?? config('scrapingbee.api_key');
30+
31+
$this->baseUrl = config(
32+
'scrapingbee.walmart_product_base_url',
33+
'https://app.scrapingbee.com/api/v1/walmart/product'
34+
);
35+
}
36+
37+
/**
38+
* @throws ConnectionException
39+
*/
40+
public function get(): Response
41+
{
42+
$this->params['api_key'] = $this->apiKey;
43+
$response = Http::get($this->baseUrl, $this->params);
44+
$this->reset();
45+
46+
return $response;
47+
}
48+
49+
/**
50+
* https://www.scrapingbee.com/documentation/walmart/#light_request_WalmartAPIProduct
51+
*/
52+
public function lightRequest(bool $lightRequest = true): self
53+
{
54+
$this->params['light_request'] = $lightRequest;
55+
56+
return $this;
57+
}
58+
59+
/**
60+
* https://www.scrapingbee.com/documentation/walmart/#product_id
61+
*/
62+
public function productId(string $storeId): self
63+
{
64+
$this->params['product_id'] = $storeId;
65+
66+
return $this;
67+
}
68+
69+
70+
/**
71+
* https://www.scrapingbee.com/documentation/walmart/#domain_WalmartAPIProduct
72+
*/
73+
public function domain(string $domain): self
74+
{
75+
$this->params['domain'] = $domain;
76+
77+
return $this;
78+
}
79+
80+
/**
81+
* https://www.scrapingbee.com/documentation/walmart/#delivery_zip_WalmartAPIProduct
82+
*/
83+
public function deliveryZip(string $deliveryZip): self
84+
{
85+
$this->params['delivery_zip'] = $deliveryZip;
86+
87+
return $this;
88+
}
89+
90+
/**
91+
* https://www.scrapingbee.com/documentation/walmart/#store_id_WalmartAPIProduct
92+
*/
93+
public function storeId(string $storeId): self
94+
{
95+
$this->params['store_id'] = $storeId;
96+
97+
return $this;
98+
}
99+
100+
/**
101+
* https://www.scrapingbee.com/documentation/google/#extra_params
102+
*/
103+
public function extraParams(array $extraParams): self
104+
{
105+
$this->params['extra_params'] = http_build_query($extraParams);
106+
107+
return $this;
108+
}
109+
110+
/**
111+
* https://www.scrapingbee.com/documentation/walmart/#add_html_WalmartAPIProduct
112+
*/
113+
public function addHtml(): self
114+
{
115+
$this->params['add_html'] = true;
116+
117+
return $this;
118+
}
119+
120+
/*
121+
* If the API hasn't caught up and you need to support a new ScrapingBee parameter,
122+
* you can set it using this method.
123+
*/
124+
public function setParam(string $key, mixed $value): self
125+
{
126+
$this->params[$key] = $value;
127+
128+
return $this;
129+
}
130+
131+
private function reset(): self
132+
{
133+
$this->params = [];
134+
135+
return $this;
136+
}
137+
}

src/LaravelScrapingBeeWalmartSearch.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
<?php
22

3-
declare(strict_types=1);
4-
53
namespace Ziming\LaravelScrapingBee;
64

75
use Illuminate\Http\Client\ConnectionException;
@@ -179,7 +177,7 @@ public function extraParams(array $extraParams): self
179177
}
180178

181179
/**
182-
* https://www.scrapingbee.com/documentation/google/#add_html
180+
* https://www.scrapingbee.com/documentation/walmart/#add_html_WalmartAPISearch
183181
*/
184182
public function addHtml(): self
185183
{

0 commit comments

Comments
 (0)