ScreenshotNeo

BlogHow-to

URL2PNG Screenshot API Examples in PHP for Indian Developers

Build a signed URL2PNG v6 request in PHP, configure capture options, and understand caching, errors, and an alternative with ScreenshotNeo.

By the ScreenshotNeo team4 October 20267 min read

To generate a screenshot with URL2PNG in PHP, URL-encode the page URL, assemble the API options into a query string, calculate the request token as md5($query_string . $secret), and put that token and query string into the URL2PNG v6 endpoint. Keep the secret in server-side configuration. The API returns an image URL or image response according to the documented request pattern; check the current URL2PNG v6 quickstart for the exact endpoint format before deploying.

1. Configure the API credentials

Create or retrieve your URL2PNG API key and secret through the service, then provide them to PHP through environment variables or your deployment’s secret manager. Do not commit real credentials to source control or expose the secret in browser JavaScript.

export URL2PNG_API_KEY='YOUR_PUBLIC_API_KEY'
export URL2PNG_SECRET='YOUR_PRIVATE_SECRET'

Those shell commands are suitable for a local session; configure persistent environment variables using your hosting platform for production. The values above are unmistakable placeholders.

2. Build and sign a URL2PNG request in PHP

This runnable example follows the official v6 PHP signing pattern. It encodes the target URL, assembles options, signs the complete query string followed by the secret, and constructs the endpoint URL. The example prints the resulting screenshot URL; use that URL in your application or download it server-side as appropriate for the response format documented by URL2PNG.

<?php
declare(strict_types=1);

$apiKey = getenv('URL2PNG_API_KEY');
$secret = getenv('URL2PNG_SECRET');

if (!$apiKey || !$secret) {
    throw new RuntimeException('Set URL2PNG_API_KEY and URL2PNG_SECRET first.');
}

$targetUrl = 'https://example.com/';
$options = [
    'url' => $targetUrl,
    'fullpage' => 'true',
    'viewport' => '1280x900',
    'thumbnail_max_width' => '1200',
];

// URL2PNG's signing sample signs the complete query string plus the secret.
$queryString = http_build_query($options, '', '&', PHP_QUERY_RFC3986);
$token = md5($queryString . $secret);

// Confirm the exact v6 endpoint path structure in the current official docs.
$endpoint = 'https://api.url2png.com/v6/' . rawurlencode($apiKey) . '/' . $token . '/';
$screenshotUrl = $endpoint . '?' . $queryString;

echo $screenshotUrl, PHP_EOL;

The official sample’s essential token formula is md5($query_string . $URL2PNG_SECRET). The signature must be made from the same complete query string that is sent. Keep option order and encoding consistent; changing either after signing can result in a token mismatch. The code uses RFC 3986 encoding via PHP’s http_build_query; if the current official sample constructs the query string differently, match that documented serialization exactly.

For actual integration, handle the endpoint response according to the current v6 response contract. Avoid assuming that a successful HTTP status always means the page content rendered correctly; validate the returned content type and inspect error responses.

3. Choose capture and cache options

Option What it controls Practical guidance
viewport Browser viewport dimensions. The documented default is 1480×1037. Set a fixed width and height when matching a target layout. Example: 500x500.
fullpage Requests a capture of the whole document rather than only the viewport; default is false. Use for long articles or landing pages. Very tall pages can create large images and take longer to render.
thumbnail_max_width Constrains or scales the screenshot width. Use when producing smaller previews. Check resulting dimensions for your downstream layout.
unique Forces a fresh capture by varying the request value. Use a timestamp-like value when page content changes and the cached result is stale.
ttl Cache lifetime in seconds. The documented default is 2,592,000 seconds (30 days). Choose a shorter period for frequently changing pages; choose longer only when stale output is acceptable.
delay Adds a delay before capture. Use only when the page needs extra time after initial load; delays add rendering time.
say_cheese Provides a DOM condition for capture timing. Use when the page exposes a reliable condition indicating that its content is ready.
custom_css_url Applies custom CSS from a URL. Useful for adjusting presentation before the capture; ensure the stylesheet can be fetched by the service.
user_agent Sets the browser user-agent value. Use when the site serves a different layout to a specific client type.
accept_languages Sets accepted language preferences. Use to request localized page content when the site honors language negotiation.

These are documented controls, not a promise that every site will render identically. JavaScript-heavy pages, authentication walls, geolocation behavior, and third-party resources can affect the result. The reviewed URL2PNG documentation does not establish India-specific API behavior; the target site’s own locale rules determine whether an Indian developer sees localized content.

4. Understand caching, freshness, and cost

URL2PNG documents a 30-day default cache lifetime, and its plans page says retrieving a cached screenshot does not count against the plan. Set ttl in seconds to control cache duration, or vary unique to request a fresh capture. A stable set of options and target URL can therefore reuse a cached result; a changing unique value intentionally bypasses that reuse.

URL2PNG’s plans page says the service does not offer free accounts and lists paid quotas and prices that can change. Check the current URL2PNG plans before estimating project cost; do not rely on prices copied into an evergreen integration guide.

  • Cache stable pages to reduce repeated render work and avoid unnecessary quota use.
  • Use a fresh unique value only when new page content is needed.
  • Measure the output size and render duration for your own pages; no independent benchmark is established by the cited materials.
  • For reliability, log the target URL, sanitized option set, HTTP status, response content type, and provider error details. Never log the secret or signed URL if it exposes sensitive values.

5. Troubleshoot common problems

Symptom Likely cause Fix
Invalid token or authentication failure The token was calculated from a different query string than the one sent, or the API key or secret is wrong. Rebuild the query once, sign that exact serialized string plus the secret, and confirm the current endpoint format in the official v6 docs.
Wrong page or malformed URL The target URL was not encoded consistently or query characters were interpreted as API parameters. Use a standards-compliant query builder and verify the final decoded url value. Do not concatenate unescaped user input.
Screenshot shows only the top portion Full-page capture was not enabled, or the page’s content is loaded dynamically. Set fullpage=true; if content appears late, use a suitable delay or documented say_cheese condition.
Screenshot is stale A cached image is being reused. Reduce ttl or vary unique when you specifically need a fresh result.
Unexpected viewport layout The request relies on the default viewport or the target is responsive. Set explicit viewport dimensions and test the intended breakpoint.
Blank, incomplete, or broken rendering The page may need more time, depend on blocked or unavailable resources, require authentication, or reject automated browsers. Check that the target is publicly reachable by the capture service, try an appropriate wait condition, and inspect the provider response. Do not assume a longer delay will solve access restrictions.
PHP cannot find credentials Environment variables were not set in the PHP-FPM, container, or hosting process that runs the code. Configure them in that runtime’s environment and verify presence without printing their values.

6. Or skip the browser setup

ScreenshotNeo is a website screenshot API and MCP server for developers. Its one-call API can return a PNG, JPEG, WebP, or PDF; see the ScreenshotNeo API documentation for request options.

<?php
$apiKey = getenv('SCREENSHOTNEO_API_KEY');
$targetUrl = 'https://example.com/';
$query = http_build_query([
    'access_key' => $apiKey,
    'url' => $targetUrl,
], '', '&', PHP_QUERY_RFC3986);

$response = file_get_contents('https://api.screenshotneo.com/v1/shot?' . $query);
if ($response === false) {
    throw new RuntimeException('ScreenshotNeo request failed.');
}
file_put_contents('shot.webp', $response);

It removes cookie banners, popups, and chat widgets before capture. Bot checks, blank pages, and failed loads are never billed, and response headers identify the page verdict and billing status. Its MCP server lets AI agents take screenshots. The free plan includes 1,000 screenshots a month with no card; paid plans start at $5 for 3,000 screenshots.

For the equivalent request in other clients:

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp
import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://example.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://example.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);

Create a free ScreenshotNeo account for 1,000 screenshots a month with no card.

7. Frequently asked questions

How do I take a full-page screenshot with URL2PNG?

Include fullpage=true in the signed query string. The token must cover that option because the signature is computed over the complete query.

How do I generate the URL2PNG token in PHP?

Build the exact query string first, then calculate md5($queryString . $secret), following the official v6 sample. Keep the secret server-side.

Does URL2PNG have a free account?

The reviewed URL2PNG plans page says it does not offer free accounts. Verify current plan details directly because pricing and quotas can change.

Is there special URL2PNG behavior for developers in India?

The reviewed official sources establish no India-specific API behavior. Your network environment, the target site’s localization, and the provider’s current service terms are the relevant factors.

Sources