|
| 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