Running PHP WebService on shared host

I need to create a web service running on my shared linux host, and found this post, then this one. One thing they didn’t mention in post is that the XML_Serializer is in PEAR.

If you try to follow the wiki doc from dreamhost to install pear on shared host, watch out, there is a typo : PATH_SEPARATOR.

set_include_path( '.' . PATH_SEPARATOR . '/home/(youruser)/pear/php' . PATH_SEPARATOR . get_include_path() );
Advertisements

Switching from simpletest to phpUnit

NetBeans is becoming my favorite IDE for php and ruby. I’ve been used simpletest for php unit-test framework for more than one year, and I didn’t realize NetBeans support phpUnit! It’s time to switch.

I chose manual install phpUnit, download release package, extract to php folder, replace tokens in phpunit.bat (windows), hookup batch file (so called script) to NetBeans. Yeah, everything looks normal.

Swtich my old simpletest file to phpUnit, just change the require path and the base class from UnitTestCase to PHPUnit_Framework_TestCase, Shift+F6, it runs! The syntax in both test frameworks are exactly same!!

Only one problem I found so far, assertEqual() hung in NetBeans 6.8, I’m using phpUnit 3.4.9. Will try the latest once I upgrade my php version.

About code coverage, I need to download Xdebug extension for php from here: http://www.xdebug.org/find-binary.php, run php -i , paste output into textbox, the right version will be automatically picked up. Wow, what a smart community!

My php code in Netbeans is very colorful now.

Here is the install steps on Mac:

  1. Install PEAR, according to this page, get the go-pear.php file, run php go-pear.php.
  2. Install phpUnit, see instruction here, needs to manually cp phpUnit folder to default php extension folder which is /usr/lib/php, not in extension subfolder. After this, phpunit shell command should be available. The trick here is to set PATH variable in Mac, echo ‘export PATH=YOURPATHHERE:$PATH’ >> ~/.profile (from  this post.)
  3. Install xDebug php extension, same as windows, goto http://pear.php.net/manual/en/installation.getting.php, follow instructions.
  4. NetBeans IDE conventions for test cases, test  class name should call targetClassTest. File name doesn’t matter.

RSS parser and generator, PHP and Ruby, CData

I was using Kouhei Sutou‘s rss lib to parse and generate a rss feed, it was running very well until I stuck with setting the CDATA value to description field. On Kouhei’s webpage it was said this lib started support content:encoded syntax since version 2.11. But my test found that it can read CDATA, not setting.

Emailed to Kouhei directly, got response pretty fast: I can’t do that.

OK, Ruby is too new to those stuff. I had to turn to php then.

Found 2 nice libs, lastRSS and feed generator, both are from Europe, fantastic tools, even neither of them has been updated since 2005.

The only glitch in feed generator is the default value of encoding, I had to hatch into its class module to change channel.encoding, the outside operation doesn’t work.

To set the CDATA using feed generator, syntax look like this:

$item->description = “asdfasfasf<br />asdfasdfa<br />”;
$item->descriptionHtmlSyndicated = true;

The generated rss xml will be:

<description><![CDATA[asdfasfasf<br />asdfasdfa<br />]]></description>

I have another problem using lastRSS, the fopen(rss_url) method only works locally, not on my DH shared host, maybe the allow_url_open is off by default on DH? I didn’t spend too much time on it because I switched to curl to work around immediately. I will look into this setting later.

LastRSS does not support ATOM, so I use famous MagpieRSS instead for an ATOM feed. There is only one thing I don’t like MagpieRSS is, it only reads feed from url, while lastRss can read both from url and file.