Title: Empowering your site with HTTP Requests Increasing speed without loading the whole page Byline: by Martin Fasani Deck: The XML HTTP Request was implemented by Microsoft in internet explorer 5 to provide client-side protocol support for communication with HTTP servers. Later on the developers of the Mozilla project implemented a native version on Mozilla 1.0. Thankfully they modeled their implementation with the same methods of the MS's one so aside for the initial creation of the object the rest of the code will be the same for both. Requirements: Author: please enter your requirements below. PHP: 4.x.x O/S: Linux - Windows Other software: Useful / Related Links http://www.mozilla.org/xmlextras/ http://sarissa.sourceforge.net/doc/ Author bio: Martin works for telecommunications in Spain delivering statistic reports and building extranets for clients. He is interested on social software and can be contacted through his website http://movil.be Article body: Actually there is a growing support for this HTTP request method so almost all the browsers support it or are moving in that direction. Internet Explorer 5 and above on Windows, Konqueror, Safari on Mac OS-X and Mozilla on all platforms handles HTTP requests, Opera 7.6preview 1 also now features the object. Why do we need this technology ? Besides using frames there is no way in standard html to reload a part of the page getting information from the server side. Using the XML HTTP Request method (HTTPREQ from now on) makes this fast and elegant. It's a object that runs inside the client browser allowing you to make requests without reloading the page. While the object is called the XML HTTP Request object it is not limited to being used with XML so it can request or send any type of document. Like Apple comments, it's a Cool combo, since web developers wanted a clean way to maintain connection with the server so they can make transactions in the background bringing newly updated data in the page without using frames or java applets. Live examples about this request method in use are Google Suggest (http://www.google.com/webhp?complete=1&hl=en ) and another one is the now popular Gmail. Many people wondered why it had those impressive page-loading speeds or how it keeps state of changes without refreshing the page. The magic behind that is the HTTPREQ object. When I first started writing I was wondering that about the fact that is client side object,so why I would write an article for php|arch ?. They are two the reasons that push me to keep on, the first one is that in standard environment you've normal security restrictions that avoid making this type of request outside the domain they are running, the second one is that it can be handy to write a php class to make this without needing to type client side code or at least, keeping it to the minimum expression. The catch to build interfaces using the HTTPREQ methods is that you need to combine both worlds the client scripting with the server side. Being the client the one that will take care of sending the request and after getting the response the one that will reflect the changes on the page. What can be done ? HTTPREQ is flexible, and that means that you can make different types of requests through it: ? A Simple Request gets a file on the server dynamically ? Sending a SOAP/ REST request ? A Head Request returns the HTTP headers of a page Head requests are very useful to check when a log file or any other file gets updated on your server getting the last-modified header. Basically you can do any standard HTTP request using GET or POST the only difference is that the response is going to be returned to the same page and handled by this HTTPREQ object. Putting it all this together you can do lots of little things which when added up can make certain pages and forms work faster. This makes HTTPREQ an extremely versatile system for making user friendly administration control-panels. You can grab from the server the information that you need and change that specific part of page without refreshing the hole information. Like this you make an improvement over speed, sending less amount of information between server and client, the essential information you need and not the entire fields of the form/html. Considering that being an object on the client browser is running on a "sandbox" it adopts the same domain security policies of typical JavaScript activity. That will be no problem since we will execute php on our domain to make the external requests. This opens the way to make the hierarchical forms items items as Country, Province and City get the content elements from the server, without needing to charge it all on one page or reloading the hole form just get the information for the next select box. New messages on a BBS forum or any other multi user system will get also the goodies of this silent requests. While I'm going to focus this article in doing things with the object and php it's worth to mention that I will try to keep the javascript to the minimum expression just to handle the object and bringing back the results. The easiest thing then is building a class to pass the properties and methods we are going to use so it creates all the After creating the object, the open() and send() methods are the ones you'll likely use most: HTTPREQ.open("method", "URL"[, asyncFlag]) method specifies if you use GET / POST asyncFlag (boolean) true: async mode This is the preferred method to use in a web page false: sync mode Client-Server connection is maintained (the UI will while lock until request is being made.) It 's recommended to use the ASYNC mode to prevent freezing the browser UI until you get the response. ASYNC is the preferred method to use in a webpage since it prevents locking and avoids high response times from the server. There is really no need of using SYNC mode in a website, or at least I didn't found the necessity of using it as it freezes until the result comes back to the browser making it work in a one time request basis. HTTPREQ.send(variable) Sends the HTTPREQ to the server and receives a response. Optionally if you need to force a stream to be treated and parsed as text/xml, after calling open , you can also use the setRequestHeader : setRequestHeader ( in string header, in string value ) Then onreadystatechange will specify the event handler that is called when the state of the request changes, which will let you grab the value of readyState property which represents the state of the request: readyState== 0 Object not initiated readyState== 1 Loading, object created, send method still not initiated readyState== 2 Send method called, but nothing available yet readyState== 3 Some data received, response is not fully available readyState== 4 Completed, at this point you can read the result of the request. The basic logic to use the HTTPREQ : 1.The page is loaded 2. The browser initializes the HTTPREQ object 3. The onload function is called (or any other through user interaction) 4. A request is performed 5. When the request is completed (readyState ==4), a function is called 6. Based on response you can be add content to an html container or you can use to update/change any other element of the page. To keep compatibility with the most broad range of browsers it's recommended to use the methods and properties of Mozilla which are listed at: http://unstable.elemental.com/mozilla/build/latest/mozilla/extensions/dox/interfac ensIXMLHttpRequest.html Integrating with your server side php code Basically you create your php normally receiving the GET or POST parameters. Then you request the page with the XMLREQ Method described before. After doing the work in php you return response that you interpret again in the client side. Making it more visual: Client -> XMLREQ -> Server php -> Response -> Client again interpreting response and publishing results. Now let's go back to business and let's create a handy class to generate javascript code used to make some simple requests. I hope that this start will be useful to extend the class to another uses, or just to copy and paste the generated code to customize and add additional features. The mission of this class will be to create client side code to make basic requests in 4 lines of code: require("classhttpreq.php"); $req1 = new httpreq('req1'); $req1->httpdorequest('GET','helloworld.txt'); $req1->httpcreate(); ?> We will create some public methods that build chunks of javascript to generate the request and when done will publish the response in