Developer-friendly & type-safe Php SDK specifically catered to leverage screenshothis/php API.
Screenshothis API: API designed to take screenshots of websites
The SDK relies on Composer to manage its dependencies.
To install the SDK and add it as a dependency to an existing composer.json file:
composer require "screenshothis/php"declare(strict_types=1);
require 'vendor/autoload.php';
use Screenshothis\Screenshothis;
use Screenshothis\Screenshothis\Models\Operations;
$sdk = Screenshothis\Screenshothis::builder()->build();
$request = new Operations\TakeScreenshotRequest(
apiKey: 'sk_live_abcdef1234567890abcdef1234567890',
url: 'https://example.com',
selector: '.main-content',
blockRequests: '*.doubleclick.net\n' .
'*.googletagmanager.com\n' .
'*/analytics/*',
blockResources: [
Operations\BlockResource::Script,
Operations\BlockResource::Stylesheet,
Operations\BlockResource::Font,
],
cacheKey: 'homepage-desktop-light',
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
headers: 'User-Agent: MyBot/1.0\n' .
'Authorization: Bearer token123\n' .
'X-Custom-Header: value',
cookies: 'session_id=abc123; Domain=example.com; Path=/; Secure\n' .
'user_pref=dark_mode; Max-Age=3600',
);
$response = $sdk->takeScreenshot(
request: $request
);
if ($response->twoHundredImageJpegBytes !== null) {
// handle response
}Available methods
- takeScreenshot - Generate optimized website screenshot
- health - Comprehensive health check
- ready - Readiness probe
- live - Liveness probe
Handling errors in this SDK should largely match your expectations. All operations return a response object or throw an exception.
By default an API error will raise a Errors\APIException exception, which has the following properties:
| Property | Type | Description |
|---|---|---|
$message |
string | The error message |
$statusCode |
int | The HTTP status code |
$rawResponse |
?\Psr\Http\Message\ResponseInterface | The raw HTTP response |
$body |
string | The response content |
When custom error responses are specified for an operation, the SDK may also throw their associated exception. You can refer to respective Errors tables in SDK docs for more details on possible exception types for each operation. For example, the takeScreenshot method throws the following exceptions:
| Error Type | Status Code | Content Type |
|---|---|---|
| Errors\ForbiddenException | 403 | application/json |
| Errors\InternalServerError | 500 | application/json |
| Errors\APIException | 4XX, 5XX | */* |
declare(strict_types=1);
require 'vendor/autoload.php';
use Screenshothis\Screenshothis;
use Screenshothis\Screenshothis\Models\Errors;
use Screenshothis\Screenshothis\Models\Operations;
$sdk = Screenshothis\Screenshothis::builder()->build();
try {
$request = new Operations\TakeScreenshotRequest(
apiKey: 'sk_live_abcdef1234567890abcdef1234567890',
url: 'https://example.com',
selector: '.main-content',
blockRequests: '*.doubleclick.net\n' .
'*.googletagmanager.com\n' .
'*/analytics/*',
blockResources: [
Operations\BlockResource::Script,
Operations\BlockResource::Stylesheet,
Operations\BlockResource::Font,
],
cacheKey: 'homepage-desktop-light',
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
headers: 'User-Agent: MyBot/1.0\n' .
'Authorization: Bearer token123\n' .
'X-Custom-Header: value',
cookies: 'session_id=abc123; Domain=example.com; Path=/; Secure\n' .
'user_pref=dark_mode; Max-Age=3600',
);
$response = $sdk->takeScreenshot(
request: $request
);
if ($response->twoHundredImageJpegBytes !== null) {
// handle response
}
} catch (Errors\ForbiddenExceptionThrowable $e) {
// handle $e->$container data
throw $e;
} catch (Errors\InternalServerErrorThrowable $e) {
// handle $e->$container data
throw $e;
} catch (Errors\APIException $e) {
// handle default exception
throw $e;
}The default server can be overridden globally using the setServerUrl(string $serverUrl) builder method when initializing the SDK client instance. For example:
declare(strict_types=1);
require 'vendor/autoload.php';
use Screenshothis\Screenshothis;
use Screenshothis\Screenshothis\Models\Operations;
$sdk = Screenshothis\Screenshothis::builder()
->setServerURL('https://api.screenshothis.com')
->build();
$request = new Operations\TakeScreenshotRequest(
apiKey: 'sk_live_abcdef1234567890abcdef1234567890',
url: 'https://example.com',
selector: '.main-content',
blockRequests: '*.doubleclick.net\n' .
'*.googletagmanager.com\n' .
'*/analytics/*',
blockResources: [
Operations\BlockResource::Script,
Operations\BlockResource::Stylesheet,
Operations\BlockResource::Font,
],
cacheKey: 'homepage-desktop-light',
userAgent: 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
headers: 'User-Agent: MyBot/1.0\n' .
'Authorization: Bearer token123\n' .
'X-Custom-Header: value',
cookies: 'session_id=abc123; Domain=example.com; Path=/; Secure\n' .
'user_pref=dark_mode; Max-Age=3600',
);
$response = $sdk->takeScreenshot(
request: $request
);
if ($response->twoHundredImageJpegBytes !== null) {
// handle response
}This SDK is in beta, and there may be breaking changes between versions without a major version update. Therefore, we recommend pinning usage to a specific package version. This way, you can install the same version each time without breaking changes unless you are intentionally looking for the latest version.
While we value open-source contributions to this SDK, this library is generated programmatically. Any manual changes added to internal files will be overwritten on the next generation. We look forward to hearing your feedback. Feel free to open a PR or an issue with a proof of concept and we'll do our best to include it in a future release.