How to insert RDF content into a Web site with PHP (iv)

Source: Internet
Author: User
Tags format array arrays include net variables php class php script
web| Insert | The site returns to the class (Back to Class)

Since you have so much power, why should you limit yourself to just a single source of RDF? As I said earlier, most major sites often take snapshots of what they provide. In fact, it's fairly easy to insert all these different sources into your site. Let's see how it's done.

First, we modularize the code in the previous example. In this way, you don't have to rewrite the same code over and over again for each individual source. The simplified approach is to package it into a class, and then include this class in my PHP script.

The class code is as follows:

?
Class Rdfparser
{
//
Variables
//

Set up local variables to this class
var $currentTag = "";
var $flag = "";
var $count = 0;

This is a associative array of channel data with keys
("title", "link", "description")
var $channel = array ();

This is a array of arrays with each array element
Representing an <item>
Each outer array element was itself an associative array
With keys ("title", "link", "description")
var $items = array ();


//
Methods
//

Set the name of the RDF file to parse
This is usually a local file
You could set it to a remote file if your PHP build supports
URL fopen ()
function Setresource ($file)
{
$this->file = $file;
}


Parse the RDF file set with Setresource ()
This populates the $channel and $items arrays
function Parseresource ()
{
Create parser
$this->xp = Xml_parser_create ();

Set object reference
Xml_set_object ($this->xp, $this);

Set handlers and parser options
Xml_set_element_handler ($this->xp, "Elementbegin",
"Elementend");
Xml_set_character_data_handler ($this->xp,
"Characterdata");
Xml_parser_set_option ($this->xp,
Xml_option_case_folding, TRUE);
Xml_parser_set_option ($this->xp, Xml_option_skip_white,
TRUE);

Read XML file
if (!) ( $fp = fopen ($this->file, "R"))
{
Die ("Could not read $this->file");
}

Parse data
while ($xml = Fread ($fp, 4096))
{
if (!xml_parse ($this->xp, $xml, feof ($FP))
{
Die ("XML parser Error:".)
Xml_error_string (Xml_get_error_code ($this->xp)));
}
}

Destroy parser
Xml_parser_free ($this->xp);
}

Opening Tag Handler
function Elementbegin ($parser, $name, $attributes)
{
$this->currenttag = $name;
Set flag if entering <channel> or <item> block
if ($name = = "ITEM")
{
$this->flag = 1;
}
else if ($name = = "CHANNEL")
{
$this->flag = 2;
}
}

Closing Tag Handler
function Elementend ($parser, $name)
{
$this->currenttag = "";

Set flag if exiting <channel> or <item> block
if ($name = = "ITEM")
{
$this->count++;
$this->flag = 0;
}
else if ($name = = "CHANNEL")
{
$this->flag = 0;
}
}

Character Data Handler
function Characterdata ($parser, $data)
{
$data = Trim (Htmlspecialchars ($data));
if ($this->currenttag = = "TITLE" | | $this->currenttag = =
"LINK" | | $this->currenttag = "DESCRIPTION")
{
Add data to $channels [] or $items [] array
if ($this->flag = = 1)
{

$this->items[$this->count][strtolower ($this->currenttag)]. = $data;
}
else if ($this->flag = 2)
{

$this->channel[strtolower ($this->currenttag)]. = $data;
}
}
}

Return a associative array containing channel information
(The $channel [] array)
function Getchannelinfo ()
{
return $this->channel;
}

Return a associative array of arrays containing item
Information
(The $items [] array)
function GetItems ()
{
return $this->items;
}

}
?>
If you are familiar with the PHP class, then it is quite easy to understand this piece of code. If you do not understand, then skip to the link section at the end of the article to see a good article about the working principle of the class. Then go back and read the code above.

Before using this class, I'll take a few minutes to point out one line of code-that is, the line above the Xml_set_object () function call.

The question now is how to use this class to actually generate a Web page with multiple content sources.

?
Include ("Class.") Rdfparser.php ");
How many items to display in each channel
$maxItems = 5;
?>
<basefont face= "Verdana" >
<body>

<table width= "100%" border= "0" cellspacing= "5" cellpadding= "5" > <tr>
<!--the--> of the cell
&LT;TD Valign=top align=left>
<font size= "-1" >
?
Get and Parse Freshmeat.net channel
$f = new Rdfparser ();
$f->setresource ("HTTP://WWW.FRESHMEAT.NET/BACKEND/FM-RELEASES.RDF");
$f->parseresource ();
$f _channel = $f->getchannelinfo ();
$f _items = $f->getitems ();
Now format and print it ...
?>
The latest from <a href=<? echo $f _channel["link"]; >><? Echo
$f _channel["title"];?></a> <br> <ul> Iterate through items array
for ($x =0; $x < $maxItems; $x + +) {
if (Is_array ($f _items[$x]))
{
Print data
$item = $f _items[$x];
echo "<li><a href=". $item ["link"]. ">".
$item ["title"]. "</a>";
}
}
?>
</ul>
</font>
</td>

<!--second cell-->
&LT;TD Align=center width=50%>
<i>primary page Content here</i>
</td>

<!--third cell-->
&LT;TD Valign=top align=left>
<font size= "-1" >
?
Get and Parse slashdot.org channel
$s = new rdfparser ();
$s->setresource ("HTTP://SLASHDOT.ORG/SLASHDOT.RDF");
$s->parseresource ();
$s _channel = $s->getchannelinfo ();
$s _items = $s->getitems ();
Now format and print it ...
?>
The latest from <a href=<? echo $s _channel["link"]; >><? Echo
$s _channel["title"];?></a> <br> <ul> Iterate through items array
for ($x =0; $x < $maxItems; $x + +) {
if (Is_array ($s _items[$x]))
{
Print data
$item = $s _items[$x];
echo "<li><a href=". $item ["link"]. ">".
$item ["title"]. "</a>";
}
}
?>
</ul>
</font>
</td>

</tr>
</table>

</body>


This piece of code is fairly straightforward. Once you use the "new" keyword to generate an instance of a class,

$f = new Rdfparser ();

You can then use the class method to set the location of the RDF file to parse.

$f->setresource ("HTTP://WWW.FRESHMEAT.NET/BACKEND/FM-RELEASES.RDF");
$f->parseresource ();
And get the $channel and $items arrays for later processing.



?
$f _channel = $f->getchannelinfo ();
$f _items = $f->getitems ();
?>

The latest from <a href=<? echo $f _channel["link"]; >><? Echo
$f _channel["title"];?></a> <br> <ul> Iterate through items array
for ($x =0; $x < $maxItems; $x + +) {
if (Is_array ($f _items[$x]))
{
Print data
$item = $f _items[$x];
echo "<li><a href=". $item ["link"]. ">".
$item ["title"]. "</a>";
}
}
?>
</ul>


Each time you reload the above script, the corresponding RDF file is taken from a specific location, and after analysis, it is displayed in the required format.

If your site has a high volume of traffic, you may find our hard work meaningless, especially if the RDF data is not being updated as quickly. In this case, perhaps it would be wise to explore the local caching of RDF data: either extend the above example program to include caching in it, or spend a few hours a day on downloading a local copy of the latest RDF file to your Web server, and then use this local copy, Rather than the "live" one.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.