Web Technology Solutions Class: Application Syndication: Parse and Publish RSS & XML Data Date : 3/3/2010 Tonight ✤ Introduction to XML ✤ Creating XML, RSS, and JSON with PHP ✤ using simpleXML for XML\RSSLab Lab Preview ✤ Application Development ✤ Registration Feature ✤ Login\Logout ✤ Maintain State ✤ Admin CRUD for Survey ✤ Publish XML and RSS Lab Preview ✤ Application Development: ✤ Adding XML and RSS ✤ ✤ Publish an RSS feed of your survey responses ✤ Publish an XML dump of your survey responses You will implement an ECO to provide XML publication of a table in your database. Provide a URL of feed. Final Project Review ✤ Final Project - Web App (link) ✤ registration feature (week 6) ✤ login logout (week 7) ✤ admin ability to create\read\update\delete (CRUD) (week 4-5) ✤ Maintain State throughout app (cookies\sessions) (week 7) ✤ XML and RSS feeds (week 8) ✤ Valid XHTML and CSS design Web Apps ✤ Problem with apps: ✤ Data stored in other systems can’t be shared easily. ✤ Data is in different formats ✤ Data has no meaning to other systems, just its own native system. Web Apps ✤ Goals: ✤ Share information across multiple environments ✤ Open, flexible language to share data ✤ Provide schematic meaning to data elements Solution: XML Intro to XML ✤ Markup language for documents containing structured information. ✤ Extensible Markup Language (akin to HTML) ✤ Supported by W3C as a standard ✤ Provides known structure and relevant meaning to that data structure. ✤ Widely used (iTunes, e.g.) ✤ Perfect for PHP developers to Terms of XML Meta: literally “provides ✤ meaning”. ✤ Schema: A logical organized structure of data. ✤ Tags: Elements in XML that are data containers ✤ Attributes: Extend meaning of a tag ✤ Serialize: saving or storing data ✤ Nodes: a DOM structure of a data object (tree) XML Sample ✤ Consider this example: ✤ <?xml version=”1.0”?> ✤ <product barcode=”2323423”> ✤ <maker>Smithson</maker> ✤ <name>Apex DVD Player 1.2</name> ✤ <quantity>3</quantity> ✤ <size>33cm</size> ✤ <color>black</color> ✤ </product> XML Syntax Structure ✤ Consider this example: ✤ <?xml version=”1.0”?> // XML Definition (version) ✤ <product barcode=”2323423”> ✤ <maker>Smithson</maker> ✤ <name>Apex DVD Player 1.2</name> ✤ <quantity>3</quantity> ✤ <size>33cm</size> ✤ <color>black</color> ✤ </product> XML Syntax Structure ✤ Consider this example: ✤ <?xml version=”1.0”?> // XML Definition (version) ✤ <product barcode=”2323423”> // Extending Tagging System (user defined markup adds meaning) ✤ <maker>Smithson</maker> ✤ <name>Apex DVD Player 1.2</name> ✤ <quantity>3</quantity> ✤ <size>33cm</size> ✤ <color>black</color> ✤ </product> XML Syntax Structure ✤ Consider this example: ✤ <?xml version=”1.0”?> // XML Definition (version) ✤ <product barcode=”2323423”> // Extending Tagging System (user defined markup adds meaning) ✤ <maker>Smithson</maker> //Tags are created and defined by the developer (XSD) XML Schema ✤ <name>Apex DVD Player 1.2</name> ✤ <quantity>3</quantity> ✤ <size>33cm</size> ✤ <color>black</color> ✤ </product> XML Syntax Structure ✤ Consider this example: ✤ <?xml version=”1.0”?> // XML Definition (version) ✤ <product barcode=”2323423”> // Extending Tagging System (user defined markup adds meaning) ✤ <maker>Smithson</maker> //Tags are created and defined by the developer (XSD) XML Schema ✤ <name>Apex DVD Player 1.2</name> //Tags contain strictly strings ✤ <quantity>3</quantity> //requires: start tag <quantity> and end tag </quantity> ✤ <size>33cm</size> // string value is found between the tags. ✤ <color hexvalue=”0x000000”>black</color> //tags can have attributes ✤ </product> Nodes in XML ✤ The Node: ✤ Contains all relevant data about an element. ✤ For example: <product>, which contains all the data for that individual product. ✤ ✤ This node has “childnodes”, such as <color> Data can be a string (but more...) CDATA in XML ✤ CDATA ✤ Data can hold more complex data, like HTML, using CDATA. ✤ Tells the node to contain “raw character data”, not markup. ✤ <![CDATA[ ✤ ]]> ✤ Good for containing html, JS, etc, as it will not be treated as plain string data. Create XML with PHP ✤ fopen() - allows you to open a document on the filesystem. // resource ✤ you can $file= fopen("presidents.xml", "w"); // note safe_mode , open_basedir ✤ ✤ read only (r) ✤ read and write(r+) ✤ write only(w) ✤ write and create(w+) ✤ write to end of file (a) ✤ write to end and create (a+) permissions are an issue! Try chmod to 777 to start and throttle back. Create XML with PHP ✤ fwrite() ✤ Write to the resource you were using. ✤ Allows you to complete the write to the file system ✤ will return the bytes written if successful. ✤ Be aware of permissions! chmod 777 to start and go lower as needed. Create XML with PHP ✤ fclose() ✤ closes the current resource. ✤ returns boolean. worth checking! XML Example Create RSS with PHP ✤ ✤ ✤ Stands for “Real Simple Syndication” great for sharing or syndicating your content to outside systems commonly only holds “some recent data” ✤ natively uses XML as a transport ✤ Has several standardized formats. ✤ We’ll use RSS 2.0, common Create RSS with PHP ✤ RSS has a defined format: ✤ A Channel which allows to add meta about what your syndicating ✤ Should Contain a title, link, and description, (optional date, language,etc) ✤ RSS should have a defined tabbed structure ✤ Related Links: often found on blogs to send a person to the correct page to get more details. Create RSS with PHP ✤ ✤ ✤ Browsers “sniff” for RSS feeds on a site You let the browser know you’re publishing RSS by embedding a meta data tag: <link rel="alternate" type="application/rss+xml" title="RSS" href="/pathto/rss.xml" /> RSS Example SimpleXML ✤ Simple XML is PHP5’s longawaited class that allows PHP developers to easily and effectively parse XML data! ✤ Simple to Use (its in the name) ✤ Lets see it in action. xPath ✤ What is xPath and why do I care? ✤ XSLT Standard by the W3C ✤ xPath is a syntax for finding information in XML documents ✤ Can find values in nodes or attributes ✤ xPath is part of SimpleXML ✤ Learn more about it! Lab & Next Week ✤ ✤ Lab ✤ implement xml\rss in app ✤ working session for final Reading: Chapter 13 See you Sunday!