cloudsoup

no soup, no clouds

Archive for the ‘PHP’ tag

PHP problems in cron jobs at Dreamhost

leave a comment

Dreamhost, excellent hosting company on the whole but I had a php cron job setup that worked fine from my browser but failed with strange error messages when run from the command line.

Could it be, I wondered, that the path to php I’d given in my cron job wasn’t the path to the version of php I was running on the website. And that was the problem. My website was running 5.x but from the command line I was running 4.x

A quick fix so that the cron job referenced the path to php 5:

30 14 * * * * /usr/local/php5/bin/php [path to my script]/writeHolidayArray.php

and everything worked fine.

Written by David

July 13th, 2009 at 6:27 pm

Posted in Science and Technology

Tagged with , , ,

Weblog, WordPress, RSS

leave a comment

In the course of trying to tidy up cloudsoup a little I noticed that my RSS feeds’ contents weren’t formatted nicely in my News Reader – SharpReader. Not a Sharpreader problem it turns out, but (I think) a WordPress problem.

My RSS description node content is being converted to safe tokens by output-escaping, rather than being wrapped in a CDATA tag. According to the RSS spec, encoding HTML within a CDATA Section in the description is perfectly permissable but there seems to be no option in the default WordPress wp-rss2.php file, the PHP code that does the job of creating the RSS – no parameter that can be passed in the WordPress function.

If you choose to syndicate the full post of your weblog post in your RSS feed then the RSS is built like this:

<description>
<?php the_excerpt_rss(get_settings(’rss_excerpt_length’), 0) ;>
</description>
<content:encoded>
<![CDATA[<?php the_content('’, 0, ‘’) ?>]]>
</content:encoded>

whereas if you choose only an excerpt, the RSS gets built like so:

<description>
<?php the_excerpt_rss(get_settings(’rss_excerpt_length’), 0) ?>
</description>

and the function the_excerpt_rss doesn’ t take a parameter that stops all encoding transformations.

The unwanted upshot of this is that, for example, a post I’d like to see looking like this in my RSS:

  • CrashBonsai » toy cars and miniature trees collide
  • BBC NEWS | Wales | Royal seal of approval for Wales Millenium Centre » That’s a relief then
  • Sushi Eating HOWTO »…

actually looks lke this:

CrashBonsai » toy cars and miniature trees collide BBC NEWS | Wales | Royal seal of approval for Wales Millenium Centre » That’s a relief then Sushi Eating HOWTO »…

Which is not good. The WordPress Wiki’s entry on the_excerpt_rss says that I should be able to impose minimal filtering so I think my problem is being caused by the fact that I don’t have an excerpt, only a post body to most posts – and WordPress is doing another conversion on my behalf.

I could always replace the function, the_excerpt_rss with the_excerpt – which isn’t encoded by default. But when I try that, although the content is not tokenised, I still can’t see my del.icio.us lists properly.

The correct, WordPress solution would be to create a WordPress plugin. Until I do that, this is a quick-and-dirty way. The temporary solution is to make a small change to the file wp-rss2.php, amending

<description>
<?php the_excerpt_rss(get_settings(’rss_excerpt_length’), 0) ?>
</description>

to

<description>
<![CDATA[<?php the_excerpt_rss(get_settings(’rss_excerpt_length’), 3) ?>]]>
</description>

and then to make another amendment to the function get_the_excerpt in the file template-functions-post.php. Simply comment out the line :

$output = strip_tags($output);

That seems to be working now for my RSS feed (which, incidentally, I’ve burned through FeedBurner). I’ll need to test it a little first to make sure everything’s still working properly.

Written by David

December 5th, 2004 at 5:51 pm

Posted in Uncategorized

Tagged with , , ,

Using RSS content with PHP

one comment

RSS feeds and other syndication formats are becoming common and using an aggregator like NetNewsWire, Sharpreader or Bloglines is an easy way to keep up-to-date with your favourite websites without having to visit all of them every day. But what if you want that RSS content on your own website? Here’s a very simple way to reuse RSS weather information from www.rssweather.com with PHP.

I’ll use an example that reads the weather for West Wales and San Francisco because I live in West Wales and I’m visiting California soon. This is a very quick-and-dirty solution that doesn’t make much of the fact that RSS is an XML format; all I want is the temperature and the cloud cover on my website every day.

The feeds from rssweather.com are simple enough to understand – there are a number of <item> nodes in the XML and the first <item> is always the Current Weather and it contains a <content:encoded> node that has the info I want. I can make use of the fact that the content is very highly structured and consistent to extract what I want without having to parse the RSS XML.

RSS is a generalised syndication format. If you wanted to broadcast weather information as XML from scratch you probably wouldn’t want to use RSS because all the interesting data, made of discrete and well-defined items, is collected in the pre-formatted undifferentiated lump of the <content:encoded> node; but RSS has the benefit that it can be consumed by lots of existing applications and that’s why rssweather.com uses RSS. I’ll have to parse the pre-formatted information to extract the few data items in which I’m interested.

Part of the value of the <content:encoded> node always looks like this:

<span class=”sky”>Mostly clear</span> <span class=”temp”>54°F</span>

and it’ll be easy pick this up with a regular expression.

Get the RSS – once

First of all, you need to consume the RSS. Understandably, rssweather.com don’t want you hammering their server so if you’re testing your own application, test with a copy on your own server until you’re happy everything works.

A good way to pick up the RSS feed with PHP is to use the fopen function which can open remote URLs as well as local files. This code opens a file handle, reads the contents into a buffer and closes the file:


$url = "http://http://www.rssweather.com/rss.php?etc
if (!($fp = fopen($url, “r”))) {
die(”could not open RSS source”);
}
$data = fread($fp,8912);
fclose($fp);

We could go straight ahead and extract the two values we want using the PHP function preg_match_all — but instead I’ll show you a simple way of parsing the XML source.

Parsing the RSS / XML

xml_parse_into_struct parses the XML content into two array structures and it’s a bit easier to handle than SAX event-driven processing; it’s a good solution if your XML source isn’t too massive.


$xml_parser = xml_parser_create();
xml_parse_into_struct($xml_parser, $data, $vals, $index);
xml_parser_free($xml_parser);

Next, we find the <content:encoded> node by iterating through the $vals array. Then we can read the value from the [’value’] element:


foreach ($vals as $xml_elem) {
if($xml_elem[’tag’] == ‘CONTENT:ENCODED’){
$weather = $xml_elem[’value’];
break;
}
}

Regexp to get the data

Now we have the content we can use a regular expression to pick out the values we want.


$itemregexp = "%<span class="sky">(.+?)< /span>(.+?) >(.+?)< /span> %is";
$match_count = preg_match_all($itemregexp, $weather, $items);
if($match_count > 0){
$sky = $items[1][0];
$temp = $items[3][0];
}

Save it for later inclusion

We don’t want to hammer rssweather.com’s server so we’ll save this information as a file that we can include into a PHP page. When that’s working ok we can setup a cron job to do it for us once a day.

Write an include file :


$inc = "<dl>";
$inc .= “<dt>Sky</dt>”;
$inc .= “<dt>Temp</dt>”;
$inc .= “<dd>”.$sky.”</dd>”;
$inc .= “<dd>”.temp.”</dd>”;
$inc .= “</dl>”;
$filename = {myfilename}
$handle= fopen($filename,’w');
fwrite($handle, $inc);
fclose($handle);

Written by David

June 14th, 2004 at 12:48 pm

Posted in Uncategorized

Tagged with , ,

Apache and PHP on Windows XP

one comment

A quick guide to installing Apache and PHP on Windows XP.

  1. Setup PHP

    1. Download PHP from php.net. If you want the latest stable version then it’s currently the PHP 4.3.7 zip package (6,895Kb). Unpack to a folder – say, C://PHP
    2. Copy all the dlls in C://php/dlls to C://WINDOWS/SYSTEM32
    3. Copy C://php/php4ts.dll to C://WINDOWS/SYSTEM32
    4. Copy C://php/php.ini-dist to C://WINDOWS and rename it php.ini
    5. Edit C://WINDOWS /php.ini and change the extension_dir line to read:
      extension_dir = "C:/php/extensions"
  2. Setup Apache

    1. Download Apache from apache.org. You’ll want the Win32 Binary (MSI Installer). Run the installer, take the defaults.
    2. Open the Apache config file C:// Program Files/ Apache Group/ Apache2/ conf/ httpd.conf in notepad
    3. Find the section with lots of ‘LoadModule’ directives and at the end of that section add another line that reads:
      LoadModule php4_module c:/php/sapi/php4apache2.dll
    4. Find the line that says:
      DirectoryIndex index.html
      and change it to read
      DirectoryIndex index.php index.html
    5. Find the section that has a line like this:
      #AddType application/x-tar .tgz
      and insert after it this line :
      AddType application/x-httpd-php .php

Restart Apache and you’re done. There’s a lot more to configure in a development environment but this will get you up and running.

Written by David

June 10th, 2004 at 11:36 am

Posted in Uncategorized

Tagged with ,