Dec 19

Google charts is a wonderful tool for generating all kinds of graphs and charts. It provides two methods for supplying data, either in the URL or via POST.
The URL version is limited to 2048 characters and the POST version is limited to 16K characters. I wish the FAQ explained a little more about why the limitation exists.

I’ve run into two problems using it in my applications. SSL applications complain if any non-ssl content is pulled into the page and occasionally, I have tables too large for the URL api but that really is the only way to retrieve the image in my application.

I’ve got a simple php-curl based proxy which solves both issues. https://github.com/derak-kilgo/google-chart-proxy Its a drop in replacement for the google’s chart URL.

http://chart.apis.google.com/chart?chs=350x225&cht=p3&chd=s:Mx&chdl=Charts+Users|Should+Use+Charts&chl=Users|Don't+Use&chtt=Google+Charts&chts=676767,20

becomes

http://your-domain.com/proxy.php?chs=350x225&cht=p3&chd=s:Mx&chdl=Charts+Users|Should+Use+Charts&chl=Users|Don't+Use&chtt=Google+Charts&chts=676767,20

Its just that simple.

Tagged with:
Jul 18

The end result is to take a based install of ubuntu 10.x desktop and make it ready for php web development.

These commands download about 500MB of software.

Run it line by line as root or make a bash script out of it.


#!/bin/bash

#All of these steps must be done as root.
if [ "$(whoami)" != 'root' ]; then
echo "This script must be run as root."
exit 1;
fi

#Add the zend repo to apt.
echo "deb http://repos.zend.com/zend-server/deb server non-free" >> /etc/apt/sources.list

#Add zend's signing key to the apt key ring so we can use the zend repo.
wget http://repos.zend.com/deb/zend.key -O- | sudo apt-key add -

#Add yogarine's repo so we can download the latest version of eclipse and the php development tools.
add-apt-repository ppa:yogarine/eclipse/ubuntu

# Update your repo cached software list.
apt-get update

#Install eclipse with php development tools (latest), zend server (apache, php, and php control panel), mysql (cli client and server) in a single command.
apt-get install eclipse-pdt zend-server-ce-php-5.3 php-5.3-extra-extensions-zend-server-ce mysql-server mysql-client phpmyadmin

Post install Tasks:

Reboot your computer.
Eclipse will install openJDK and you must restart to complete the installation.

Visit http://127.0.0.1:10081 to complete the setup of your zend server control panel.

Setup your document root.
I usually make a directory in var for my workspace and point eclipse to that location like so.

cp /var/www/*.php ~/workspace/
sudo rm -f -r /var/www
#Replace $USER with your login name.
sudo ln -s -v ~/home/$USER/workspace/ /var/www

To access the debugger from PDT, add the following get variables to your request:

http://localhost/test/info.php?debug_host=127.0.0.1%2C127.0.0.1&start_debug=1&debug_port=10000&original_url=http%3A%2F%2Flocalhost%2Ftest%2Finfo.php&send_sess_end=1&debug_stop=1&debug_start_session=1&debug_no_cache=1310991085348&debug_session_id=1000

Apr 15

On March 8th, the reCaptcha team announced they’re changing their API URL, effective Apr. 11th, 2011.

As others have pointed out, really poor communication from reCaptcha guys. Use your blog, put it on slashdot, reach out to the communities listed on your site. Don’t just yank the rug out from under folks. A month isn’t anywhere near long enough for this important change to trickle out to all the folks integrating with your service. Paypal gave a year and a half worth of notice to is developer community before switching its api urls and that wasn’t long enough either.

Really minor change for zend framework users or other scripting languages.
ZF hasn’t offically said when they’ll update their code. There is a bug report for it.

Here is what change to get ZF ReCaptcha service working again:
Open:
/ZendFramework-1.11.0/library/Zend/Service/ReCaptcha.php


/**
* URI to the regular API
*
* @var string
*/
const API_SERVER = 'http://www.google.com/recaptcha/api';

/**
* URI to the secure API
*
* @var string
*/
const API_SECURE_SERVER = 'https://www.google.com/recaptcha/api';
/**
* URI to the verify server
*
* @var string
*/
const VERIFY_SERVER = 'http://www.google.com/recaptcha/api/verify';
[/code]
#The old urls are still working but are being redirected.

ReCaptcha has already reflected this in their documentation.

Tagged with:
Jun 14

My new favorite toy is a box running ESXi.

ESXi is a strange beast. I’m used to using linux based host OS’s so the limitations of ESXi are a little frustrating. No direct access to physical drives, no direct access to USB devices, ESXi only knows how to read its own proprietary files system vmfs.
For all the headache, its much faster than a linux host and has a ton of configuration options via the vSphere client software.

The no cli thing bugs me a lot. The only supported way to interact with esxi is via the vSphere client software.
It does include sshd, but its off by default.
There are lots of articles on how to enable this mode.

Once you’ve got SSH access, you’ll quickly discover that most of the commands your used to in linux are missing.
Including perl, rsync, etc…

Since there are no dev tools, no gcc, or glib headers for this kernel, you can’t compile software directly on the host either.
Ah, but if you can build it into a static binary, it will run!

I’ve found an rsync binary and a php binary.
Installing these opens doors for writing useful scripts with esxi.

Login as root, plop these into your /bin/ directory and make them executable with a chmod a+x and your good to go.

Not quite that easy. The binaries work, just /bin doesn’t persist between reboots. You’ll need to place your utilities in a persistent storage location to keep them around.

Tagged with:
Jul 19

This sketch is for two really simple RSS readers.
One with php and one with javascript.

RSS or Really Simple Syndication is an text based format for publishing news and information. Being text makes it easy to manipulate with your language of choice. I like php and javascript so I’m using php and javascript for this example.

Download my code for these examples

Files included in this example

Files included in this example

First lets look at the php example:

If you open reader.php, you’ll see two functions:

One is a php environment test that checks out your php instance to make sure the functions  using are turned on.
Some hosting companies disable some of these extensions. If you don’t have direct control over the server environment, its always good to do a little probing to make sure things are going to work as expected. It also has a constant at the top  of the page which will disable the test. Once you’ve run this code on your server successfully, there is no need to waste CPU cycles on this test with every page load.

The second function is the rss reader. Lets looks at the feed data so we can understand what is going on here.

Example RSS2.0 feed data:

<xml>
<rss>

<channel>
<item>
<title>Welcome to my blog</title>
<link>http://127.0.0.1/my-blog</link>
<description>A simple example from my blog</description>
</item>

<channel>
</rss>

With SimpleXML, if I want the title from the first post your code would look something like this:

$strData = file_get_contents(‘http://www.127.0.0.1/rss);
$oXml = simple_xml_load_string($strData);
$title = $oXml->channel->item[0]->title;

PHP allows urls to be loaded with any of the file functions as if they are a file on the local filesystem. As such, I can use file_get_contents() to load the data from the url into the string. (This depends on PHP’s ‘allow_url_fopen’  setting, which must be enabled.)

SimpleXML is doing all the hard work. Its going to turn that string of xml in to a set of nested objects which will allow us to programmaticly get at the data in the xml document without having to build a complicated custom string parser.

In the example, all operations with SimpleXML are enclosed in a try-catch block. This is because SimpleXML will throw exceptions the XML is not formed correctly(missing open or close tag, including reserve characters where there shouldn’t be any, ect…) , or if you try to access an element that does not exist.

You’ll also notice from the example that third parameter of ‘simplexml_load_string’. By giving the function ‘LIBXML_NOCDATA’, SimpleXML will automatically convert any <![CDATA] blocks it encounters into strings. This sort of block is used to safely enclose characters that would otherwise be reserved by the xml format without breaking the XML format. Without that option, our feed will return an empty SimpleXML object every time it encounters a <![CDATA] block.

That is all you need to read XML with PHP.

One note: Most servers send XML as ‘UTF-8′ instead of simple ASCII text. If your feed reads some UTF-8 text and tries to  display it as ASCII text without properly converting it first, you may notice artifacts in the text that don’t make sense. These kinds of issues can be fixed with PHP’s multi-byte string functions at the expense of added complexity to the feed reader code.

This is not ‘production’ code.

The php example is not really production code. The PHP process is blocked while waiting for the feed to be read. It won’t continue loading the page until the feed is loaded or the connection between your server and the feed server times out.  A workaround to this is to load the feed and cache it on the server as a file or in memory with a tool like memcache. In most cases this scales well and mitigates the dependency on the other site but does not eliminate the problem.

Reading feeds with Javascript:

A javascript example from scratch would be a lot more complicated that this php example so I’m using Jean-Francois Hovinne’s jQuery plug-in, jFeed.

This implementation has the added bonus of dealing well with other character sets like UTF-8.
The files included in the example code:

  • jquery.js – The core of the jQuery javascript library
  • jquery.jfeed.pack.js – A mini-fied version of the jFeed plug-in
  • reader.js – A simple reader which works just like the php reader, except with javascript.
  • proxy.php – Built on Jean’s example with an extra security check.

In this case, the page will load completely before the jFeed library tries to load the feed. jFeed will use a XMLHttpRequest to load the feed data and jquery’s DOM function to parse the data giving us access to the data elements similar to SimpleXML in php.

Once the feed is loaded, it then uses jQuery’s DOM function to add the content to the page. The user will notice some lag between the page loading and the feed loading, but the feed won’t slow down the rest of the page as it did with the php example. Caching could also be used here with a quick php script to speed things up a bit.

Javascript does have limitations. You won’t be able to read a feed from another domain, which is why Jean-Francois included the ‘proxy.php’ script. This script loads the feed and presents it to the javascript as if it came from the same domain. He says, “don’t use this in production” and I would agree. I made on little change to his script, adding a check to make sure the request is coming from local host and not some external site using us as generic proxy which should make it a little safer to use. Having something like this on your server also opens your domain to XSS attacks.

Javascript Example Code

To read the feed, include the required javascript file in your documents head (which are included in the sample code):

<html>
<head>

<script type=”text/javascript” src=”jquery/jquery.js”></script>
<script type=”text/javascript” src=”jquery/jquery.jfeed.pack.js”></script>
<script type=”text/javascript” src=”jquery/reader.js”></script>

Add a div tag place holder for where you would like your rss feed to appear

<div id=”feedReplace”></div>

Add this just above the closing body tag.

<script type=”text/javascript”>getFeed(‘#feedReplace’, ‘http://yoururl.com/rssfeed’,5);</script>

When the page loads, the javascript will load the rss, parse it, format it and append it into the page at the location specified. You can also specify how many items it should show. The example above shows the top 5 items.


Which approach is better?

If the feed data is a featured item of your site it may make more sense to use php because you can be sure the content will load. If the feed is just extra on a larger site, javascript makes a lot of sense because it doesn’t bog down the site while feed is loading.