GrabzIt Screenshot API Examples for PHP
Capture a website, HTML string, or local HTML file with GrabzIt’s PHP API. Compare Save and SaveTo, configure image options, and troubleshoot common issues.
To capture a screenshot with GrabzIt’s PHP API, create a GrabzItClient with your application key and secret, call an input method such as URLToImage, then save the result with SaveTo or Save. Use SaveTo for a local file and for localhost workflows; use Save when you have a publicly reachable handler URL to process the completed capture.
The examples below use placeholder credentials. Keep your real application key and secret out of source control. See GrabzIt’s PHP library overview and image capture options for the vendor documentation.
1. Install and configure the PHP client
Get the GrabzIt PHP library using the installation method documented for the version you use. The official overview’s example includes GrabzItClient.php. Make that file available to your application, then configure credentials in environment variables or a secret store. The code below assumes the library file is in the same directory as the script.
<?php
require_once __DIR__ . '/GrabzItClient.php';
$applicationKey = getenv('GRABZIT_APPLICATION_KEY');
$applicationSecret = getenv('GRABZIT_APPLICATION_SECRET');
if (!$applicationKey || !$applicationSecret) {
throw new RuntimeException('Set GrabzIt application credentials first.');
}
$grabzIt = new \\GrabzIt\\GrabzItClient($applicationKey, $applicationSecret);
Set GRABZIT_APPLICATION_KEY and GRABZIT_APPLICATION_SECRET in your runtime environment before executing the script. Avoid printing them in error messages or committing them to a repository.
2. Capture a URL and save the image locally
This complete script captures a URL and saves the result beside the PHP file. Use a writable output directory in a real application. GrabzIt documents URLToImage for URL input and SaveTo to save the created image locally.
<?php
require_once __DIR__ . '/GrabzItClient.php';
$key = getenv('GRABZIT_APPLICATION_KEY');
$secret = getenv('GRABZIT_APPLICATION_SECRET');
if (!$key || !$secret) {
throw new RuntimeException('Missing GrabzIt application credentials.');
}
$grabzIt = new \\GrabzIt\\GrabzItClient($key, $secret);
$grabzIt->URLToImage('https://example.com');
$grabzIt->SaveTo(__DIR__ . '/result.jpg');
Choose a destination your PHP process can write to. If captures run in a web request, avoid placing generated files in a publicly served directory unless you intend to expose them.
3. Choose Save or SaveTo
The two save methods fit different completion workflows:
| Method | Use it when | What to provide |
|---|---|---|
SaveTo |
You want the PHP process to wait while the capture is created and saved to a local path. The GrabzIt overview specifically recommends it for localhost. | A writable filesystem path. |
Save |
You have a public callback endpoint that can receive and process the completed capture. | A reachable handler URL. |
Example callback-based invocation:
<?php
require_once __DIR__ . '/GrabzItClient.php';
$key = getenv('GRABZIT_APPLICATION_KEY');
$secret = getenv('GRABZIT_APPLICATION_SECRET');
$grabzIt = new \\GrabzIt\\GrabzItClient($key, $secret);
$grabzIt->URLToImage('https://example.com');
$grabzIt->Save('https://your-domain.example/handler.php');
The handler URL must be publicly reachable by the service and must implement the processing flow required by the library. Configure the handler for a hosted demo or application when needed; the GrabzIt demo setup guide describes credentials and optional handler configuration.
4. Select the input source
Pick the capture method that matches where your page content lives:
URLToImage($url)captures a page identified by a URL.HTMLToImage($html)renders an HTML string.FileToImage($path)renders a local HTML file.
Each capture request needs a save step afterward. For example, replace the capture line in the local-file script with one of the following:
$grabzIt->HTMLToImage('<html><body><h1>Report</h1></body></html>');
$grabzIt->SaveTo(__DIR__ . '/report.jpg');
$grabzIt->FileToImage(__DIR__ . '/report.html');
$grabzIt->SaveTo(__DIR__ . '/report.jpg');
For HTML that references external stylesheets, scripts, fonts, or images, make sure those resources can be loaded in the capture environment. The library also documents related PDF input methods such as URLToPDF, HTMLToPDF, and FileToPDF; those create documents rather than screenshot images.
5. Configure image dimensions, format, and page content
GrabzIt’s image options cover output format, dimensions, browser dimensions, delay, and selector-based capture behavior. The exact option methods and accepted values depend on the PHP library API version, so use the official image option reference and technical documentation when adding option calls.
Format
JPG is documented as the default. PNG is also supported and is recommended in GrabzIt’s guidance for screenshot cases where JPG quality may be insufficient. PNG may be a better fit for sharp text and interface details; JPG can be appropriate when a smaller photographic image is desired. This is format guidance, not a performance benchmark.
Dimensions and full-page captures
Set width and height when you need a fixed output size. The documented value -1 disables cropping for that dimension. Uncropped full-page images can become large, and the documentation notes that there is no full-length browser width. Large dimensions increase file size and can make downstream storage or delivery slower.
Capture one element
Use a CSS selector when the goal is a component rather than an entire page. For example, a selector such as #features targets an element. If multiple elements match, the technical reference says the first matching element is used. Confirm the selector matches the intended element at capture time.
Dynamic pages and interactions
For pages that render after initial navigation, the options reference lists delay and wait-for-element controls. Selector features also include target, hide, click, hover, and scroll behavior. Use the narrowest wait that matches the page’s actual rendering behavior. A fixed delay may help with predictable client-side rendering, while waiting for a selector ties readiness to a specific element; neither guarantees that every third-party resource or animation has finished.
6. Use cURL, Python, or Node.js instead
These examples show the same kind of URL-to-image request in other languages. They are alternatives for a service integration that accepts an HTTP request; the PHP workflow above is the GrabzIt-specific library flow.
cURL
curl -G "https://api.screenshotneo.com/v1/shot" \
-d access_key=YOUR_API_KEY \
--data-urlencode url=https://example.com \
-o shot.webp
Python
import requests
r = requests.get(
"https://api.screenshotneo.com/v1/shot",
params={"access_key": "YOUR_API_KEY", "url": "https://example.com"},
timeout=90,
)
r.raise_for_status()
with open("shot.webp", "wb") as image_file:
image_file.write(r.content)
Node.js
const q = new URLSearchParams({
access_key: 'YOUR_API_KEY',
url: 'https://example.com'
});
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`Screenshot request failed: ${res.status}`);
const image = Buffer.from(await res.arrayBuffer());
await import('node:fs/promises').then(fs => fs.writeFile('shot.webp', image));
7. Troubleshoot common problems
| Problem | Likely cause | What to check |
|---|---|---|
PHP cannot find GrabzItClient.php |
The library file is missing or the include path is wrong. | Install or copy the library as documented, and use an explicit path such as __DIR__ . '/GrabzItClient.php'. |
| Authentication fails | The application key or secret is missing, mistyped, or belongs to a different configuration. | Check the runtime environment values and account configuration without logging secrets. |
| The output file is absent | The destination directory is not writable, or the process has not completed successfully. | Use a writable path, check PHP filesystem permissions, and inspect the library’s documented error handling. |
Save callback does not arrive |
The handler is not publicly reachable or is incorrectly configured. | Verify the URL from outside your development network and configure the handler according to the PHP library workflow. Use SaveTo for localhost. |
| The page is clipped or unexpectedly sized | Fixed dimensions crop the output, or full-page behavior was not configured as intended. | Review width and height settings; use the documented -1 uncropped value where appropriate, and account for potentially large output. |
| A component is missing from an element capture | The CSS selector matched nothing, matched a different element, or the content appeared later. | Check the selector in the rendered page, remember that the first matching element is used, and consider a wait-for-element or delay option. |
| Rendered content looks stale or incomplete | Client-side rendering or external resources were not ready when capture occurred. | Use a relevant wait or delay option and verify external resources are accessible from the capture environment. |
| The image is much larger than expected | An uncropped full-page dimension or high-resolution page produced a large bitmap. | Set suitable dimensions, capture only the needed element, or select an appropriate output format. |
8. Reliability, performance, and cost considerations
SaveTo is synchronous according to the GrabzIt overview: the application waits while the screenshot is created and written to the requested path. That is straightforward for a command-line script or a low-volume task, but a long capture can occupy a web worker. For user-facing web requests, consider whether the callback-based Save flow fits your application and make the handler reachable and robust.
Capture time and output size depend on the page, selected dimensions, dynamic content, and waits. Avoid unnecessarily long fixed delays, very large uncropped images, and capturing more page area than the use case needs. Set operational limits and handle failures through the library’s documented behavior. The supplied GrabzIt sources do not establish current pricing, quotas, or a PHP compatibility range; check the vendor’s current account and technical documentation for those details.
9. Or skip the browser setup
If you want a one-call screenshot API instead of managing a browser capture flow, ScreenshotNeo accepts a URL and returns an image or PDF. See the ScreenshotNeo API documentation.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp
- Cookie banners, popups, and chat widgets are removed before the shot.
- Bot checks, blank pages, and failed loads are never billed.
- An MCP server lets AI agents use screenshot tools.
- 1,000 screenshots a month are free with no card; paid plans start at $5 for 3,000.
Sign up free for 1,000 screenshots a month, with no card required.
10. Frequently asked questions
Can I capture HTML without hosting it first?
Yes. The PHP library documents HTMLToImage($html) for an HTML string. Save the resulting capture with Save or SaveTo.
Can I save a PDF instead of an image?
The PHP overview lists URL, HTML, and file methods for PDF output, including URLToPDF, HTMLToPDF, and FileToPDF. Use those when you need a document rather than a raster screenshot.
Does a selector capture every matching element?
No. The technical reference says the first matching element is used when multiple elements match. Make the selector specific if the page contains duplicates.
Which save method should I use for a local development server?
Use SaveTo when you are working on localhost, as the GrabzIt overview recommends. A callback-based Save needs a handler the service can reach.


