Below you will find code examples that build a Screenshotlayer API request URL, including the optional Secret Key. Replace YOUR_ACCESS_KEY with your API Access Key and YOUR_SECRET_KEYWORD with the Secret Keyword set in your Dashboard (only if you use one).
Code Example - PHP
Capture Snapshots via PHP:
Find below a simple PHP function that lets you capture a screenshot and define all required and optional parameters:
function screenshotlayer($url, $args) {
// set access key
$access_key = "YOUR_ACCESS_KEY";
// set secret keyword (defined in account dashboard)
$secret_keyword = "YOUR_SECRET_KEYWORD";
// encode target URL
$params['url'] = urlencode($url);
$params += $args;
// create the query string based on the options
foreach($params as $key => $value) { $parts[] = "$key=$value"; }
// compile query string
$query = implode("&", $parts);
// generate secret key from target URL and secret keyword
$secret_key = md5($url . $secret_keyword);
return "https://api.apilayer.net/screenshotlayer/capture?access_key=$access_key&secret_key=$secret_key&$query";
}
// set optional parameters (leave blank if unused)
$params['fullpage'] = '';
$params['width'] = '';
$params['viewport'] = '';
$params['format'] = '';
$params['css_url'] = '';
$params['delay'] = '';
$params['ttl'] = '';
$params['force'] = '';
$params['placeholder'] = '';
$params['user_agent'] = '';
$params['accept_lang'] = '';
$params['export'] = '';
// capture
$call = screenshotlayer("google.com", $params);Please Note: Optional parameters may be left blank if they are not being used.
Code Example - Ruby
Capture Snapshots via Ruby:
Find below a Ruby function that lets you capture a screenshot and define all required and optional parameters:
require 'cgi' unless defined?(CGI)
require 'digest' unless defined?(Digest)
def screenshotlayer(url, options={})
# set access key
access_key = 'YOUR_ACCESS_KEY'
# set secret keyword (defined in account dashboard)
secret_keyword = 'YOUR_SECRET_KEYWORD'
# define parameters
parameters = {
:url => url,
:fullpage => options[:fullpage],
:width => options[:width],
:viewport => options[:viewport],
:format => options[:format],
:css_url => options[:css_url],
:delay => options[:delay],
:ttl => options[:ttl],
:force => options[:force],
:placeholder => options[:placeholder],
:user_agent => options[:user_agent],
:accept_lang => options[:accept_lang],
:export => options[:export],
}
query = parameters.
sort_by {|s| s[0].to_s }.
select {|s| s[1] }.
map {|s| s.map {|v| CGI::escape(v.to_s) }.join('=') }.
join('&')
# generate md5 secret key
secret_key = Digest::MD5.hexdigest(url + secret_keyword)
"https://api.apilayer.net/screenshotlayer/capture?access_key=#{access_key}&secret_key=#{secret_key}&#{query}"
end
# set url (required), optional parameters (leave blank if unused) & call function
puts screenshotlayer("www.cnn.com",{
fullpage: "",
width: "",
viewport: "",
format: "",
css_url: "",
delay: "",
ttl: "",
force: "",
placeholder: "",
user_agent: "",
accept_lang: "",
export: "" })Please Note: Optional parameters may be left blank if they are not being used.
Code Example - Python
Capture Snapshots using Python:
Building a query to capture a screenshot using Python is as simple as:
#!/usr/bin/env python3
import hashlib
from urllib.parse import urlencode
def screenshotlayer(access_key, secret_keyword, url, args):
# encode URL
query = urlencode(dict(url=url, **args))
# generate md5 secret key
secret_key = hashlib.md5((url + secret_keyword).encode()).hexdigest()
return "https://api.apilayer.net/screenshotlayer/capture?access_key=%s&secret_key=%s&%s" % (access_key, secret_key, query)
# set optional parameters (leave blank if unused)
params = {
'fullpage': '',
'width': '',
'viewport': '',
'format': '',
'css_url': '',
'delay': '',
'ttl': '',
'force': '',
'placeholder': '',
'user_agent': '',
'accept_lang': '',
'export': ''
}
# set your access key, secret keyword and target URL
access_key = "YOUR_ACCESS_KEY"
secret_keyword = "YOUR_SECRET_KEYWORD"
url = "www.cnn.com"
print(screenshotlayer(access_key, secret_keyword, url, params))Please Note: Optional parameters may be left blank if they are not being used.
Help & Support
Any issues with your API integration? Contact our support team at [email protected].