Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ return [
'timeout' => env('SCRAPINGBEE_TIMEOUT', 140),

'google_search_base_url' => env('SCRAPINGBEE_GOOGLE_SEARCH_BASE_URL', 'https://app.scrapingbee.com/api/v1/store/google'),
'fast_search_base_url' => env('SCRAPINGBEE_FAST_SEARCH_BASE_URL', 'https://app.scrapingbee.com/api/v1/fast_search'),

'walmart_search_base_url' => env('SCRAPINGBEE_WALMART_SEARCH_BASE_URL', 'https://app.scrapingbee.com/api/v1/walmart/search'),
'walmart_product_base_url' => env('SCRAPINGBEE_WALMART_PRODUCT_BASE_URL', 'https://app.scrapingbee.com/api/v1/walmart/product'),
Expand Down Expand Up @@ -107,6 +108,22 @@ Look at the source code of `src/LaravelScrapingBeeGoogleSearch.php` for the othe

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

### The Fast Search ScrapingBee Client

```php
$fastSearchScrapingBeeClient = Ziming\LaravelScrapingBee\LaravelScrapingBeeFastSearch::make();

$response = $fastSearchScrapingBeeClient
->search('pizza in new york')
->countryCode('us')
->language('en')
->page(1)
->get();
```
Look at the source code of `src/LaravelScrapingBeeFastSearch.php` for the other methods (link below).

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

### Walmart ScrapingBee Clients

#### The Walmart Search ScrapingBee Client
Expand Down
1 change: 1 addition & 0 deletions config/scrapingbee.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
'timeout' => env('SCRAPINGBEE_TIMEOUT', 140),

'google_search_base_url' => env('SCRAPINGBEE_GOOGLE_SEARCH_BASE_URL', 'https://app.scrapingbee.com/api/v1/store/google'),
'fast_search_base_url' => env('SCRAPINGBEE_FAST_SEARCH_BASE_URL', 'https://app.scrapingbee.com/api/v1/fast_search'),

'walmart_search_base_url' => env('SCRAPINGBEE_WALMART_SEARCH_BASE_URL', 'https://app.scrapingbee.com/api/v1/walmart/search'),
'walmart_product_base_url' => env('SCRAPINGBEE_WALMART_PRODUCT_BASE_URL', 'https://app.scrapingbee.com/api/v1/walmart/product'),
Expand Down
118 changes: 118 additions & 0 deletions src/LaravelScrapingBeeFastSearch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

declare(strict_types=1);

namespace Ziming\LaravelScrapingBee;

use Illuminate\Http\Client\ConnectionException;
use Illuminate\Http\Client\Response;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Traits\Conditionable;

final class LaravelScrapingBeeFastSearch
{
use Conditionable;

private readonly string $baseUrl;
private readonly string $apiKey;

private array $params = [];

public static function make(#[\SensitiveParameter] ?string $apiKey = null): self
{
return new self($apiKey);
}

public function __construct(#[\SensitiveParameter] ?string $apiKey = null)
{
// If somebody pass '' into the constructor, we should use '' as the api key
// even if it doesn't make sense.
// If $apiKey is null, then we use the 1 in the config file.
$this->apiKey = $apiKey ?? config('scrapingbee.api_key');

$this->baseUrl = config(
'scrapingbee.fast_search_base_url',
'https://app.scrapingbee.com/api/v1/fast_search'
);
}

/**
* @throws ConnectionException
*/
public function get(): Response
{
$response = Http::withToken($this->apiKey)->get($this->baseUrl, $this->params);
$this->reset();

return $response;
}

/**
* required
* https://www.scrapingbee.com/documentation/fast-search/?fpr=php-laravel#search
*/
public function search(string $query): self
{
$this->params['search'] = $query;

return $this;
}

/**
* https://www.scrapingbee.com/documentation/fast-search/?fpr=php-laravel#country_code
*/
public function countryCode(string $countryCode): self
{
$this->params['country_code'] = $countryCode;

return $this;
}

/**
* https://www.scrapingbee.com/documentation/fast-search/?fpr=php-laravel#language
*/
public function language(string $language): self
{
$this->params['language'] = $language;

return $this;
}

/**
* https://www.scrapingbee.com/documentation/fast-search/?fpr=php-laravel#page
*/
public function page(int $page): self
{
$this->params['page'] = $page;

return $this;
}

/**
* https://www.scrapingbee.com/documentation/fast-search/?fpr=php-laravel#tag
*/
public function tag(string $tag): self
{
$this->params['tag'] = $tag;

return $this;
}

/*
* If the API hasn't caught up, and you need to support a new ScrapingBee parameter,
* you can set it using this method.
*/
public function setParam(string $key, mixed $value): self
{
$this->params[$key] = $value;

return $this;
}

private function reset(): self
{
$this->params = [];

return $this;
}
}
46 changes: 46 additions & 0 deletions tests/LaravelScrapingBeeFastSearchTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Ziming\LaravelScrapingBee\Tests;

use Illuminate\Http\Client\Request;
use Illuminate\Support\Facades\Http;
use Ziming\LaravelScrapingBee\LaravelScrapingBeeFastSearch;

class LaravelScrapingBeeFastSearchTest extends TestCase
{
public function test_it_sends_fast_search_request_with_bearer_token(): void
{
config()->set('scrapingbee.api_key', 'test-api-key');
config()->set('scrapingbee.fast_search_base_url', 'https://example.test/fast_search');

Http::fake([
'*' => Http::response(['status' => 'done']),
]);

$response = LaravelScrapingBeeFastSearch::make()
->search('pizza in new york')
->countryCode('us')
->language('en')
->page(2)
->tag('docs-example')
->get();

$this->assertSame(['status' => 'done'], $response->json());

Http::assertSent(function (Request $request): bool {
parse_str((string) parse_url($request->url(), PHP_URL_QUERY), $query);

return str_starts_with($request->url(), 'https://example.test/fast_search?')
&& $request->hasHeader('Authorization', 'Bearer test-api-key')
&& $query === [
'search' => 'pizza in new york',
'country_code' => 'us',
'language' => 'en',
'page' => '2',
'tag' => 'docs-example',
];
});
}
}