PHP 5 Recipes A Problem-Solution Approach PHẦN 9 pot

68 229 0
PHP 5 Recipes A Problem-Solution Approach PHẦN 9 pot

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

How It Works In this example, you create and append the tree attributes to the table element and assign them some values. The output will then look like this: <html><body><table width="100%" height="50%" border="1"><tr> <td>value1</td> <td>value2</td> </tr></table></body></html> Because attributes are automatically created, you can reduce this example a bit without impacting the result. The next example shows how to remove the creation and appending of attributes and simply assign the needed attributes to the elements where you need them. The Code <?php // Example 14-4-2.php $root = new DomDocument('1.0', 'iso-8859-1'); $html = $root->createElement("html"); $body = $root->createElement("body"); $table = $root->createElement("table"); $table->setAttribute("width", "100%"); $table->setAttribute("height", "50%"); $table->setAttribute("border", "1"); $row = $root->createElement("tr"); $cell = $root->createElement("td", "value1"); $row->appendChild($cell); $cell = $root->createElement("td", "value2"); $row->appendChild($cell); $table->appendChild($row); $body->appendChild($table); $html->appendChild($body); $root->appendChild($html); echo $root->saveHTML(); ?> The DomElement object also includes methods to check for the existence of an attribute, remove an attribute, and get the value of an attribute. The methods are called hasAttribute(), removeAttribute(), and getAttribute(). These functions all take the attribute name as the only parameter. 14-4 ■ CREATING AND SETTING ATTRIBUTES522 5092_Ch14_FINAL 8/26/05 9:59 AM Page 522 14-5. Parsing XML So far we have been discussing generating HTML and XML documents, but you can also use the DOM extension to load and parse both HTML and XML documents. Unlike XML documents, HTML documents do not have to be well formatted (browsers can render HTML documents with missing end tags), so it is likely to see errors or warnings when these documents are loaded. The DomDocument() class includes methods to parse string values as HTML or XML and methods to load the content directly from a file or as a stream. The next example does not make much sense, but it demonstrates how to read the HTML content directly from a Uniform Resource Locator (URL). The resulting HTML document is then echoed directly to the client. The loadHTMLFile() method is called statically, and this will create the DomDocument() object auto- matically. You can also create the DomDocument() object first and then have the loadHTMLFile() applied to it, with the same result. The Code <?php // Example 14-5-1.php $doc = DOMDocument::loadHTMLFile("http://php.net"); echo $doc->saveHTML(); ?> How It Works This example will load the content of the default HTML document from http://php.net into a DomDocument() object, and it will create the object tree for all elements and child elements for the entire document. The entire document is the echoed back to the browser without any changes. The result from this script is too long to show here, but it might include lines like these: Warning: DOMDocument::loadHTMLFile(): htmlParseEntityRef: no name in http://php.net, line: 119 in Samples/14.10.php on line 2 This indicates that the content includes & or other undefined entities. To be well format- ted, & should be replaced with &amp;. Parsing documents with the DOM extension is more useful if the document is an XML document; as an example, you can use http://slashdot.org/slashdot.xml. This is a docu- ment that provides a list of the current stories on Slashdot. The file is structured with a root element called backslash and a number of story elements, each containing title, url, time, author, and other elements. The basic structure of this file is as follows with a single story entry. The complete file contains multiple story sections. <?xml version="1.0"?><backslash xmlns:backslash="http://slashdot.org/backslash.dtd"> <story> <title>Dell Axim X50 Running Linux</title> <url>http://slashdot.org/article.pl?sid=05/06/15/022211</url> <time>2005-06-15 04:10:00</time> 14-5 ■ PARSING XML 523 5092_Ch14_FINAL 8/26/05 9:59 AM Page 523 <author>timothy</author> <department>tempty-tempty</department> <topic>100</topic> <comments>0</comments> <section>hardware</section> <image>topichandhelds.gif</image> </story> … </backslash> The following code shows a simple script that loads the content of this file into a DOM object tree. The Code <?php // Example 14-5-2.php $slashdot = DOMDocument::load("http://slashdot.org/slashdot.xml"); ■Caution Many sites that provide XML feeds require that you fetch an updated version only at certain intervals. Please respect this, and store a local copy of the file on your own system until it is time to request a new file from the server. For Slashdot, the minimum time between requests is 30 minutes. If the previous code is excecuted too often, it will return errors, as the document read from the server will no longer be a valid XML document. The next example handles the local caching of the document and uses that as long as the local version is valid. The Code <?php // Example 14-5-3.php $local_file = "slashdot.xml"; $ttl = 30 * 60; // Cache in 30 min. if (file_exists($local_file) && filemtime($local_file) > time() - $ttl) { echo "Loading from cache\n"; $slashdot = DOMDocument::load($local_file); } else { echo "Loading from server\n"; $slashdot = DOMDocument::load("http://slashdot.org/slashdot.xml"); $fp = fopen($local_file, "wt"); if ($fp) { fwrite($fp, $slashdot->saveXML()); fclose($fp); 14-5 ■ PARSING XML524 5092_Ch14_FINAL 8/26/05 9:59 AM Page 524 } } ?> How It Works First you define variables for the local document name and the time to live in the cache. Then you check whether the local document exists and whether it is valid. If that is the case, you load the document from the local file. If the document is invalid, you load a new copy from the original website and store that copy on the disk. Loading from server Any other execution of the code will produce output like this: Loading from cache When the document is loaded into the object tree, you can get the different elements by using either getElementsByTagName() or getElementsById(). The first function looks for all the elements in the document where the tag name is equal to the parameter. The second function uses the special attribute called id to build a list of elements. So, if you want to use this document to create a new HTML document that contains a list of all the titles with links to the full stories, you could get the individual stories by starting from the top. You can extract all the story elements with a single call to the getElementsByTagName() method. This will return a list of nodes that can be examined one at the time in a foreach() loop, as shown next. The Code <?php // Example 14-5-4.php $slashdot = DOMDocument::load("http://slashdot.org/slashdot.xml"); $stories = $slashdot->getElementsByTagName("story"); foreach($stories as $story) { $titles = $story->getElementsByTagName("title"); foreach($titles as $title) { echo $title->nodeValue . " - "; } $urls = $story->getElementsByTagName("url"); foreach($urls as $url) { echo $url->nodeValue . "\n"; } } ?> 14-5 ■ PARSING XML 525 5092_Ch14_FINAL 8/26/05 9:59 AM Page 525 How It Works This example uses the special property on the DomElement object, called nodeValue, to extract the actual value for the title and url elements. The getElementsByTagName() method exists on the DomDocument() object as well as the DomElement object. This allows you to scan for the title and URL for a selected element only. The output from this example will look like this: FDA OKs Brain Pacemaker for Depression – http://slashdot.org/article.pl?sid=05/07/21/1657242 Do Not Call List Under Attack - http://slashdot.org/article.pl?sid=05/07/21/1439206 Firefox 1.1 Scrapped - http://slashdot.org/article.pl?sid=05/07/21/142215 World of Warcraft For The Win - http://slashdot.org/article.pl?sid=05/07/21/1341215 Space Shuttle Discovery to Launch July 26 – http://slashdot.org/article.pl?sid=05/07/21/1220218 Microsoft Continues Anti-OSS Strategy – http://slashdot.org/article.pl?sid=05/07/21/1218247 Security Hackers Interviewed - http://slashdot.org/article.pl?sid=05/07/21/1215217 Pay-Per-Click Speculation Market Soaring – http://slashdot.org/article.pl?sid=05/07/21/124230 Websurfing Damaging U.S. Productivity? – http://slashdot.org/article.pl?sid=05/07/21/0132206 VoIP Providers Worry as FCC Clams Up – http://slashdot.org/article.pl?sid=05/07/21/0135213 PHP 5.0 includes a new extension for parsing XML documents called SimpleXML. The SimpleXML extension makes the parsing of files such as slashdot.xml much easier. You can handle the previous example with the following small piece of code. The Code <?php // Example 14-5-5.php $stories = simpleXML_load_file("http://slashdot.org/slashdot.xml"); foreach($stories as $story) { echo $story->title . " - "; echo $story->url . "\n"; } ?> How It Works This code produces the same output as the previous example. The content is loaded directly from the URL, but the resulting SimpleXML object is a list of all the story elements. The backslash element is ignored, because XML files can contain exactly one root-level element. You do not need to call functions or methods to get values or attributes on a SimpleXML object. 14-5 ■ PARSING XML526 5092_Ch14_FINAL 8/26/05 9:59 AM Page 526 These are made available directly on the object structure (such as PHP objects), as shown in the next two examples where the attributes are extracted from the same XML file using first the DOM method and then the SimpleXML method. In the first example, you create a file that con- tains the XML content; in this case, use a short list of books. Each book has an ID defined as an attribute on the book element and a title defined as a child element to the book element. <?xml version="1.0" ?> <! example books.xml > <books> <book book_id="1"> <title>PHP 5 Recipes</title> </book> <book book_id="2"> <title>PHP Pocket Reference</title> </book> </books> ■Note You can use comment elements in XML documents in the same way you use them in HTML. In the next example, you create the script that uses the DOM extension to create a list of title and book_id attributes. The Code <?php // Example 14-5-6.php $doc = DOMDocument::load("books.xml"); $books = $doc->getElementsByTagName("book"); foreach($books as $book) { $titles = $book->getElementsByTagName("title"); foreach($titles as $title) { echo $title->nodeValue . " - "; } $id = $book->getAttribute("book_id"); echo "book_id = $id\n"; } ?> Now create the same example with the SimpleXML extension. The Code <?php // Example 14-5-7.php $books = simpleXML_load_file("books.xml"); 14-5 ■ PARSING XML 527 5092_Ch14_FINAL 8/26/05 9:59 AM Page 527 foreach($books as $book) { echo $book->title . " - "; echo "book_id = $book[book_id]\n"; } ?> How It Works Both examples use the same XML file to create a DOM object tree or a Simple XML tree, and both examples create the same output with the title and book_id attributes for each book: PHP 5 Recipes - book_id = 1 PHP Pocket Reference - book_id = 2 This example uses a small and simple XML file. If the file were more complex, the advan- tages of using SimpleXML to parse the content would be obvious. The SimpleXML extension does not include any features to manipulate the XML document in memory, but both exten- sions have functions that allow for the exchange of documents between the two standards. It’s possible to use the DOM extension to build a document with values from a database or other source and then convert it to SimpleXML before the document is passed to another process for further processing. The advantage of the DOM extension is the ability to add, remove, and change elements and attributes in the object tree. 14-6. Transforming XML with XSL Transforming XML documents to other XML documents or even to HTML documents is an important part of handling XML documents. Before PHP 5.0, you could do this with the XSLT extension. (XSLT stands for XSL Transformations, and XSL stands for Extensible Stylesheet Language.) The XSLT extension was built as a processor-independent application program- ming interface (API) with support for the Sabletron library. Since PHP 5.0, a new extension called XSL is available for transformations, and the XSLT extension has been moved to the PECL repository. The XSL extension builds on libxslt and is available on both Unix and Win- dows platforms. Unlike the DOM and SimpleXML, this extension is not enabled/loaded by default; you must load it from php.ini or with the dl() function. You can also compile it as a static module with no need for loading. You do this by including the –with-xsl option when running the configure script on a Unix platform. If you return to the Slashdot example, where an XML file is loaded into a DomDocument() object, you can use the same file to see how XSL can transform this document to an HTML document that can be included on other web pages. Working with XSL is in many ways similar to how DOM works, though the methods and functions are different. The follow- ing document shows how to create an instance of xsltProcessor(), import a stylesheet, and transform the slashdot.xml document. 14-6 ■ TRANSFORMING XML WITH XSL528 5092_Ch14_FINAL 8/26/05 9:59 AM Page 528 The Code <?php // Example 14-6-1.php if (!extension_loaded("xsl")) { dl("php_xsl.dll"); } $xslt = new xsltProcessor; $xslt->importStyleSheet(DomDocument::load('slashdot.xsl')); $slashdot = new DomDocument("1.0", "iso-8889-1"); $slashdot->preserveWhiteSpace = false; $local_file = "slashdot.xml"; $ttl = 30 * 60; // Cache in 30 min. if (file_exists($local_file) && filemtime($local_file) > time() - $ttl) { $slashdot->load($local_file); } else { $slashdot->load('http://slashdot.org/slashdot.xml'); $fp = fopen($local_file, "wt"); if ($fp) { fwrite($fp, $slashdot->saveXML()); fclose($fp); } } echo $xslt->transformToXML($slashdot); ?> ■Note The code assumes the XSL extension is compiled in on Unix Unix platforms and available as a DLL on Windows platforms. How It Works The biggest differences are that you need to load the XSL extension and that you are working with two documents. The slashdot.xml file is loaded from the local cache or from the remote server (so it will always be up-to-date without violating the rules of usage for the service), and the stylesheet is loaded from the local hard drive. You could use the static method to load the XML file as well, but in this case you want to get rid of whitespace in the XML file, so create a DomDocument() object manually and set the property preserverWhiteSPace to false before you load the document. 14-6 ■ TRANSFORMING XML WITH XSL 529 5092_Ch14_FINAL 8/26/05 9:59 AM Page 529 The stylesheet, called slashdot.xsl, is itself an XML file that includes definitions for how different elements in the slashdot.xml file should be converted. The Stylesheet <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <! Example slashdot.xsl > <xsl:param name="site" select="'slashdot.org'"/> <xsl:output method="html" encoding="iso-8859-1" indent="no"/> <xsl:template match="/"> <html><body><center> <h1>Welcome to latest extract from <xsl:value-of select="$site"/></h1> <table border="1" width="75%"> <xsl:apply-templates/> </table> </center></body></html> </xsl:template> <xsl:template match="story"> <tr> <td> <a> <xsl:attribute name="href"> <xsl:value-of select="url"/> </xsl:attribute> <xsl:value-of select="title"/> </a> </td> <td><xsl:value-of select="author"/></td> </tr> </xsl:template> </xsl:stylesheet> ■Note The root element is named / in the first template. This could also be named backslash in this case, as that is the name of the root element. The template file has two templates. The first one is for the backslash element (the root element in the XML file), and the second template is for the story element. It does not matter which order the two templates are defined in the XSL file. The <xsl:apply-templates/> element used in the first template defines where the second template is inserted. Figure 14-1 shows the output from converting slashdot.xml to an HTML document. 14-6 ■ TRANSFORMING XML WITH XSL530 5092_Ch14_FINAL 8/26/05 9:59 AM Page 530 Figure 14-1. Browser output from converting slashdot.xml to an HTML document 14-7. Using RSS Feeds RSS is an XML standard for syndicating web content. It was originally developed by Netscape but is widely used by many websites. An RSS feed has two parts. The first part is the XML doc- ument that contains one Resource Description Framework (RDF) <rdf:RDF> element and a list of the elements for the actual content. The second part is one or more files described in the rdf tag. These files contain additional descriptive information about the feed’s structure. An easy way to work with RSS feeds is to use the PEAR::XML_RSS class. You can use this class to read the RSS file from the remote server and parse the file so the contents will be stored in a number of PHP arrays. The RSS file has a number of sections that will be converted into a PHP array with the PEAR class (see Table 14-1). Table 14-1. RSS Sections Name Description Channel Information about the channel, the publisher, and so on Items A short list of items with a direct link to the full story Item A detailed description of each item, often with a short abstract of the story Images A list of images provided by the file (can be empty) TextInputs A list of text input fields provided by the file (can be empty) 14-7 ■ USING RSS FEEDS 531 5092_Ch14_FINAL 8/26/05 9:59 AM Page 531 [...]... sale sale sale sale sale sale sale in in in in in in in in in in in in January was 10 February was 12 Marts was 15 April was 19 May was 30 June was 45 July was 12 August was 50 September was 20 October was 34 November was 55 December was 70 54 1 50 92 _Ch14_FINAL 54 2 8/26/ 05 9 : 59 AM Page 54 2 14 -9 ■ USING SOAP 14 -9 Using SOAP So far you have seen techniques to exchange data, where the format is simple and... in a MySQL database 54 9 50 92 _Ch14_FINAL 8/26/ 05 9 : 59 AM Page 55 0 50 92 _Ch 15_ FINAL 8/26/ 05 10:00 AM CHAPTER Page 55 1 15 ■■■ Using MySQL Databases in PHP 5 A n important aspect of web development is being able to collect, store, and retrieve many different forms of data In the past, different methods have been created to handle such features Flat files, which are essentially text-based informational files,... type="xsd:int"/> 54 3 50 92 _Ch14_FINAL 54 4 8/26/ 05 9 : 59 AM Page 54 4 14 -9 ■ USING SOAP The SoapClient() class has a method called soapCall() that you can use to create and... standard for many years After many problems with portability, speed, and functionality, flat files were generally phased out in favor of true database applications Many database solutions are available on the Internet, including Microsoft Access, SQL Server, Oracle, and a few others Out of the pack of available options, however, one piece of database software has proven repeatedly to be a robust, affordable... is as follows: bool mysql_close ( [resource link_identifier] ) 50 92 _Ch 15_ FINAL 8/26/ 05 10:00 AM Page 55 3 15- 2 ■ QUERYING THE DATABASE 15- 2 Querying the Database Naturally, once you have a connection to the database, you will query the database Queries come in many shapes and forms and can have a wide variety of arguments to pass to them MySQL makes sufficient use of Structured Query Language (SQL) and... affordable solution MySQL is the database of choice in the opensource community because of its powerful infrastructure, fast querying, large data storage capabilities, and robust features Basic Database Concepts This chapter presents a few examples of powerful PHP and MySQL-based technology You will learn how to connect to a database, store information in a database, and retrieve information from a database;... wddx_serialize_value() Serializes a single value into a WDDX packet wddx_serialize_vars() Serializes variables into a WDDX packet WDDX works as a packet format, and each document contains one packet One packet can be a single variable or any number of simple or complex variables The next example shows how to create a simple WDDX document with a single variable The Code < ?php // Example 14-8-1 .php $var =... http://www .php. net/soap This is a soap integration for PHP (pear package) This is a soap integration for PHP (pear package) http://pear .php. net/package-info .php? pacid=87 SOAP Client/Server for PHP, PHP License ?? Current Release 0 .9. 1 (beta) was released on 20 05- 05- 31 Implementation of SOAP protocol and services http://pear .php. net/SOAP PHP SOAP list... 20 05" ); wddx_add_vars($pid, "months"); wddx_add_vars($pid, "sales"); echo wddx_packet_end($pid); ?> 50 92 _Ch14_FINAL 8/26/ 05 9 : 59 AM Page 54 1 14-8 ■ USING WDDX How It Works In this case, you embed more than one variable into a single WDDX packet You do this by creating a packet handle called $pid with the wddx_packet_start() function You use the packet handle each time you want to add a new variable... version='1.0'>PHP Packet Creating a WDDX document with a single value. 53 9 50 92 _Ch14_FINAL 54 0 8/26/ 05 9 : 59 AM Page 54 0 14-8 ■ USING WDDX The next example shows how to use the previous example, with HTTP to transfer data , from one server to another The Code < ?php // Example 14-8-2 .php $fp = fopen("http://localhost/14-7-1 .php" , "rt"); . functions all take the attribute name as the only parameter. 14-4 ■ CREATING AND SETTING ATTRIBUTES522 50 92 _Ch14_FINAL 8/26/ 05 9 : 59 AM Page 52 2 14 -5. Parsing XML So far we have been discussing generating. FEEDS532 50 92 _Ch14_FINAL 8/26/ 05 9 : 59 AM Page 53 2 RSS feeds are available from a broad range of servers and organizations, and you can use the simple script in the previous example to create a script. output: The sale in January was 10 The sale in February was 12 The sale in Marts was 15 The sale in April was 19 The sale in May was 30 The sale in June was 45 The sale in July was 12 The sale in August

Ngày đăng: 12/08/2014, 16:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan