About the Author

Chris Shiflett

Hi, I'm Chris, a web developer and a founding member of Analog. I live and work in Brooklyn, NY.


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

ConFoo

10 - 12 Mar 2010

At Hilton Montréal Bonaventure, Montréal, Canada.

South by Southwest

12 - 16 Mar 2010

At Austin Convention Center, Austin, Texas.

Dutch PHP Conference

10 - 12 Jun 2010

At TBD, Amsterdam, Netherlands.

O'Reilly Open Source Convention

19 - 23 Jul 2010

At Oregon Convention Center, Portland, Oregon.

New Comments

liukang wrote:

I have problem with this example. In my php.ini magic_quotes_gpc is off so i'm using only addsla...

Posted in addslashes() Versus mysql_real_escape_string()
RyanTheGreat wrote:

Well, I'm not Chris, but I will do my best to address the questions raised in the comments by Ian...

Posted in Security Corner: Cross-Site Request Forgeries
Chris Shiflett wrote:

Thanks for the kind words, Simon. I'm glad you liked the tutorial. In case it's helpful, here'...

Posted in Webstock
Chris Shiflett wrote:

Hi Robin, I plan to post something about it, but it's going to be hard to express everything i...

Posted in Webstock
Simon Mahony wrote:

Hi Chris, I really enjoyed your workshop on the Evolution of Security at Webstock. I think I g...

Posted in Webstock

Browse Comments


Work and Books

Analog Essential PHP Security HTTP Developer's Handbook