xml in php xml in php
play

XML in PHP XML in PHP a.k.a. PHP : Hypertext Preprocessor Created - PowerPoint PPT Presentation

Introduction to PHP Introduction to PHP PHP (Personal Home Page Tools) XML in PHP XML in PHP a.k.a. PHP : Hypertext Preprocessor Created in 1994 by Rasmus Lerdorf originally to track users at his personal Environments Environments


  1. Introduction to PHP Introduction to PHP •PHP (Personal Home Page Tools) XML in PHP XML in PHP •a.k.a. PHP : Hypertext Preprocessor •Created in 1994 by Rasmus Lerdorf originally to track users at his personal Environments Environments Website •www.zend.com/zend/hof/rasmus.php •In 1995 PHP became 'freeware‘ Presented by Presented by •PHP mailing list was created for bug fixes, and to share ideas and code Chris Fox, Matthew Yee, Sean Feehan Feehan, , Chris Fox, Matthew Yee, Sean •In 1996 Lerdorf created PHP 2 Travis Adolf Adolf, Heather , Heather Kroening Kroening Travis •In 1997 PHP 3 was released •PHP 4 was released in 1997. It contains the Zend Engine. The Zend Zend Engine Engine General Information on PHP The General Information on PHP • • The Zend The Zend Engine is responsible for the following tasks in PHP: Engine is responsible for the following tasks in PHP: • PHP is an Open source technology PHP is an Open source technology • • High performance parsing (including syntax checking) • High performance parsing (including syntax checking) • PHP is platform independent • PHP is platform independent • • In In- -memory compilation and execution of PHP scripts memory compilation and execution of PHP scripts • implementations exist for all major UNIX, Linux implementations exist for all major UNIX, Linux • • • Implementation of all of the standard data structures of PHP Implementation of all of the standard data structures of PHP and Windows platforms and Windows platforms • Interfacing with extension modules for connectivity to Interfacing with extension modules for connectivity to • external resources and protocols • PHP is used to read and write cookies PHP is used to read and write cookies external resources and protocols • • Overloading the Object Oriented syntax, for integration with Overloading the Object Oriented syntax, for integration with • • PHP supports a large number of databases PHP supports a large number of databases • Java and .NET Java and .NET including: access, sybase sybase, , mysql mysql, oracle and many , oracle and many including: access, • • Providing all of the standard services, including memory Providing all of the standard services, including memory more more management, resource management and more for the whole management, resource management and more for the whole of PHP of PHP

  2. PHP Code PHP Code • PHP is embedded directly into XHTML • PHP is embedded directly into XHTML documents documents • PHP code is inserted between the scripting delimiters <?PHP • PHP code is inserted between the scripting delimiters <?PHP • PHP files typically have the extension . • PHP files typically have the extension .php php but a but a and ?> and can be placed anywhere inside the XHTML and ?> and can be placed anywhere inside the XHTML server can be modified to accept different server can be modified to accept different markup markup • • example: example: extensions extensions 1. 1. <html> <html> • PHP must be installed on the web server for a PHP must be installed on the web server for a • 2. 2. <head> <head> 3. 3. <title>SAMPLE PHP</title> <title>SAMPLE PHP</title> php page to function correctly page to function correctly php 4. <?PHP $statement = "GoOdByE GoOdByE cRuEl cRuEl WoRlD WoRlD" ?> " ?> 4. <?PHP $statement = " 5. </head> 5. </head> 6. 6. <body> <body> 7. 7. <p> PHP is alive <?PHP <p> PHP is alive <?PHP print("$statement print("$statement");?></p> ");?></p> 8. 8. </body> </body> 9. 9. </html> </html> PHP database connectivity PHP database connectivity • PHP variables are persistent and always start PHP variables are persistent and always start • 1. <?PHP $select = "select * from foo foo"; "; 1. <?PHP $select = "select * from with a '$' with a '$' 2. 2. // connect to // connect to MySQL MySQL database database 3. if (!($database = mysql_connect("localhost mysql_connect("localhost", "username", "password"))) ", "username", "password"))) 3. if (!($database = • PHP variables are PHP variables are multityped multityped and can be and can be 4. 4. die ("could not connect to Database"); die ("could not connect to Database"); • 5. // use the Bar database 5. // use the Bar database changed using the settype settype function function 6. 6. if (! if (!mysql_select_db("Bar mysql_select_db("Bar", $database)) ", $database)) changed using the 7. die ("could not open Bar database"); 7. die ("could not open Bar database"); 8. 8. // use the query // use the query • • PHP includes Regular expressions and Perl PHP includes Regular expressions and Perl- -like like 9. if (!($result = mysql_query($query mysql_query($query, $database))){ , $database))){ 9. if (!($result = 10. 10. print("Could not execute query!< print("Could not execute query!<br br>"); >"); string manipulation string manipulation 11. 11. die (mysql_error die ( mysql_error()); ()); 12. 12. } } 13. 13. ?> ?> 14. 14. <table> <table> 15. 15. <?PHP for ($counter = 0;$row = <?PHP for ($counter = 0;$row = mysql_fetch_row($result);$counter mysql_fetch_row($result);$counter++;){ ++;){ 16. 16. print("< print("<tr tr>"); >"); 17. 17. foreach foreach( $row as $key => $value) ( $row as $key => $value) 18. 18. print("<td>$value</td>"); print("<td>$value</td>"); 19. 19. print("</tr print("</ tr>"); >"); 20. } 20. } 21. 21. ?> ?> 22. </table> 22. </table>

  3. Dealing with XML in application Dealing with XML in application The Answer The Answer with PHP with PHP • XML uses tags similar to HTML but can be user XML uses tags similar to HTML but can be user • Achieve this by inserting another layer between Achieve this by inserting another layer between • • defined the client and server to take care of the XML defined the client and server to take care of the XML parsing parsing • Once XML file is created, it has to be modified Once XML file is created, it has to be modified • • Problem: Most parsers don Problem: Most parsers don’ ’t have a XML or t have a XML or • XSL parser built into it • PHP can take care of this PHP can take care of this XSL parser built into it • • How do we generate HTML from a XML • How do we generate HTML from a XML source? source? • • We need something else to take care of this We need something else to take care of this SAX parser Web Services SAX parser Web Services • PHP has a built in SAX (Simple API for XML) PHP has a built in SAX (Simple API for XML) As we all know, a web service is a program with a • As we all know, a web service is a program with a parser web accessible interface. These interfaces are parser web accessible interface. These interfaces are usually built with XML and HTTP for data usually built with XML and HTTP for data transfer. Popular web services include google google, , transfer. Popular web services include • What this does is traverse an XML document What this does is traverse an XML document • amazon.com and NYSE. and NYSE. amazon.com and call specific functions to deal with different and call specific functions to deal with different tags tags

  4. Web Service Protocols: Web Service Protocols: Basic steps of a web service Basic steps of a web service XML- -RPC vs. SOAP RPC vs. SOAP XML 1. Client composes a message (function call to XML- -RPC in PHP: RPC in PHP: 1. Client composes a message (function call to XML server). server). One of the earliest way of doing remote procedure One of the earliest way of doing remote procedure 2. Client sends message. calls with XML. In php php with the with the xmlrpc.inc xmlrpc.inc file, file, 2. Client sends message. calls with XML. In setting up servers and clients is almost trivial. setting up servers and clients is almost trivial. 3. Server responds to message. 3. Server responds to message. 4. 4. Client processes response. Client processes response. Server: Client Server: Client Client passes server the name of the function and an array of Client passes server the name of the function and an array of parameters, QED. parameters, QED. Creating an xml- Creating an xml -rpc rpc web service in web service in php php is very easy and very quick. is very easy and very quick. However, in certain cases where more advanced functionality is However, in certain cases where more advanced functionality is Server PHP program creates a server object and associates a needed (such as user defined datatypes datatypes or Polymorphic or Polymorphic needed (such as user defined function with it. Accessors Accessors), SOAP may be a better choice. ), SOAP may be a better choice.

Recommend


More recommend