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

xml in php xml in php
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

XML in PHP XML in PHP Environments Environments

Presented by Presented by

Chris Fox, Matthew Yee, Sean Chris Fox, Matthew Yee, Sean Feehan Feehan, , Travis Travis Adolf Adolf, Heather , Heather Kroening Kroening

Introduction to PHP Introduction to PHP

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

personal Website

  • www.zend.com/zend/hof/rasmus.php
  • In 1995 PHP became 'freeware‘
  • PHP mailing list was created for bug fixes, and to share ideas and code
  • In 1996 Lerdorf created PHP 2
  • In 1997 PHP 3 was released
  • PHP 4 was released in 1997. It contains the Zend Engine.

The The Zend Zend Engine Engine

  • The

The Zend Zend Engine is responsible for the following tasks in PHP: Engine is responsible for the following tasks in PHP:

  • High performance parsing (including syntax checking)

High performance parsing (including syntax checking)

  • In

In-

  • memory compilation and execution of PHP scripts

memory compilation and execution of PHP scripts

  • Implementation of all of the standard data structures of PHP

Implementation of all of the standard data structures of PHP

  • Interfacing with extension modules for connectivity to

Interfacing with extension modules for connectivity to external resources and protocols external resources and protocols

  • Overloading the Object Oriented syntax, for integration with

Overloading the Object Oriented syntax, for integration with Java and .NET Java and .NET

  • Providing all of the standard services, including memory

Providing all of the standard services, including memory management, resource management and more for the whole management, resource management and more for the whole

  • f PHP
  • f PHP

General Information on PHP General Information on PHP

  • PHP is an Open source technology

PHP is an Open source technology

  • PHP is platform independent

PHP is platform independent

  • implementations exist for all major UNIX, Linux

implementations exist for all major UNIX, Linux and Windows platforms and Windows platforms

  • PHP is used to read and write cookies

PHP is used to read and write cookies

  • PHP supports a large number of databases

PHP supports a large number of databases including: access, including: access, sybase sybase, , mysql mysql, oracle and many , oracle and many more more

slide-2
SLIDE 2
  • PHP is embedded directly into XHTML

PHP is embedded directly into XHTML documents documents

  • PHP files typically have the extension .

PHP files typically have the extension .php php but a but a server can be modified to accept different server can be modified to accept different extensions extensions

  • PHP must be installed on the web server for a

PHP must be installed on the web server for a php php page to function correctly page to function correctly

PHP Code PHP Code

  • PHP code is inserted between the scripting delimiters <?PHP

PHP code is inserted between the scripting delimiters <?PHP and ?> and can be placed anywhere inside the XHTML and ?> and can be placed anywhere inside the XHTML markup markup

  • example:

example: 1. 1. <html> <html> 2. 2. <head> <head> 3. 3. <title>SAMPLE PHP</title> <title>SAMPLE PHP</title> 4. 4. <?PHP $statement = " <?PHP $statement = "GoOdByE GoOdByE cRuEl cRuEl WoRlD WoRlD" ?> " ?> 5. 5. </head> </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 variables are persistent and always start

PHP variables are persistent and always start with a '$' with a '$'

  • PHP variables are

PHP variables are multityped multityped and can be and can be changed using the changed using the settype settype function function

  • PHP includes Regular expressions and Perl

PHP includes Regular expressions and Perl-

  • like

like string manipulation string manipulation

PHP database connectivity PHP database connectivity

1. 1. <?PHP $select = "select * from <?PHP $select = "select * from foo foo"; "; 2. 2. // connect to // connect to MySQL MySQL database database 3. 3. if (!($database = if (!($database = mysql_connect("localhost mysql_connect("localhost", "username", "password"))) ", "username", "password"))) 4. 4. die ("could not connect to Database"); die ("could not connect to Database"); 5. 5. // use the Bar database // use the Bar database 6. 6. if (! if (!mysql_select_db("Bar mysql_select_db("Bar", $database)) ", $database)) 7. 7. die ("could not open Bar database"); die ("could not open Bar database"); 8. 8. // use the query // use the query 9. 9. if (!($result = if (!($result = mysql_query($query mysql_query($query, $database))){ , $database))){ 10. 10. print("Could print("Could not execute query!< not execute query!<br br>"); >"); 11. 11. die ( die (mysql_error 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("</ print("</tr tr>"); >"); 20. 20. } } 21. 21. ?> ?> 22. 22. </table> </table>

slide-3
SLIDE 3

Dealing with XML in application Dealing with XML in application with PHP with PHP

  • XML uses tags similar to HTML but can be user

XML uses tags similar to HTML but can be user defined defined

  • 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 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

The Answer The Answer

  • Achieve this by inserting another layer between

Achieve this by inserting another layer between the client and server to take care of the XML the client and server to take care of the XML parsing parsing

  • PHP can take care of this

PHP can take care of this

SAX parser SAX parser

  • PHP has a built in SAX (Simple API for XML)

PHP has a built in SAX (Simple API for XML) parser parser

  • What this does is traverse an XML document

What this does is traverse an XML document and call specific functions to deal with different and call specific functions to deal with different tags tags

Web Services Web Services

As we all know, a web service is a program with a As we all know, a web service is a program with a web accessible interface. These interfaces are 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
  • transfer. Popular web services include google

google, , amazon.com amazon.com and NYSE. and NYSE.

slide-4
SLIDE 4

Basic steps of a web service Basic steps of a web service

1. 1. Client composes a message (function call to Client composes a message (function call to server). server). 2. 2. Client sends message. Client sends message. 3. 3. Server responds to message. Server responds to message. 4. 4. Client processes response. Client processes response.

Web Service Protocols: Web Service Protocols: XML XML-

  • RPC vs. SOAP

RPC vs. SOAP

XML XML-

  • RPC in PHP:

RPC in PHP: One of the earliest way of doing remote procedure One of the earliest way of doing remote procedure calls with XML. In calls with XML. In php php with the with the xmlrpc.inc xmlrpc.inc file, file, setting up servers and clients is almost trivial. setting up servers and clients is almost trivial.

Server: Server:

Server PHP program creates a server object and associates a function with it.

Client 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 needed (such as user defined needed (such as user defined datatypes datatypes or Polymorphic

  • r Polymorphic

Accessors Accessors), SOAP may be a better choice. ), SOAP may be a better choice.

slide-5
SLIDE 5

Soap in PHP Soap in PHP

There are many soap packages for PHP which allow the There are many soap packages for PHP which allow the programmer to not directly deal with the XML messages, which programmer to not directly deal with the XML messages, which is good because SOAP documents are hard to read, and also is good because SOAP documents are hard to read, and also eliminate some of the overhead of using SOAP (when compared eliminate some of the overhead of using SOAP (when compared to XML to XML-

  • RPC).

RPC). Some SOAP PHP implementations are more active than others. Some SOAP PHP implementations are more active than others. SOAP is constantly under revision, so it SOAP is constantly under revision, so it’ ’s probably a good idea s probably a good idea to use a package that is under active development. to use a package that is under active development. PEAR::SOAP PEAR::SOAP – – from the developers of PHP from the developers of PHP PHP PHP-

  • SOAP

SOAP – – does not appear to be in active development. does not appear to be in active development. NuSOAP NuSOAP – – Most active of the 3, used by Google and Most active of the 3, used by Google and Amazon.com Amazon.com for their web services. for their web services.

Server Server

Compared with XML Compared with XML-

  • RPC, the server requires a little more

RPC, the server requires a little more

  • verhead in creating the WSDL for the web service.
  • verhead in creating the WSDL for the web service.

Client Client

But by creating your client with a WSDL, in association But by creating your client with a WSDL, in association with a proxy, web service functions can be called with a proxy, web service functions can be called

  • directly. However,
  • directly. However, WSDLs

WSDLs are not explicitly required are not explicitly required in order to call a SOAP service in in order to call a SOAP service in php php. .

Web Service Summary Web Service Summary

  • XML

XML-

  • RPC is simple and often

RPC is simple and often “ “good enough good enough” ”. .

  • SOAP can do more, and has better

SOAP can do more, and has better “ “buzzword buzzword” ” compliance compliance

slide-6
SLIDE 6

Why Should I Use (or Not Use) PHP and Why Should I Use (or Not Use) PHP and XML? XML?

  • PHP is the new language for web programming

PHP is the new language for web programming. .

  • XML is the new way to store and transfer data.

XML is the new way to store and transfer data.

  • but are they suited for your project, either

but are they suited for your project, either together or separately? together or separately?

Advantages Advantages

  • PHP supports XML parsing (DOM and SAX) as well

PHP supports XML parsing (DOM and SAX) as well as a new feature called as a new feature called SimpleXML SimpleXML that converts XML that converts XML to an object that can be processed with normal to an object that can be processed with normal property selectors and array property selectors and array iterators iterators. .

  • Although you don

Although you don’ ’t see the XML, PHP supports both t see the XML, PHP supports both SOAP and XML SOAP and XML-

  • RPC, and implementing it is quite

RPC, and implementing it is quite simple. simple.

  • Great for displaying data from simple XML files.

Great for displaying data from simple XML files.

  • All the other kids are doing it! Web services from

All the other kids are doing it! Web services from Amazon and Google all use PHP and SOAP. Amazon and Google all use PHP and SOAP.

Disadvantages Disadvantages

  • While PHP has made steps forward with

While PHP has made steps forward with SimpleXML SimpleXML, , it doesn it doesn’ ’t have XML object classes, like Java does. t have XML object classes, like Java does.

  • PHP reads XML in as text, and it has to output it as

PHP reads XML in as text, and it has to output it as text, which can cause some major overhead. text, which can cause some major overhead.

  • XML is sometimes used in places it should not. For

XML is sometimes used in places it should not. For example, to replace a database. In this case the example, to replace a database. In this case the

  • verhead that XML in PHP causes will be astronomical
  • verhead that XML in PHP causes will be astronomical

compared to the relatively simple database function it compared to the relatively simple database function it would need to call would need to call

So Should I Use (or Not Use) PHP and So Should I Use (or Not Use) PHP and XML? XML?

Now you know a little bit about why (or why not) Now you know a little bit about why (or why not) to use XML and PHP. But the only way to to use XML and PHP. But the only way to answer the above question is to play around answer the above question is to play around with the technologies. with the technologies.

slide-7
SLIDE 7

Links Links

PHP: PHP:

  • www.php.net

www.php.net

  • www.phpbuilder.com

www.phpbuilder.com XML: XML:

  • www.xml.com

www.xml.com

  • http://www.w3.org/XML/

http://www.w3.org/XML/ PHP and SOAP ( PHP and SOAP (Nusoap Nusoap): ):

  • http://dietrich.ganx4.com/nusoap/index.php

http://dietrich.ganx4.com/nusoap/index.php

  • http://

http://pear.php.net pear.php.net/ /