0 Replies - 316 Views - Last Post: 04 August 2008 - 09:26 PM

#1 akozlik   User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 798
  • Joined: 25-February 08

DIC Blog Snippet

Posted 04 August 2008 - 09:26 PM

Description: You only need to set a few variables

Set the value of $url to the RSS feed link to your blog. Mine is:

http://www.dreaminco...4&req=syndicate

Finally, store your blog title in the $blogtitle variable as follows:

$blogtitle = "Knowledge is Power";

This will keep your parser from displaying your blog title and the description "blogtitle Syndication".

Finally, I know this isn't the cleanest code. By all means, clean it up for your uses and do what you need with it. This was just a quick and dirty method for accomplishing something I needed.This snippet will parse your DIC blog for use on your website. I currently have it set up to only pull the title of each blog post, as well as the first 10 words of each post. You could then link from the blurb to the actual post on DIC.

I'm sure this could be adapted to whatever rss feed you are using.
<?php

// Set URL to blog syndication RSS feed
$url = "http://www.dreamincode.net/forums/index.php?automodule=blog&blogid=184&req=syndicate";

// Set your title of the blog
$blogtitle = "Knowledge is Power";
$syndication = $blogtitle . " Syndication";

// Initialize your curl handler
$ch = curl_init();

// Set a few Curl options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FAILonerror, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Store the resulting XML file into $result
$result = curl_exec($ch) or die("Could not curl");

// Instantiate your xml parser
$xml_parser = xml_parser_create();

// Parse into an associative array
xml_parse_into_struct($xml_parser, $result, $vals, $index) or die ("Could not parse XML");

// Free up your parser's memory
xml_parser_free($xml_parser);

// Loop through each element of the XML that was stored in the array $vals
for ($i=0; $i<sizeof($vals); $i++) {
  //Check if the tag type is TITLE and that the value is not the title of your blog
  if ($vals[$i]['tag'] == "TITLE" && $vals[$i]['value'] != $blogtitle) {    
      echo "<b>" . $vals[$i]['value'] . "</b>";
      echo "
";
  }
  
  // Check if the tag type is description and if so, that it is not "blog title Syndication";
  if ($vals[$i]['tag'] == "DESCRIPTION" && $vals[$i]['value'] != $syndication) {
    $string = $vals[$i]['value'];
    
    // Split the description into an array of words
    $words = str_word_count($string, 2);
    
    // Get the first 5 words
    $firstwords = array_slice($words, 0, 10);
    
    // Implode the array
    $blurb = implode(' ', $firstwords);
    
    // Echo out your blurb
    echo $blurb . " . . . more

";
  }
}

?>



Is This A Good Question/Topic? 0
  • +

Page 1 of 1