Wednesday, January 24th, 2007
PHP for Microsoft Ajax Library
>Steve Marx has created a PHP library for the Microsoft Ajax 1.0 release that uses the JavaScript piece in PHP land.
The Microsoft AJAX Library is a pure-JavaScript library that's used by ASP.NET AJAX but is also available as a separate download. Because it's pure JavaScript, it's not tied to ASP.NET on the backend. PHP for MS AJAX is code to help you make use of the Microsoft AJAX Library from PHP applications. With this first Alpha release, it simply supports exposing PHP classes as AJAX-enabled web services, just as in ASP.NET applications. In fact, the generated proxies are identical to what you get from ASP.NET, meaning you can have full interoperability.
Hello World
The service on the backend so to speak:
-
-
<?php
-
-
require_once '../../dist/MSAjaxService.php';
-
-
class HelloService extends MSAjaxService
-
{
-
function SayHello($name)
-
{
-
return "Hello, " . $name . "!";
-
}
-
}
-
-
$h = new HelloService();
-
$h->ProcessRequest();
-
And the front end that will talk to it:
-
-
<title>Hello, World!</title>
-
<script type="text/javascript" src="../../MicrosoftAjaxLibrary/MicrosoftAjax.js"></script>
-
<script type="text/javascript" src="HelloService.php/js"></script>
-
</head>
-
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>
-
Related Content:











Using PHP5.2 and don’t have the $_SERVER variable HTTP_CONTENT_TYPE.
This led the application to throw an exception.
Commented lines 58 to 62 in MSAjaxService.php and all is working nicely.
Thanks for reporting that! I’ve opened a bug to track this: http://www.codeplex.com/phpmsajax/WorkItem/View.aspx?WorkItemId=92.
Strangely, I’m also using PHP5.2 (but on Windows) and don’t seem to have any trouble.
Be careful with your fix if you need security on your services! Checking the content-type prevents CSRF (Cross-Site Request Forgery) attacks in the case of cookie authentication.
I’ll try to reproduce this error and find a fix that maintains security.
FYI, I believe this is now fixed. Some platforms seem to use CONTENT_TYPE instead of HTTP_CONTENT_TYPE, so now we support both.
Please download the new build and give it another try.
Nice one, Steve. That works great.
I don’t suppose you can link me to more documentation? Unfortunately the MS Code isn’t readable so I can’t see what else I can do with these scripts.
Thanks.
Nice work Steve,
Will you be porting this XAML/Vista as well
There are still some problems with that CONTENT_TYPE, I have error_reporting set to E_ALL and PHP shows a notice, that $_SERVER['HTTP_CONTENT_TYPE'] isn’t set. You should precheck it with isset.
Sorry, I haven’t been reading these comments for a while. Thanks, Alexander! I just yesterday found the same thing… I hadn’t been testing with E_ALL, and you’re absolutely right. This will be fixed in the next release.