Wednesday, January 24th, 2007

PHP for Microsoft Ajax Library

Category: .NET, PHP

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:

  1. < ?php
  2.  
  3. require_once '../../dist/MSAjaxService.php';
  4.  
  5. class HelloService extends MSAjaxService
  6. {
  7.    function SayHello($name)
  8.    {
  9.        return "Hello, " . $name . "!";
  10.    }
  11. }
  12.  
  13. $h = new HelloService();
  14. $h->ProcessRequest();

And the front end that will talk to it:

  1. <html>
  2. <head>
  3. <title>Hello, World!</title>
  4. <script type="text/javascript" src="../../MicrosoftAjaxLibrary/MicrosoftAjax.js"></script>
  5. <script type="text/javascript" src="HelloService.php/js"></script>
  6. </head>
  7. <body>
  8. Name: <input id="name" type="text" />
  9. <input type="button" value="Say Hello" onclick="button_click(); return false;" />
  10. <br />
  11. Response from server: <span id="response"></span>
  12. </body>
  13. <script type="text/javascript">
  14.     function button_click() {
  15.         HelloService.SayHello($get('name').value, function (result) {
  16.             $get('response').innerHTML = result;
  17.         });
  18.     }
  19. </script>
  20. </html>

Posted by Dion Almaer at 8:08 am
7 Comments

++++-
4.5 rating from 101 votes

7 Comments »

Comments feed TrackBack URI

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.

Comment by Billy — January 24, 2007

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.

Comment by Steve Marx — January 24, 2007

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.

Comment by Steve Marx — January 24, 2007

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.

Comment by Billy — January 25, 2007

Nice work Steve,
Will you be porting this XAML/Vista as well

Comment by Venkat — January 29, 2007

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.

Comment by Alexander Rysenko — February 5, 2007

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.

Comment by Steve Marx — February 9, 2007

Leave a comment

You must be logged in to post a comment.