Activate your free membership today | Log-in

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:

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

And the front end that will talk to it:

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

Related Content:

  • PHP library ships for Microsoft Ajax
    Developers using PHP for the scripting of Microsoft ASP.NET Ajax 1.0 applications are getting some help from PHP for Microsoft Ajax Library, a product...
  • JavaScript Achilles heal for Ajax?
    Web 2.0 applications can be hacked by exploiting a security hole in the JavaScript component of Ajax, according to security vendor Fortify Software...
  • Ajax Learning Guide
    Chances are, you've been doing JavaScript and XML developer work in Lotus Domino for quite some time. This old/new approach is causing quite a stir in...
  • Ajax Learning Guide
    Are you a Web developer? The time has come to rethink your entire approach to developing Web applications. Find out about the Ajax approach...
  • Ajax Learning Guide
    Are you a Web developer? The time has come to rethink your entire approach to developing Web applications. Find out about the Ajax approach...

Posted by Dion Almaer at 8:08 am
7 Comments

++++-
4.6 rating from 95 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.