Now Playing

Archive for June, 2009

PHP and stdClass

June 28th, 2009 | php, www | 1 Comment

The built in class stdClass can be instantiated manually for creating generic objects which you can then set member variables for, this is useful for passing objects to other functions or methods which expect to take an object as an argument. An even more likely usage is casting an array to an object which takes each value in the array and adds it as a member variable with the name based on the key in the array.

<?php
$obj = (object) array("foo" => "bar")
echo $obj->foo; // outputs "bar"
?>

One place you might get caught out here is trying to directly access numerically indexed values on the object, this breaks PHP’s naming convention and does not work correctly even with escaping.

<?php
$obj = (object) array("foo"=>"bar","foobar");
echo $obj->foo; //outputs "bar"
echo $obj->{0}; //$x->0 would cause a syntax error, this simply causes a notice for being unable to find the variable.

HTTP Auth with PHP

June 27th, 2009 | php, www | No Comments

HTTP Auth has a number of practical uses even in complex PHP backed systems. A prime example, and the reason I had looked into it, is web service and API authentication. In this case it is much simpler for the client to provide the details in this manner rather than perform an extra request to grab an authorisation token. A basic example lifted from the PHP manual is below.

<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
    header('WWW-Authenticate: Basic realm="My Realm"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Text to send if user hits Cancel button';
    exit;
} else {
    echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
    echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
}
?>

Three Web Text Widget

June 17th, 2009 | apple | 1 Comment

In a follow up from my recent release of an API to Three mobile’s new web texts, I decided
to make an accessible interface for myself.

Being a Mac user, the quickest way to make this happen was through the dashboard. It’s terribly basic right now, with only two hours or so put into it so far, but it’s very usable.

You can get it from my releases directory.

AIB and Three Webtext PHP5 APIs

June 15th, 2009 | php, www | 1 Comment

Over the last day I’ve put together two PHP5 API classes (loosely termed, it’s screen scraping at heart) for online services I use, AIB online banking and Three’s newly launched webtexts. I’m releasing both under the LGPL license and you can find them in my open source releases directory.

Update: I’ve ported the Three API to Perl and added it to the three releases directory

About

A Web Developer by nature and by trade. Living in Dublin, Ireland and working at Web Reservations International. Enjoys working on Open Source, web server stacks, XHTML/CSS, JavaScript and playing with the latest trends in technology.