From: Christian Weiske Date: Mon, 14 Apr 2014 20:13:29 +0000 (+0200) Subject: example php client X-Git-Tag: v0.1.0~15 X-Git-Url: https://git.cweiske.de/phancap.git/commitdiff_plain/dade6ba7341d724e9a46d213853556d936dec476 example php client --- diff --git a/README.rst b/README.rst index 17faa8e..5de7946 100644 --- a/README.rst +++ b/README.rst @@ -95,6 +95,11 @@ __ http://tools.ietf.org/html/rfc5849#section-3.4.2 Example ======= + +.. note:: + + The ``docs/`` directory contains an example PHP client implementation. + We want to create a screenshot of ``http://example.org/`` in size 400x300, using the browser size of 1024x768:: 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