Tuesday, August 29, 2006

Display extracts form my Website on blogs - or other pages

We're running several websites and a few blogger blogs. The websites are PHP-MySQL driven and the new (small) challenge was to show extracts from the sites in the blogger sidebar.

The easiest method I found to include something into blogger is via javascript:
<script type="text/javascript" src="URL_of_remote_file_outputting_javascript"></script>

On my end I need something producing the necessary javascript (which boils down to essentially plain html in my case).
Two PHP scripts do the job.
The first looks like this:
<?php
ob_start();

include("php_script_producing_the_actual_html.php");
$string = ob_get_contents();
ob_end_clean();
$string = str_replace(chr(10), "", $string); //NL
$string = str_replace(chr(11), "", $string); //vertical tab
$string = str_replace(chr(12), "", $string); //NP
$string = str_replace(chr(13), "", $string); //CR
$string = addslashes($string);

print "document.writeln ('$string')";
?>
I found this essential hint somewhere googling around - can't find it anymore now. I'd like to put a link to the original here (especially since it may additionally explain the str_replace commands) - so please let me know if you come across it.

As you can see the second script is called by the first one. It just connects to the database and echoes out the html I want to appear on my blogger sidebar.

I also added some variables into the inital call to modify the output from the blogger side.
For example the first php file might call a second file which is defined in by the calling site.
Let's take the theory above into my practical example:
1. I've got a php file called resto_blogger.php which spits out some html by using echo generated with data from my database (this is the second file using the above nomenclature).
2. I've got a generic php file called blogger_js.php which accepts a GET variable to choose the page to call.
3. The call from the blog passes this variable.

content of blogger_js:
<?php
ob_start();

here some code to check the validity of the $_GET variables
I also save the $_GET variable 'page' into $page


include($page."_blogger.php");
$string = ob_get_contents();
ob_end_clean();
$string = str_replace(chr(10), "", $string); //NL
$string = str_replace(chr(11), "", $string); //vertical tab
$string = str_replace(chr(12), "", $string); //NP
$string = str_replace(chr(13), "", $string); //CR
$string = addslashes($string);

print "document.writeln ('$string')";
?>

call from the blog:
<script type="text/javascript" src="http://mysite.bla/blogger_js.php?page=resto"></script>

One more thing...
The sites being in French there are a lot of special characters. To display them correctly just tell blogger which charset to expect in the script tag:
<script type="text/javascript" src="http://mysite.bla/blogger_js.php?page=resto" CHARSET="ISO-8859-1"></script>

1 Comments:

Blogger macspert said...

I found my source again. Please find the original article here:
http://www.aviatorrecords.com/design/2005/04/php-blogger-post-pages.htm

3/9/06 11:46  

Post a Comment

<< Home