Simon Willison’s Weblog

Subscribe
Atom feed for php

190 posts tagged “php”

2002

A new XML-RPC library for PHP

I spent most of yesterday messing around with XML-RPC. There are a variety of XMl-RPC libraries available for PHP but none of them felt right for what I needed. So, I’ve written my own.

[... 115 words]

Grabbing web pages with Perl and PHP

Web Basics with LWP (via Scott) is an excellent tutorial on Perl’s LWP, a powerful set of modules which make it easy to retrieve content from the web. I’ve been using the excellent Snoopy class for PHP for the same purpose, but I have to admit it isn’t half as comprehensive as LWP. I’ve also written my own simple function safeGet for more light weight tasks—it grabs and returns the contents of a web page but limits both the size of the page and the maximum time it can take to download it.

PHP XML-RPC

I’ve been playing with XML-RPC and PHP today. I ended up using Keith Devin’s pleasantly straight forward library thanks to PHP’s built in (and completely undocumented) XML-RPC support conflicting with the function names used by Useful Inc’s partially object oriented library. I’ve been messing around with Stuart’s idea for a trackback alternative and things are looking pretty promising.

Templating with Smarty

Zend have a short tutorial on PHP Templating with Smarty.

PHP generated PDFs

R&OS PDF PHP classes (via tidak ada). This is the most useful PHP library I’ve seen in a long time. It allows dynamic generation of PDF files without needing any additional modules installed on the server (although GD is required if you want to add images to your PDFs). It is extremely easy to use and has an impressive set of features, including PDF drawing tools, built in page number support and excellent documentation. On the topic of PDFs, Yes You Can advocates their use for presentations and touches on a method of generating them using Python.

PHP immune to SQL injection attacks

An interesting thread on SitePoint about SQL injection attacks. One of the points brought up is that PHP is by default virtually immune to injection attacks thanks to magic quotes (discussed here yesterday).

Magic quotes solution

Pink Goblin (otherwise known as HarryF) explains why magic quotes are evil. This is an issue that every PHP developer should be aware of, as it can cause all kinds of problems in your scripts if you ignore it. He suggests using a custom myAddSlashes() function which only calls addslashes() if magic quotes are turned off. I have an alternative solution—chose your preferred setting (quotes on or off) and apply it at run time to all incoming data in one go. My code for doing this is available here. By a bizzare coincidence I wrote the script this morning, then spotted a link to the Pink Goblin article on tidak ada literally five minutes after finishing it.

PHP numbered code listings

Michael V has written a couple of functions to apply my CSS numbered code listing technique to PHP’s built in syntax highlighting.

PHP and ID3 tags

MP3 Piranha is a clever application which indexes your MP3 collection and uses the Amazon Web Service API to look up the album cover, related albums and provide a link to buy the album from Amazon. Out of curiosity, I ran a search for a PHP library to decode ID3 tags to see if such a thing could be built with PHP, and came up with this script by Leknor. The class is well written and I learnt a lot about ID3 tags looking through it—it seems they take up the last 128 bytes of an MP3 file and can be decoded using PHP’s unpack() function.

Archivist goes live

After a successful private beta, the new searchable css-discuss archive is ready for use by the general public. If you spot any bugs or have any suggestions for improving the archive please drop me a line.

[... 88 words]

PHP strings tip

PHP Tip: You can access characters within a PHP string using the index of the caracter in curly braces after the variable name. For example, $string{0} returns the first character, $string{3} returns the fourth character and $string{strlen($string)-1} returns the last character. You can assign to individual characters of a string in the same way, so $string{0} = strtoupper($string{0}); will convert the first character of a string to upper case. For more tips on working with strings see the PHP manual and Zend’s useful strings tutorial.

Smarty 2.30

Smarty 2.3.0 is out, and includes a useful new debugging function and support for assigning template variables by reference. I get a mention in the CHANGELOG for a small bug fix I submitted. Open source at work.

Multi-lingual PHP

A thread on SitePoint got me thinking about how PHP’s little known parse_ini_file() function could be used to easily manage multiple language versions of web site messages. Sections could be set up for each supported language, with message definitions repeated in each section. You could even have a default message section at the top which is used when a message has not been defined for a particular language. The comments on the parse_ini_file manual page suggest that the function is not particularly suitable for large scale use—PHP exits if the ini file is malformed and it can’t handle files larger than 16,382 bytes. That said, rolling a more reliable native PHP version should be a trivial project.

Warp factor PHP

I’ve been working on a PHP application that can take an XTM formatted Topic Map and convert it in to relational data in MySQL, run queries on it and convert it back to an XTM later. My work on the initial parser has involved some pretty heavy duty processing, and the speed with which PHP and MySQL are handling the data I’m throwing at them is phenomenal. The classic Italian Opera Topic Map example weighs in over a megabyte of XML, but PHP is munching it up and spitting out (and executing) over 13,000 SQL queries in less than seven seconds.

Instant PHP Web Services

XML-RPC Class Server is a really clever piece of code. It consists of a single file which you can drop in a directory full of PHP .class.php files to instantly provide an XML-RPC interface to every class in the directory. Private methods that begin with an underscore are not included in the web service. Unfortunately the system requires PHP’s XML-RPC extensions to be enabled.

PHP object overloading

I’m not sure how this one snuck under the radar, but PHP now supports object overloading (as of version 4.2.0). It can be implemented by creating class methods __set(), __get() and __call() and then applying the new overload() function to the class name. The documentation claims that __call() is not yet supported but is apparently out of date. Standard warnings about the experimental and unfrozen nature of the extension apply.

New PHP vulnerability

Vulnerability found in PHP 4.2.0 and 4.2.1 involving HTTP POST requests. The PHP group have released PHP 4.2.2 which fixes this problem.

Amazon search updated

I’ve updated PHP Amazon Search to implement a few more search methods, and altered the example script to allow searches for related items.

Fun with Amazon

There’s plenty of activity surrounding Amazon web services today. My limited demo barely scratches the surface of the possibilities—people are already experimenting with Amazon’s similarity search and Mark Pilgrim has released PyAmazon, a Python wrapper for the Amazon API. I’ve started listing alternative implementations on the PHP Amazon Search page, and I’ll be sure to blog the more innovative examples as and when I find them.

Amazon web services

Amazon have launched a brand new web service interface to their huge database of products. I’ve been playing around with it, and I’ve knocked together a simple search engine example in PHP, with the code available for anyone who wants it. I did a similar thing a few months ago when Google released their Web API so we’ve set up a new site at Incutio to host these and other open source projects—scripts.incutio.com. The site is only a few hours old and we’d love some feedback—contact us directly or add a comment to this entry.

Blogchat rocks

I spent a while today over at Brent Ashley’s blog chatting away on BlogChat. BlogChat is Brent’s impressive DHTML chat system (backend in PHP, front end via JSRS) which allows anyone visiting his blog to talk to him (and other visitors) in real time, provided he is online to host the session. During the afternoon I got to talk to people from all over the world, all with similar interests because they all had the same taste in blogs. I am hoping to install a version of Brent’s system on this site in the not-too-distant future.

Python in PHP

Python in PHP (via HarryF on the SitePoint Forums):

[... 123 words]

Wiki fun

Yesterday I set up a Wiki for Smarty as well. I like Wikis. The WikiEngine used for Smarty and MACCAWS is called TaviWiki, and is implemented in PHP with a MySQL back end. I had previously deployed PhikiWiki for a couple of university projects, which is good in that it is the only PHP Wiki I know of that works from the file system, but bad in that it has no support for version tracking (essential if your Wiki is accessible to the public).

Smarty at OSCON

Smarty at OSCON: Andrei Zmievski and Sterling Hughes will be presenting a tutorial titled “Template Architectures with Smarty” at the O’Reilly Open Source Convention in San Diego, July 22-26, 2002. Further details are available here. Smarty is a powerful template engine for PHP which combines a highly versatile templating language with excellent performance gained through template compilation.

Busy day

Quiet blogging day today, but I’ve been busy behind the scenes. Firstly I’ve been playing around with TaviWiki (an excellent PHP Wiki engine), using it as a proof of concept for a Wiki-driven small website content management system. I’ve looked at several PHP Wiki’s in the past and TaviWiki is the best by far, with rock solid features and a well organised code base. My only criticism is that the script relies on globals a bit too much, making it quite hard to figure out what is going on at times. I’ve also been working on IncDirectory, the open source sequel to my aging links directory script ssLinks. Add to that a new CSS experiment and preliminary work on rewriting this blog to use mySQL and today has been my most productive in quite some time.

PHP XML Classes

PHP XML ClassesA collection of classes and resources to process XML using PHP. Includes PHP implementations of Xquery lite and RDQL along with parsers for RDF, RSS and RDDL.

Rasmus Lerdorf’s blog

Rasmus Lerdorf (the creator of PHP) has a blog. His latest entry discusses Palladium, and asks if it will actually help build up the alternative market of non wintel users.

PHP form problem

I’m suffering from a bizzare PHP bug. For some reason, POSTed form entries over a certain length are being lost when submitted to scripts running on this site. Here is a demo script which demonstrates the problem—try typing in a short chunk of text and it will submit fine, but anything long (seemingly over about 1300 characters) will be mysteriously lost. The server’s PHP Info output is available. If you have any idea what could be causing this drop me a line or add a comment to this entry.

PHP auto class inclusion

When developing PHP applications, I usually have a classes directory somewhere in which I keep all of my PHP classes ready for inclusion. I name the class files ClassName.class.php. Normally I have a common.inc.php file that is included in all of the scripts in my application and requires the classes needed by the application, but today I wrote a few lines of code that saves me from having to alter that file every time I write a new class:

[... 154 words]

Kuro5hin on AudioGalaxy

Kuro5hin are running a fascinating story on AudioGalaxy, written by one of the AudioGalaxy developers who wrote a large portion of the web interface code. It covers the history of the system and its recent demise at the hands of the RIAA, with quite a few technology pointers as well (they used PHP to serve over 90 million hits a day).