Using PhpBase to update GoogleBase

I’m not sure I quite “get” GoogleBase just yet – in as much as Google is already indexing my sites so why do I need to submit my content all over again into this system… and who would actually use it to find stuff – surely the standard Google Search Engine is doing a good enough job?

I received my newsletter from the phpClasses today and noticed that there was a tool to make it easier to upload articles and information to GoogleBase so I thought I’d give it a try.

PhpBase

The code is still quite new and there are only a few options but it’s easy enough to extend and use. Documentation is needed and I’m sure it will follow. Given that the author appears to be Spanish I think it’s already pretty good.

Safemode Fix

I’ve only found one line in the mass of files which needs to be changed. It’s line 31 of AkFtpClient.php. Change the w+ to a+
[php] $temp = fopen($tmpfname, ‘a+’);[/php]

RSS Example

My test was for the articles in this blog so first up I needed a schema file for articles. I called the file Article.php and extended the _attributes according to the list at Google Base – RSS 2.0 Specification
[php]< ?php $schema = array( '_attributes' => array(‘title’,’description’,’link’,’id’,’author’,’expiration_date’, ‘label’, ‘news_source’, ‘pages’, ‘publish_date’, ‘expiration_date_time’, ‘image_link’),
‘_not_null’ => array(‘title’,’description’,’link’,’id’),
‘_default_values’ => array(),
‘_multiple_choice’ => array()
);

?>
[/php]

Next up I had to be able to “render” the RSS file – and there’s no standard file for this either. So I created RSS2.php
[php]< ?php !defined('DS') ? define('DS', DIRECTORY_SEPARATOR) : null; !defined('PHPBASE_DIR') ? define('PHPBASE_DIR', str_replace('PhpBase'.DS.'renders'.DS.'Tabbed.php','',__FILE__)) : null; require_once PHPBASE_DIR.'PhpBase'.DS.'GoogleBaseRender.php'; class RSS2Render extends GoogleBaseRender { function render($data_rows) { global $_SERVER; $rss2_file = '


GoogleBase Upload ‘.$_SERVER[‘HTTP_HOST’].’ Articles and information for GoogleBase
‘;
foreach ($data_rows as $data_row)
{
$line = “
{$data_row->title} {$data_row->link} {$data_row->description}\n”;
foreach ($this->schema as $column)
{
if (!in_array($column, array(‘title’, ‘description’, ‘link’)) && !empty($data_row->$column))
{
$line .= “{$data_row->$column}\n”;
}
}
$line .= “
\n”;

$rss2_file .= $line;
}
$rss2_file .= “\n“;
return $rss2_file;
}
}
?>[/php]

Finally all I had to do was create my script to run it all
[php]< ?php error_reporting(E_ALL); require_once 'PhpBase.php'; require_once('/path/to/dbsetupscript.php'); $test = new PhpBase('Article'); $sql = "select `id`, `post_title`, `post_content`, `post_name` from `wp_posts` where `post_status` = 'publish' order by `id` DESC"; $result = mysql_query($sql) or die(mysql_error() . '
‘.$sql);

while ($row = mysql_fetch_array($result))
{
$bits = explode(‘post_content’, $row[‘post_content’]);
$bits = explode(‘[‘, $bits[0]);

$test->addItem( array(
‘title’ =>$row[‘post_title’],
‘description’=>$bits[0],
‘link’ =>”http://www.itamer.com/{$row[‘post_name’]}/{$row[‘id’]}/”,
‘id’ =>$row[‘id’],
‘author’ =>’Sarah King’
));
}

//this puts it to the screen
//header(“Content-type: text/xml; charset=UTF-8”);
//echo $test->render(‘RSS2’);

//this sends it to google
echo $test->send(‘uname’, ‘pwd’, ‘feed.xml’);

?>[/php]

I still have a few issues but lets see how it goes…

Categories

Recent Comments

Tags

6 Comments

  1. February 11, 2006

    Hi Sarah I’m Bermi Ferrer from PhpBase,

    First of all thanks for giving PhpBase a try and posting about it on your Blog.

    $temp = fopen($tmpfname, ‘a+’);
    Why should you want to use the ‘a+’ append options for the fopen() function in the Ftp class? Did you have any strange behaviour? Did you try to change it for ‘w’ instead of ‘a+’?

    Regarding the render, I already had a Rss2 render (http://svn.berlios.de/wsvn/phpbase/trunk/PhpBase/renders/Rss2.php?op=file&rev=0&sc=0), but I haven’t give it use it at all. I’ve uploaded to the SVN repository in case you want to give it a try.

    I’ve seen that you have created an Articles shema, did you tried the NewsAndArticles.php (http://svn.berlios.de/wsvn/phpbase/trunk/PhpBase/schemes/NewsAndArticles.php?op=file&rev=0&sc=0) schema included in PhpBase?

    There are a couple of bugs that have already been addressed on the source repository at http://svn.berlios.de/wsvn/phpbase, now for example you can set the render and its options on the send() method like this:

    [php]< ?php $PhpBase->send(‘user_name’, ‘password’, ‘file_name.xml’,
    array(
    ‘format’=>’Rss2’,
    ‘title’=> ‘My Rss2 channel title’,
    ‘description’=> ‘My Rss2 channel description’,
    ‘link’=> ‘My Rss2 channel link’
    )
    );
    ?>[/php]

    Regarding the documentation for the project I’m really ashamed for that, but it will come soon.

  2. February 11, 2006

    Hi Berni

    I just used the code in the download file and missed all those extra bits in the repository. Perhaps there should be something on the PhpBase site to make it clearer that people should look there.

    As for the a+ -> New Zealand webhosts have a fascination with safe_mode and no number of examples and explanations will convince them otherwise. Most of the time it’s no big deal but from time to time you just want to scream. In this case the original code wouldn’t work until I made the change. You can lose alot of time fighting little things like this 🙂

    Now, my outstanding problem, and I’ll check to see if your RSS code has a different output, is that Google won’t validate my feed. Grrr! I copied the code from their site so it jolly well should! Here’s where I have asked for assistance:

    1. Google Base Help Discussion > Bulk uploads > RSS2.0: Content is not allowed in prolog.
    2. DigitalPoint > Google Base: Bulk Upload: RSS2.0: Content is not allowed in prolog

    In the meantime there’s only one movie theatre in town still showing the Harry Potter movie and they have only one session this weekend. I promised the kids they could see it at the movies on a wet day and if I want to avoid being the “worst mother” I’d better hot foot it out the door! Shame it’s a beautiful day 😉

  3. February 12, 2006

    “I just used the code in the download file and missed all those extra bits in the repository. Perhaps there should be something on the PhpBase site to make it clearer that people should look there.??

    On the PhpBase’s left menu you can find the option “Souce Code” http://www.phpbase.org/source_code where you have a link to the Subversion repository, and on the top menu a link to the Berlios development site http://developer.berlios.de/projects/phpbase/ 😀

    “Now, my outstanding problem, and I?ll check to see if your RSS code has a different output, is that Google won?t validate my feed. Grrr??

    At the end of the google base Rss2 specs page http://base.google.com/base/rss_specs.html you have some samples, and surprisingly http://base.google.com/base/news2.xml doesn’t have a prolog like the ones they recommend.

    Have you tried uploading your articles as tabbed data?

  4. February 12, 2006

    I wanted to take this opportunity to congratulate you on a very high quality Blog. I have personally added your Blog to the http://www.nubbit.com Blog directory as I feel it meets our high acceptance standards providing our site visitors the quality Blog content they are in search of.

    [snip]brown nosing[/snip]

    Congratulations and thanks in advance,
    Branden Moskwa, co-founder
    http://www.nubbit.com

  5. February 24, 2006

    Thanks for the rss example. Hopefully I will be able to use that on my blog someday.
    Keep up the good work.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.