example php client
authorChristian Weiske <cweiske@cweiske.de>
Mon, 14 Apr 2014 20:13:29 +0000 (22:13 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Mon, 14 Apr 2014 20:13:29 +0000 (22:13 +0200)
README.rst
docs/php-client.php [new file with mode: 0644]

index 17faa8e4e58011b1ae5010ef8b5d5de4cd39c566..5de79469295cc46bfeea51b36eb9720f59a69b38 100644 (file)
@@ -95,6 +95,11 @@ __ http://tools.ietf.org/html/rfc5849#section-3.4.2
 
 Example
 =======
 
 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::
 
 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 (file)
index 0000000..0074abe
--- /dev/null
@@ -0,0 +1,69 @@
+<?php
+/**
+ * Example phancap API client written in PHP.
+ * Mainly to demonstrate the authentication.
+ *
+ * @author Christian Weiske <cweiske@cweiske.de>
+ */
+
+//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 '<img src="' . htmlspecialchars($screenshotUrl) . '"'
+    . ' alt="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