Skip to content

Commit f86b7c7

Browse files
committed
Add Scrapingbee ChatGpt client
1 parent 905350e commit f86b7c7

File tree

3 files changed

+120
-0
lines changed

3 files changed

+120
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,20 @@ $response = $amazonProductScrapingBeeClient
183183
Look at the source code of `src/LaravelScrapingBeeAmazonProduct.php` for the other methods (link below).
184184
[LaravelScrapingBeeAmazonProduct.php](https://github.com/ziming/laravel-scrapingbee/blob/main/src/LaravelScrapingBeeAmazonProduct.php)
185185

186+
### ChatGPT ScrapingBee Client
187+
188+
```php
189+
$chatGptScrapingbeeClient = Ziming\LaravelScrapingBee\LaravelScrapingBeeChatGpt::make();
190+
191+
$response = $chatGptScrapingbeeClient
192+
->prompt('Explain why a career in web development is a bad choice')
193+
->get();
194+
```
195+
Look at the source code of `src/LaravelScrapingBeeChatGpt.php` for the other methods (link below).
196+
197+
[LaravelScrapingBeeChatGpt.php](https://github.com/ziming/laravel-scrapingbee/blob/main/src/LaravelScrapingBeeChatGpt.php)
198+
199+
186200
## Testing
187201

188202
Currently, there are no tests as it uses credits. But if there are tests in the future, you can run the command below to execute the testcases.

config/scrapingbee.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,10 @@
1414

1515
'amazon_search_base_url' => env('SCRAPINGBEE_AMAZON_SEARCH_BASE_URL', 'https://app.scrapingbee.com/api/v1/amazon/search'),
1616
'amazon_product_base_url' => env('SCRAPINGBEE_AMAZON_PRODUCT_BASE_URL', 'https://app.scrapingbee.com/api/v1/amazon/product'),
17+
18+
19+
// YouTube will come soon, but not yet
20+
// 'youtube_search_base_url' => env('SCRAPINGBEE_YOUTUBE_SEARCH_BASE_URL', 'https://app.scrapingbee.com/api/v1/youtube/search'),
21+
22+
'chatgpt_base_url' => env('SCRAPINGBEE_CHATGPT_BASE_URL', 'https://app.scrapingbee.com/api/v1/chatgpt'),
1723
];

src/LaravelScrapingBeeChatGpt.php

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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+
use JsonException;
12+
13+
final class LaravelScrapingBeeChatGpt
14+
{
15+
use Conditionable;
16+
17+
private readonly string $baseUrl;
18+
private readonly string $apiKey;
19+
private readonly int $timeout;
20+
21+
private array $params = [];
22+
private array $headers = [];
23+
24+
public static function make(#[\SensitiveParameter] ?string $apiKey = null, ?int $timeout = null): self
25+
{
26+
return new self($apiKey, $timeout);
27+
}
28+
29+
public function __construct(#[\SensitiveParameter] ?string $apiKey = null, ?int $timeout = null)
30+
{
31+
// If somebody pass '' into the constructor, we should use '' as the api key
32+
// even if it doesn't make sense.
33+
// If $apiKey is null, then we use the 1 in the config file.
34+
$this->apiKey = $apiKey ?? config('scrapingbee.api_key');
35+
$this->timeout = $timeout ?? config('scrapingbee.timeout');
36+
37+
$this->baseUrl = config(
38+
'scrapingbee.chatgpt_base_url',
39+
'https://app.scrapingbee.com/api/v1/chatgpt'
40+
);
41+
}
42+
43+
/**
44+
* @throws ConnectionException
45+
*/
46+
private function request(string $method, string $url, array $data = [], string $postContentType = 'application/x-www-form-urlencoded; charset=utf-8'): Response
47+
{
48+
// https://www.scrapingbee.com/documentation/#encoding-url
49+
// urlencode($url) make things fail somehow.
50+
// My guess is urlencode is run at the Http::get() / Http::post() level already
51+
$this->params['url'] = $url;
52+
53+
$this->params['api_key'] = $this->apiKey;
54+
55+
$http = Http::withHeaders($this->headers)->timeout($this->timeout);
56+
57+
$response = $http->get($this->baseUrl, $this->params);
58+
59+
// Reset the params and headers
60+
$this->reset();
61+
62+
return $response;
63+
}
64+
65+
/**
66+
* @throws ConnectionException
67+
*/
68+
public function get(string $url): Response
69+
{
70+
return $this->request('GET', $url);
71+
}
72+
73+
/**
74+
* https://www.scrapingbee.com/documentation/chatgpt/?fpr=php-laravel#prompt
75+
*/
76+
public function prompt(string $prompt): self
77+
{
78+
$this->params['prompt'] = $prompt;
79+
80+
return $this;
81+
}
82+
83+
/*
84+
* If the API hasn't caught up, and you need to support a new ScrapingBee parameter,
85+
* you can set it using this method.
86+
*/
87+
public function setParam(string $key, mixed $value): self
88+
{
89+
$this->params[$key] = $value;
90+
91+
return $this;
92+
}
93+
94+
private function reset(): self
95+
{
96+
$this->params = [];
97+
98+
return $this;
99+
}
100+
}

0 commit comments

Comments
 (0)