X-Git-Url: https://git.cweiske.de/phancap.git/blobdiff_plain/8925bb89e4ee91f9503f574fd540c2d9fe53a6d2..dade6ba7341d724e9a46d213853556d936dec476:/docs/php-client.php diff --git a/docs/php-client.php b/docs/php-client.php new file mode 100644 index 0000000..0074abe --- /dev/null +++ b/docs/php-client.php @@ -0,0 +1,69 @@ + + */ + +//phancap service configuration +$phancapConfig = array( + 'url' => 'http://example.org/phancap.phar/get.php', + 'token' => 'demo', + 'secret' => 'coho8ajj2as' +); + +//fetch the screenshot URL +$screenshotUrl = getScreenshotUrl('http://example.org/', $phancapConfig); + +//output an image tag with the screenshot url +echo 'Screenshot for example.org/' . "\n"; + +/** + * Creates an URL for a website screenshot. + * Automatically adds a authentication signature for phancap. + * + * @param string $websiteUrl URL to website from which the screenshot + * shall be generated + * @param array $phancapConfig Configuration array. Supported Keys: + * - url: Full URL to phancap's get.php + * - token: Username + * - secret: Password for the username + * + * @return string URL for the website screenshot + */ +function getScreenshotUrl($websiteUrl, $phancapConfig) +{ + //default parameters for the phancap service + $parameters = array( + 'url' => $websiteUrl, + 'swidth' => 300, + 'sformat' => 'jpg', + ); + + if (isset($phancapConfig['token'])) { + $parameters['atoken'] = $phancapConfig['token']; + $parameters['atimestamp'] = time(); + + //create signature + ksort($parameters); + foreach ($parameters as $key => $value) { + $encparams[] = $key . '=' . rawurlencode($value); + } + $encstring = implode('&', $encparams); + $signature = hash_hmac('sha1', $encstring, $phancapConfig['secret']); + //append signature to parameters + $parameters['asignature'] = $signature; + } + + //url-encode the parameters + $urlParams = array(); + foreach ($parameters as $key => $value) { + $urlParams[] = $key . '=' . urlencode($value); + } + + //final URL + return $phancapConfig['url'] . '?' . implode('&', $urlParams); +} +?> \ No newline at end of file