About the Author

Chris Shiflett

Chris Shiflett is an author and speaker who leads the web application security practice at OmniTI.


PHP Advent Calendar Day 19

Today's entry is provided by Marcus Börger.

Marcus Börger

Name
Marcus Börger
Blog
marcus-boerger.de
Biography
Marcus Börger is a specialist in C, C++, databases, UML, XML, and of course PHP. To the PHP community, he is also known as helly. As a core developer, he contributes a lot to PHP and focuses on the new OO features of PHP 5 and Zend Engine 2. Marcus has been hacking around on all sorts of things for over 15 years, and being an avid snowboarder, he happily accepted an offer from Google to work in their Zürich office. He can often be found snatching gummy bears from his favorite Italian pizzeria and taking photographs.
Location
Zürich, Switzerland

Before I joined Google, a Swiss engineer had an idea to create an internal service at Google that generates graphs from simple URLs. Shortly thereafter, the service became quite popular internally. During this time, use of the service was restricted; a key was required. This past summer, we decided to open the service to the general public, hoping a few people would like the idea as much as we did. There is now an easy, keyless service that generates various types of charts such as line charts, bar charts, pie charts, and more.

Consider the following example chart:

Hello World

This chart is generated from the following URL:

http://chart.apis.google.com/chart?...mp;chd=s:foobar

This URL consists of the base URL (http://chart.apis.google.com/chart?) followed by the query string:

  • The chart type (cht=p3) is a 3D pie chart.

  • The chart size (chs=200x100) is 200 pixels wide and 100 pixels high.

  • The chart title (chtt=Hello+World) is Hello World.

  • The chart data (chd=s:foobar) is the simple string foobar. Choices for encoding are simple (s), extended (e), and text (t). The encoding is separated from the data by a colon.

Incoming data must be scaled to the range of the encoding, so you need to provide a way to do this. When using simple encoding, the API accepts 62 values:

  • Uppercase A (0) through Z (25)

  • Lowercase a (26) through z (51)

  • Digits 0 (52) through 9 (61)

You can indicate a missing value with an underscore (_), and you can separate data sets with a comma (,).

The following function (simple_encoding()) takes care of the encoding for you:

<?php
 
function simple_encoding(array $data, $min = 0, $max = 61) {
    $codes = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' .
             'abcdefghijklmnopqrstuvwxyz' .
             '0123456789';
    $min = (float)$min;
    $max = (float)$max;
    $diff = ($max - $min) / 61;
    $result = '';
 
    foreach ($data as $value) {
        if (is_null($data)) {
            $result .= '_';
        } else {
            $value = (int)(((float)$value - $min) / $diff);
 
            if ($value < 0 || $value > 61) {
                  $result .= '_';
            } else {
                $result .= $codes[$value];
            }
        }
    }
 
    return $result;
}
 
var_dump(simple_encoding(array(-10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100), 20));
var_dump(simple_encoding(array(-10, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100), 20, 100));
 
?>

This produces the following:

string(12) "___AOds7____"
string(12) "___AHPWemt19"

More about the API can be found at chart.apis.google.com.

About This Post

PHP Advent Calendar Day 19 was posted on Wed, 19 Dec 2007 at 23:27:59 GMT.

2 Comments

1. Jakob Buis's GravatarJakob Buis said:

Another great (and recent) article on the Google Graphs API can be found @ 24ways 2007

Thu, 20 Dec 2007 at 06:47:11 GMT Link


2. Asanka Dewage's GravatarAsanka Dewage said:

Thank You!

Sun, 23 Dec 2007 at 23:41:11 GMT Link


Post A Comment

Personal Details and Comment

Style Guide

Line breaks are converted to paragraphs. Also use:

  • <a href="" title="">text</a>1
  • <em>text</em>
  • <blockquote><p>text</p></blockquote>
  • <code>2  <?php  if ($foo) {      $foo = TRUE;  }  ?></code>
  1. Note: <code> can be used inline (e.g. in paragraphs) or in a block as shown. Include whitespace and newlines in blocks.

Please enter Chris (my first name) below. This is a primitive spam prevention technique, and I apologize for the inconvenience.

Preview and Submit

Upcoming Talks

php|works / PyWorks

12 - 14 Nov 2008

At Sheraton Gateway Hotel Atlanta Airport, Atlanta, Georgia.

New Comments

Dave wrote:

Hi Seth, I'm experiencing exactly the same problem as you have. Have you fixed it? How?

Posted in
Matt Robinson wrote:

Wotcha Chris, thanks for the tip about headers in the web inspector, I hadn't noticed them! (Actu...

Posted in Inspecting and Hacking HTTP
Stelian Mocanita wrote:

Not much I know so far, didn't get far with debugging it to get as far as http headers but I know...

Posted in Facebook Worm
Chris Shiflett wrote:

Yes, good point. The message this worm sends is really just a phishing attack, and Facebook is do...

Posted in Facebook Worm
yawnmoth wrote:

Given that Samy required no action on the users part, above and beyond viewing an infected users ...

Posted in Facebook Worm

Browse Comments