Bloglines to My Web: OPML2Bookmarks.php

I'm not sure why Del.icio.us and Yahoo! MyWeb don't support importing from OPML, since that's pretty much a semi-standard for bookmarks, but they don't. This probably has to do with the idea that OPML is sorta semi-XML... They do, however have other import options, so at least there's something - both services let you import Netscape Bookmarks, and My Web lets you import RSS feeds as well.

So to I whipped up a quick php script that'll suck in my Bloglines OPML list and export it as a bookmark. I didn't put a lot of effort into it to make it a generic OPML exporter because Bloglines only allows one level of folders. Here's what it looks like:

<?php

   $url = $_GET['url'];

   $xml = simplexml_load_file($url);

?>
<!DOCTYPE NETSCAPE-Bookmark-file-1>
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>

<?php

   foreach($xml->xpath("//outline") as $outline ):

      $title = htmlspecialchars($outline['title'], ENT_QUOTES);
      $link = htmlspecialchars($outline['htmlUrl'], ENT_QUOTES);
      $rsslink = $outline['xmlUrl'];

      if($rsslink){

?>

   <DT><A HREF="<?= $link ?>"><?= $title ?></A>

<?php
      } else {

?>
    </DL><p>
    <DT><H3><?= $title ?></H3>
    <DL><p>

<?php   
      }

?>

<?php endforeach; ?>

Then I got the bright idea that I wanted to do an RSS export, so I whipped that up:

<?php

   $url = $_GET['url'];

   $xml = simplexml_load_file($url);

   header("Content-Type: application/xml");

   echo '<?xml version="1.0"?>';
?>

<rss version="2.0">
   <channel>
      <title><?= $xml->head->title ?></title>
      <link><?= $url ?></link>
      <description>OPML Export</description>

<?php

   foreach($xml->xpath("//outline[@htmlUrl !='']") as $outline ):

      $title = htmlspecialchars($outline['title'], ENT_QUOTES);
      $link = htmlspecialchars($outline['htmlUrl'], ENT_QUOTES);
?>
      <item>
         <title><?= $title ?></title>
         <link><?= $link ?></link>
      </item>

<?php endforeach; ?>

   </channel> 
</rss>

Once I was done with those though, I realized that they require PHP 5.0 because of the SimpleXML object, so I re-did the RSS exporter again using xml_parser, which is neat, but a lot less flexible (no XPath). Here's that:

<?php

   $url = $_GET['url'];

   $response = file_get_contents($url);

   $parser = xml_parser_create();
   xml_parse_into_struct( $parser, $response, $values );
   xml_parser_free( $parser ); 

   header("Content-Type: application/xml");

   echo '<?xml version="1.0"?>';

?>

<rss version="2.0">

   <channel>
        <title><?php echo $values[2]['value'] ?></title>
        <link><?php echo $url ?></link>
        <description>OPML Export</description>

<?php

        foreach( $values as $element ) {

          if( $element['tag'] === 'OUTLINE' && $element['attributes']['HTMLURL'] != '') {

            $title = htmlspecialchars($element['attributes']['TITLE'], ENT_QUOTES);
            $link = htmlspecialchars($element['attributes']['HTMLURL'], ENT_QUOTES);

?>
        <item>
                <title><?php echo $title ?></title>
                <link><?php echo $link ?></link>
        </item>

<?php
                }
        }
?>

   </channel>
</rss>

To use this stuff, save which ever version you want to a file called something like opml2bookmarks.php, and then pass it the URL of your Bloglines public opml file. Example: http://www.example.com/opml2bookmarks.php?http://www.bloglines.com/export?id=russb . They're like 30 line php scripts, so I'm sure you can figure them out if you've got any familiarity with PHP - also since there's no error checking, it should barf at you with the appropriate problem if you have one. DON'T email me with tech support questions, though if there's a better way (short lines of code?) then please, by all means send them this way.

The neat thing about the Bookmarks one is that it snagged the folder names as tags... I wonder if I added them in as categories to the RSS feed if that would work? Hmm. Well, as that itch is scratched for now, I'll let someone else mess with it. :-)

Enjoy.

-Russ

< Previous         Next >