How to use PHP to insert the RDF content into the Web site (4)

zhaozj2021-02-16  49

Return to class (Back to Class)

Since you have such a big power, why should you limit your own RDF source? As I have said earlier, most of the main sites often make snapshots for what they provide. In fact, insert all these different sources into your site is quite simple. Let us see how it is done.

First, we modularize the code in the previous example. In this way, you don't have to overwrite the same code over again over again. The simplified method is to pack it, and then this class contains this class into my PHP script.

The class code is as follows:

Class RdfParser

{

//

// Variables

//

// set up local variables for this class

Var $ currentTAG = ""

VAR $ FLAG = ""

Var $ count = 0;

// this is an associative array of channel data with keys

("Title", "LINK", "Description")

Var $ CHANNEL = array ();

// this is an array of arrays, with Each Array ELEMENT

Representing an

// Each Outer Array Element is 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 May 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 Parsresource ()

{

// 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 Or block

IF ($ Name == "Item")

{

$ this-> Flag = 1;

}

Else IF ($ name == "channel")

{

$ this-> Flag = 2;

}

}

// Closing Tag Handler

Function Elementend ($ Parser, $ Name)

{

$ this-> currittag = ""

// set flag if exitation Or 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-> currittag ==

"LINK" || $ this-> currenttag == "description")

{

// add data to $ channels [] or $ items [] array

IF ($ this-> flag == 1)

{

$ this-> items [$ this-> count] [STRTOLOWER ($ this-> currittag)]. = $ data;

}

ELSE IF ($ this-> flag == 2)

{

$ this-> Channel [STRTOLOWER ($ this-> currittag)]. = $ data;

}

}

}

// Return an associative array containing channel information

// (The $ channel [] array)

Function getChannelInfo ()

{

Return $ this-> channel;

}

// Return an associative array of arrays containing itemformation

// (The $ item [] array)

Function GetItems ()

{

Return $ this-> item;

}

}

?>

If you are more familiar with the PHP class, then understand this code is quite easy. If you don't know much, please jump directly to the link to the end of the article, see a good article about the principle of work. Then come back to continue reading the code above.

Before using this class, I have to take a few minutes to point out a line of code - the line on the XML_SET_Object () function is called.

The current problem is how to use this class to actually generate a web page with multiple content sources.

Include ("Class.rdfParser.php");

// How much items to display in Each Channel

$ MAXITEMS = 5;

?>

// Get and Parse FreshMeat.Net Channel

$ f = new rdfparser ();

$ f-> setResource ("http://www.freshmeat.net/backend/fm-releases.rdf");

$ f-> parsresource ();

$ F_CHANNEL = $ f-> getChannelInfo ();

$ f_items = $ f-> getItems ();

// Now format and print it ...

?>

The Latest from >

$ F_CHANNEL ["Title"];?>

    For ($ x = 0; $ x <$ maxItems; $ x ) {

    IF (Is_Array ($ F_ITEMS [$ x])))

    {

    // Print Data

    $ item = $ f_items [$ x];

    Echo "

  • ".

    $ item ["title"]. "";

    }

    }

    ?>

Primary Page Content Here

- third cell ->

// Get and Parse Slashdot.org Channel

$ s = new rdfparser ();

$ S-> SetResource ("http://slashdot.org/slashdot.rdf");

$ S-> Parseratesource ();

$ S_CHANNEL = $ S-> getChannelInfo ();

$ S_Items = $ S-> GetItems ();

// Now format and print it ...

?>

The Latest from

$ S_CHANNEL ["Title"];?>

    For ($ x = 0; $ x <$ maxItems; $ x ) {

    IF (is_ARRAY ($ S_ITEMS [$ x])))

    {

    // Print Data

    $ Item = $ s_items [$ x];

    Echo "

  • ".

    $ item ["title"]. "";

    }

    }

    ?>

    This code is quite simple. Once you use the "New" keyword to generate an instance of a class,

    $ f = new rdfparser ();

    Then you can use the class method to set the location of the RDF file to be analyzed,

    $ f-> setResource ("http://www.freshmeat.net/backend/fm-releases.rdf");

    $ f-> parsresource ();

    And get $ CHANNEL and $ ITEMS arrays for later processing.

    $ F_CHANNEL = $ f-> getChannelInfo ();

    $ f_items = $ f-> getItems ();

    ?>

    The Latest from >

    $ F_CHANNEL ["Title"];?>

      For ($ x = 0; $ x <$ maxItems; $ x ) {

      IF (Is_Array ($ F_ITEMS [$ x])))

      {

      // Print Data

      $ item = $ f_items [$ x];

      Echo "

    • ".

      $ item ["title"]. "";

      }

      ?>

      Each time you reload the above script, the corresponding RDF file will be taken from a specific location. After analysis, it is displayed as required format.

      If your site has high visits, you may feel that our hardships are meaningless, especially when the RDF data used is not so fast, the situation is worse. In this case, perhaps exploring the Local Cache RDF data is a wise approach: either expand the above examples, join the cache function; either for a long time for a long time to download a new RDF The local copy of the file is on your web server, and then use this local copy, not the "live" (the "Live" one.

      转载请注明原文地址:https://www.9cbs.com/read-27958.html

      New Post(0)
      CopyRight © 2020 All Rights Reserved
      Processed: 0.045, SQL: 9