3 * Example phancap API client written in PHP.
4 * Mainly to demonstrate the authentication.
9 //phancap service configuration
10 $phancapConfig = array(
11 'url' => 'http://example.org/phancap.phar/get.php',
13 'secret' => 'coho8ajj2as'
16 //fetch the screenshot URL
17 $screenshotUrl = getScreenshotUrl('http://example.org/', $phancapConfig);
19 //output an image tag with the screenshot url
20 echo '<img src="' . htmlspecialchars($screenshotUrl) . '"'
21 . ' alt="Screenshot for example.org/" />' . "\n";
24 * Creates an URL for a website screenshot.
25 * Automatically adds a authentication signature for phancap.
27 * @param string $websiteUrl URL to website from which the screenshot
29 * @param array $phancapConfig Configuration array. Supported Keys:
30 * - url: Full URL to phancap's get.php
32 * - secret: Password for the username
34 * @return string URL for the website screenshot
36 function getScreenshotUrl($websiteUrl, $phancapConfig)
38 //default parameters for the phancap service
45 if (isset($phancapConfig['token'])) {
46 $parameters['atoken'] = $phancapConfig['token'];
47 $parameters['atimestamp'] = time();
51 foreach ($parameters as $key => $value) {
52 $encparams[] = $key . '=' . rawurlencode($value);
54 $encstring = implode('&', $encparams);
55 $signature = hash_hmac('sha1', $encstring, $phancapConfig['secret']);
56 //append signature to parameters
57 $parameters['asignature'] = $signature;
60 //url-encode the parameters
62 foreach ($parameters as $key => $value) {
63 $urlParams[] = $key . '=' . urlencode($value);
67 return $phancapConfig['url'] . '?' . implode('&', $urlParams);