How to screenshot a website/URL in PHP
Converting a URL into an image is a useful function made very simple with Cloudmersive.
To begin, we add the Convert API to our library:
"require": {
"cloudmersive/cloudmersive_document_convert_api_client": "^1.4",
}
Then simply call convert_web_url_to_screenshot and set the input as our desired URL.
<?php
require_once(__DIR__ . '/vendor/autoload.php');// Configure API key authorization: Apikey
$config = Swagger\Client\Configuration::getDefaultConfiguration()->setApiKey('Apikey', 'YOUR_API_KEY');$apiInstance = new Swagger\Client\Api\ConvertWebApi(
new GuzzleHttp\Client(),
$config
);
$input = new \Swagger\Client\Model\ScreenshotRequest(); // \Swagger\Client\Model\ScreenshotRequest | Screenshot request parameterstry {
$result = $apiInstance->convertWebUrlToScreenshot($input);
print_r($result);
} catch (Exception $e) {
echo 'Exception when calling ConvertWebApi->convertWebUrlToScreenshot: ', $e->getMessage(), PHP_EOL;
}
?>
And it’s as simple as that. Your URL is now a PNG file and you’re good to go.