- Docs
- Example code
- PHP - HTML to Image Example
PHP - HTML to Image Example
Authentication with PHP
Use HTTP Basic authentication with your API ID as the username and API key as the password. Find both in the dashboard. Keep credentials in server-side configuration or environment variables.
PHP example code
The image examples use POST https://hcti.io/v1/image. Recordings use POST https://hcti.io/v1/recording and return a recording ID and media URL. See Recordings for timing, actions, and output options. Both creation endpoints require images:create; see authentication and API keys for scoped credentials and access errors.
Official Composer client
Section titled “Official Composer client”For typed requests, responses, and signed URL helpers, use the official html-css-to-image/client package.
composer require html-css-to-image/client<?php
use HtmlCssToImage\HtmlCssToImageClient;use HtmlCssToImage\Request\CreateHtmlCssImageRequest;use HtmlCssToImage\Response\CreateImageSuccessResponse;
require __DIR__ . '/vendor/autoload.php';
$client = new HtmlCssToImageClient( apiId: 'your-api-id', apiKey: 'your-api-key',);
$result = $client->createImage( new CreateHtmlCssImageRequest( html: "<div class='box'>PHP ✅</div>", css: '.box { border: 4px solid #03B875; padding: 20px; }', ),);
if ($result instanceof CreateImageSuccessResponse) { echo $result->url;} else { echo $result->error;}Recordings
Section titled “Recordings”Record HTML/CSS or a webpage as an MP4, WebM, or GIF with createRecording:
use HtmlCssToImage\Recording\RecordingFormat;use HtmlCssToImage\Recording\RecordingOutputOptions;use HtmlCssToImage\Recording\RecordingSimpleScrollAction;use HtmlCssToImage\Request\CreateUrlRecordingRequest;
$result = $client->createRecording(new CreateUrlRecordingRequest( url: 'https://example.com', durationMs: 10000, actions: [new RecordingSimpleScrollAction()], output: new RecordingOutputOptions(format: RecordingFormat::MP4),));if ($result->success) { echo "<video src=\"{$result->recordingUrl}\" controls></video>";}Embed or request the recording URL directly; it waits until the recording is ready. Use CreateHtmlCssRecordingRequest for HTML/CSS. See Recordings for triggers, actions, and output options.
The client also supports URL screenshots, templates, batches, image deletion, render options, and signed URLs. See the PHP client repository for complete usage and API documentation.
Direct HTTP example
Section titled “Direct HTTP example”<?php$ch = curl_init('https://hcti.io/v1/image');curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_POST => true, CURLOPT_USERPWD => 'your-api-id:your-api-key', CURLOPT_POSTFIELDS => http_build_query([ 'html' => "<div class='ping'>Pong ✅</div>", 'css' => '.ping { padding: 20px; font-family: sans-serif; }', ]),]);$body = curl_exec($ch);if ($body === false) { throw new RuntimeException(curl_error($ch));}$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);curl_close($ch);if ($status < 200 || $status >= 300) { throw new RuntimeException("API request failed ($status): $body");}echo json_decode($body, true, 512, JSON_THROW_ON_ERROR)['url'];PHP example with Guzzle library
Section titled “PHP example with Guzzle library”Using an HTTP library such as Guzzle can simplify your code even further. Here’s an example of how to use the HTML/CSS to Image API with Guzzle.
Installation instructions for Guzzle are here.
<?phprequire 'vendor/autoload.php';
$html = "<div class='ping'>Pong ✅</div>";$css = ".ping { padding: 20px; font-family: 'sans-serif'; }";
$client = new \GuzzleHttp\Client();// Retrieve your user_id and api_key from https://htmlcsstoimage.com/dashboard$res = $client->request('POST', 'https://hcti.io/v1/image', [ 'auth' => ['user_id', 'api_key'], 'form_params' => ['html' => $html, 'css' => $css]]);
echo $res->getBody();// {"url":"https://hcti.io/v1/image/5803a3f0-abd3-4f56-9e6c-3823d7466ed6"}?>The code turns out to be a bit more readable and less complex when using Guzzle. A great option if you’re open to adding the library to your project.
Debugging Error:SSL certificate problem: unable to get local issuer certificate
Section titled “Debugging Error:SSL certificate problem: unable to get local issuer certificate”When running this script on a Windows machine, it’s possible you’ll get an SSL error. The fix for this is here.
Order confirmations from a Laravel application
Section titled “Order confirmations from a Laravel application”A Laravel checkout can create a branded order summary from a Blade view using the order’s line items and delivery details. Send the rendered HTML to HCTI and save the returned image URL with the order. Your confirmation email can then include the summary without asking an email client to reproduce the checkout page’s CSS.
For exports that don’t need to appear immediately, put the render in a queued Laravel job after the order is committed. Keep the HTML for that export independent of session state. Sending an authenticated checkout URL alone won’t give the renderer the customer’s login session.
Need help?
Talk to a human. Email support@htmlcsstoimage.com and we’ll help you get started.