screenshotmachine/screenshotmachine-php
使用Screenshot machine捕获网站截图,并将网站转换为PDF - 在线网站截图生成器和网站到PDF转换器。
2.0.0
2019-08-26 08:53 UTC
This package is auto-updated.
Last update: 2024-09-26 20:53:52 UTC
README
使用Screenshot machine捕获网站截图,并将网站转换为PDF - 在线网站截图生成器和网站到PDF转换器。
安装
a) 使用Composer - PHP的包管理器
php composer.phar require screenshotmachine/screenshotmachine-php
b) 或者直接从GitHub.com下载源代码
首先,您需要在www.screenshotmachine.com网站上创建一个免费/付费账户。注册后,您将在用户资料中看到您的客户密钥。同时,密语也在这里维护。请始终使用密语,当您的API调用来自公开可用的网站时。
在脚本中设置您的客户密钥和密语(如果需要)
$customer_key = "PUT_YOUR_CUSTOMER_KEY_HERE"; $secret_phrase = ""; //leave secret phrase empty, if not needed
网站截图API
设置附加选项以满足您的需求
//mandatory parameter $options['url'] = "https://www.google.com"; // all next parameters are optional, see our website screenshot API guide for more details $options['dimension'] = "1366x768"; // or "1366xfull" for full length screenshot $options['device'] = "desktop"; $options['format'] = "png"; $options['cacheLimit'] = "0"; $options['delay'] = "200"; $options['zoom'] = "100";
有关选项的更多信息,请参阅我们的网站截图API。
示例代码
<?php $customer_key = "PUT_YOUR_CUSTOMER_KEY_HERE"; $secret_phrase = ""; //leave secret phrase empty, if not needed $machine = new ScreenshotMachine($customer_key, $secret_phrase); //mandatory parameter $options['url'] = "https://www.google.com"; // all next parameters are optional, see our website screenshot API guide for more details $options['dimension'] = "1366x768"; // or "1366xfull" for full length screenshot $options['device'] = "desktop"; $options['format'] = "png"; $options['cacheLimit'] = "0"; $options['delay'] = "200"; $options['zoom'] = "100"; $api_url = $machine->generate_screenshot_api_url($options); //put link to your html code echo '<img src="' . $api_url . '">' . PHP_EOL;
生成的api_url
链接可以放置在<img>
标签中或以后用于您的业务逻辑。
如果您需要将捕获的截图作为图像存储,只需调用
<?php $customer_key = "PUT_YOUR_CUSTOMER_KEY_HERE"; $secret_phrase = ""; //leave secret phrase empty, if not needed $machine = new ScreenshotMachine($customer_key, $secret_phrase); //mandatory parameter $options['url'] = "https://www.google.com"; // all next parameters are optional, see our website screenshot API guide for more details $options['dimension'] = "1366x768"; // or "1366xfull" for full length screenshot $options['device'] = "desktop"; $options['format'] = "png"; $options['cacheLimit'] = "0"; $options['delay'] = "200"; $options['zoom'] = "100"; $api_url = $machine->generate_screenshot_api_url($options); //save screenshot as an image $output_file = 'output.png'; file_put_contents($output_file, file_get_contents($api_url)); echo 'Screenshot saved as ' . $output_file . PHP_EOL;
捕获的截图将作为当前目录中的output.png
文件保存。
网站到PDF API
设置PDF选项
//mandatory parameter $options['url'] = "https://www.google.com"; // all next parameters are optional, see our website to PDF API guide for more details $options['paper'] = "letter"; $options['orientation'] = "portrait"; $options['media'] = "print"; $options['bg'] = "nobg"; $options['delay'] = "2000"; $options['scale'] = "50";
有关选项的更多信息,请参阅我们的网站到PDF API。
示例代码
$customer_key = "PUT_YOUR_CUSTOMER_KEY_HERE"; $secret_phrase = ""; //leave secret phrase empty, if not needed $machine = new ScreenshotMachine($customer_key, $secret_phrase); //mandatory parameter $options['url'] = "https://www.google.com"; // all next parameters are optional, see our website to PDF API guide for more details $options['paper'] = "letter"; $options['orientation'] = "portrait"; $options['media'] = "print"; $options['bg'] = "nobg"; $options['delay'] = "2000"; $options['scale'] = "50"; $pdf_api_url = $machine->generate_pdf_api_url($options); //save PDF file $output_file = 'output.pdf'; file_put_contents($output_file, file_get_contents($pdf_api_url)); echo 'PDF saved as ' . $output_file . PHP_EOL;
捕获的PDF将作为当前目录中的output.pdf
文件保存。
许可
MIT许可证(MIT)