PHP library for Microsoft AJAX
By Justin Silverton
Microsoft has released their AJAX library package for non-windows systems which contains a complete set of client JavaScript components that are included in the full ASP.NET AJAX installation. Developers over at Codeplex have developed a library that allows you to integrate your PHP applications with this package.
requirements
- PHP 5.2 — 5.2 is required for json_encode/json_decode; For earlier versions, you will need to install php-json
How to install
- Download the PHP library from codeplex here
- Download the Microsoft AJAX Library here
- After extracting both of these, place all the files from the Microsoft Ajax library in a directory called “MicrosoftAjaxLibrary”
Example
The following is a simple example that demonstrates a simple ajax transmission using the microsoft libraries.
(hello.htm)
<html>
<head>
<title>Hello, World!</title>
<script type="text/javascript" src="../../MicrosoftAjaxLibrary/MicrosoftAjax.js"></script>
<script type="text/javascript" src="HelloService.php/js"></script>
</head>
<body>
Name: <input id="name" type="text" /> <input type="button" value="Say Hello" onclick="button_click(); return false;" />
<br />
Response from server: <span id="response"></span>
</body>
<script type="text/javascript">
function button_click() {
HelloService.SayHello($get('name').value, function (result) { $get('response').innerHTML = result; });
}
</script>
</html>
(HelloService.php)
<?php
require_once '../../dist/MSAjaxService.php';
class HelloService extends MSAjaxService
{
function SayHello($name)
{
return "Hello, " . $name . "!";
}
}
$h = new HelloService();
$h->ProcessRequest();
?>
1 commentUsing Ajax across multiple domains
By Justin Silverton
XMLHttpRequest, the main component behind AJAX, does not automatically work across multiple domains. This means that you cannot make a request to an ovject on a domain that is different from the web page’s domain. There is an easy solution to this issue: apache’s mod_rewrite module.
Example
function getXMLHttpObject()
{
if (window.XMLHTTPRequest)
return new XMLHttpRequest();
else if (window.ActiveXObject)
return new ActiveXObject("Microsoft.XMLHTTP");
else
return null;
}
function handleHTTPResponse()
{
if (http.readyState == 4) {
results = http.responseText;
}
}
var http = getXMLHttpObject();
http.open("POST"."http://www.yahoo.com/service");
http.onreadystatechange = handleHttpResponse;
The above example will fail with both Firefox and Internet Explorer (unless you are running it on a web page located on the yahoo domain). There are other ways to allow cross site ajax. Within Internet Exporer, the default security settings can be changed or a host can be added to the “trusted hosts” list. Firefox, on the other hand, has a concept called signed scripts. Both of these methods will not work for most websites on the Internet. This is because it would involve every user coming to your site adding your page to their trusted host list.
Apache setup
- Install apache with both mod_rewrite and proxy enabled.
- Create the following rule: RewriteRule ^/yahoo_proxy http://www.yahoo.com/service [P]
Note: The [P] indicates a pass-through proxy.
Replace the above line: (http.open("POST"."http://www.yahoo.com/service")) with
http.open("POST"."http://your_host/yahoo_proxy") and a connection will be made to the yahoo domains through your apache server while not violating the security restrictions of IE or Firefox.
1 commentSecret Internet Explorer behaviors
By Justin Silverton
Many people don’t realize that there is a way to store up to 100K (1mb per domain) of data within Internet Explorer (supported by 5.5 and above).
These behaviors can be used to preserve information in the browser’s history, in favorites, in an XML store, or directly within a Web page saved to disk. When a user returns to a persisted page, the state of the page can be restored. By allowing information to safely reside on the client, fewer server transactions are required. Custom start pages and Web applications prosper because information can continue to exist without the paternal support of the server, or repeated server queries. A Web page can remain interactive to a degree, after the connection with the host has been severed, by persisting required information on the client.
Code Example
Below is an code example of how to use these behaviors. After typing some text into the box and clicking save, you will now be able to come back to this page and click load, and it will be loaded from the the IE internal persistence store.

<STYLE>
.storeuserData {behavior:url(#default#userData);}
</STYLE>
<SCRIPT>
function fnSaveInput(){
var oPersist=oPersistForm.oPersistInput;
oPersist.setAttribute("sPersist",oPersist.value);
oPersist.save("oXMLBranch");
}
function fnLoadInput(){
var oPersist=oPersistForm.oPersistInput;
oPersist.load("oXMLBranch");
oPersist.value=oPersist.getAttribute("sPersist");
}
</SCRIPT>
<FORM ID="oPersistForm">
<INPUT CLASS="storeuserData" TYPE="text" ID="oPersistInput">
<table>
<td><INPUT TYPE="button" VALUE="Load" onclick="fnLoadInput()"></td>
<td><INPUT TYPE="button" VALUE="Save" onclick="fnSaveInput()"></td>
</table>
</FORM>
How to use the digg API
By Justin Silverton

This article will show you how to use the digg API using PHP. I have also written a library in PHP that maps all of the endpoints documented in the API to easily accessible functions (it is also compatible with PHP 4).
What can you do with the digg API?
- Get all popular stories submitted after January 1, 2007 sorted by promotion date
- Get the most recent 100 upcoming stories in the topic “Apple”
- Get the first 20 stories that were promoted on or after December 25, 2005
- Determine whether a story, identified by its URL, has been submitted to Digg and, if so, get details like the number of Diggs and comments it has received
- Get the details of a story, identified by its URL on Digg
- Get all stories submitted today from nytimes.com
- Get all stories submitted by Kevin Rose (or another Digger)
How it works
Requests are sent to the digg servers through URLS that are described in the API doc (here).
Here is an example:
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com
This will return all stories. By default only 10 will be returned at a time (which can be increased to a maximum of 100). Also, you may notice an appkey=XXX argument. This is a unique identifier required by the digg servers. It is recommended to use the urlencoded refereral URL. Other return types are also available, which can make parsing easier depending on the language used.
response types:
- XML: Easily parsed in many languages on many platforms. It is particularly easy to use in Flash applications.
- JSON: May be directly eval’d in Javascript, and also can be parsed in many languages.
- Javascript: Useful as the source of a script tag, it passes JSON response to the Javascript callback function you specify.
- Serialized PHP: Easily unserialized in PHP to create objects, to which the programmer can attach custom methods. Or the programmer can directly access the response data through the public properties of the objects.
example:
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com (will return XML by default)
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com&type=json
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com&type=php
http://services.digg.com/stories?appkey=http%3A%2F%2Fexample.com&type=javascript
Other basic arguments
- count
Number of events to retrieve.
Event count integer
Default: 10, Maximum: 100.
offset
Offset in complete events list.
Event offset integer.
Default: 0.
PHP code
The following is a function that will allow you to connect to the digg service and parse the results. It also will automatically generate the appid based on the current location of the script.
function connect($hostname) {
//this is an ID that is unique to your app. It is auto-generated, based on the calling URL
$appid = urlencode("http://".$_SERVER["HTTP_HOST"].$_SERVER["REQUEST_URI"]);
$host = $hostname."&appkey=".$appid;
echo $host;
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_URL,$host);
curl_setopt ($ch,CURLOPT_USERAGENT,"Test library");
curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,60);
$response = curl_exec ( $ch );
curl_close($ch);
return $response;
}
Download
The digg toolkit can be downloaded Here
1 commentHow to test a web application
By Justin Silverton
When creating and releasing a web application, testing is a very important step in the overall process. It can determine the success of an application and is many times a very time consuming process. There is an open source application called selenium that can help automate these steps and help reduce the risk of missing anything important while testing.
How it works
Selenium uses JavaScript and Iframes to embed a test automation engine in your browser. This technique should work with any JavaScript-enabled browser. Because different browsers handle JavaScript somewhat differently, we usually have to tweak the engine to support a wide range of browsers on Windows, Mac OS X and Linux. The beauty of this design is that it can also be used to test any web page or script (independent of the the back-end technology used to generate it).
Features
- Easy record and playback
- Intelligent field selection will use IDs, names, or XPath as needed
- Autocomplete for all common Selenium commands
- Walk through tests
- Debug and set breakpoints
- Save tests as HTML, Ruby scripts, or any other format
- Support for Selenium user-extensions.js file
- Option to automatically assert the title of every page
How to test your application
Testing comes in three flavors. The first is a simple GUI interface (either a firefox extension or through a back-end application for Internet Explorer) that allows the direct recording of any action in the browser. Below is a screenshot of this interface.

Test scripts can be created by the following steps:
- Install the Firefox extension
- Go to the website/page that you would like to start testing
- Click the
and go through all of the steps for the test - When you are finished, click the button from step 3 again to finish the test
You will notice some commands listed on the main screen. These are the commands that were recorded from above. These can also be added manually.
Another method for testing is using the selenium core. Selenium Core uses a unique mechanism which allows it to run on so many platforms. Written in pure JavaScript/DHTML, you copy Selenium Core tests directly into your your application webserver, allowing the tests to run in any supported browser on the client-side. It allows you to do the following:
- Browser compatibility testing. Test your application to see if it works correctly on different browsers and operating systems. The same script can run on any Selenium platform.
- System functional testing. Create regression tests to verify application functionality and user acceptance.
The third method for testing is through the Selenium Remote Control. Selenium Remote Control provides a Selenium Server, which can automatically start/stop/control any supported browser.
The Selenium Server communicates directly with the browser using AJAX (XmlHttpRequest). You can send commands directly to the Server using simple HTTP GET/POST requests; that means that you can use any programming language that can make HTTP requests to automate Selenium tests on the browser. Wrapper objects are also included for Java, .NET, Perl, Python, and Ruby.
Download
Selenium can be downloaded for free Here
1 comment




