WWW and HTTP by YaR04Z1

VIEWS: 5 PAGES: 17

									           Integration between
              PHP and .NET
               Applications

Svetlin Nakov
National Academy for
Software Development
academy.devbg.org
           Contents

1. PHP Interoperability
2. PHP and COM
3. Writing WordPress Plug-in in C#
4. Phalanger: Running PHP in .NET CLR
PHP Interoperability
              PHP Interoperability
• What is Interoperability?
  • Ability to work together with other products,
    frameworks, languages and platforms
• PHP Interoperability
  • Call external process – shell_exec() function
  • Invoke native C/C++ code (.so / .dll libraries)
     • dl() function in PHP (load extension library)
     • SWIG – http://www.swig.org/
  • COM operability (in Windows)
  • Web services (REST / SOAP)
PHP and COM
            What is COM?
• Component Object Model (COM)
  • Microsoft Windows built-in technology
  • Inter-process communication between
    components and applications
    • Example: Display Adobe Acrobat PDF in
      Internet Explorer
  • Object-oriented approach
  • Allows dynamic object creation and method
    invocation and properties / events access
  • Language-neutral technology
               Creating COM Component
               in .NET Framework and C#
• Define the COM dispatch interface:
 [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
 public interface ICalculator {
   int Sum(int a, int b)
 }

• Define the COM implementation class:
 namespace PlayingWithCOM
 {
   [ComVisible(true)]
   [Guid("38AADE7B-79B8-31A1-B365-22C6AAE5232C")]
   public class Calculator : ICalculator {
     public int Sum(int a, int b) { return a + b; }
   }
 }
            Register COM Component
            Assembly in the Registry
• Sign the assembly with strong name:
 [assembly:AssemblyKeyFile(@"secret-key.snk")]

• Install the assembly in the Global
  Assembly Cache (GAC):
 gacutil -i PlayingWithCOM.dll

• Register the assembly as COM
  component in Windows registry:
 regasm PlayingWithCOM.dll
            Using COM Objects from
            PHP Script
• Creating COM object and invoking
  method from PHP:
 $calc = new COM("PlayingWithCOM.Calculator");
 $result = $calc->Sum(5, 6);
 echo $result;
Writing WordPress
  Plug-ins in C#
            WordPress on IIS

• Running WordPress in IIS
  • Run PHP on IIS:
    • Install FastCGI ISAPI extension for IIS
    • Install and configure PHP for Windows
    • Alternatively use Web Platform Installer
  • Install and configure MySQL for Windows
  • Configure WordPress and URL rewriting:
    • ISAPI_Rewrite in IIS 5.1/6
    • Microsoft URL Rewrite for IIS 7
            Creating WordPress Plug-in

• We want to create WordPress plug-in for
  downloading a blog post as PDF document
  • Converting HTML page to PDF is not
    straightforward problem!
  • We prefer to implement this in C#
  • Use Internet Explorer through COM
    • Open the web page URL in Internet Explorer
    • Take the entire page as graphics image
    • Cut the image into pages and write them to
      PDF document with PdfSharp
  • Invoke the C# class from PHP through COM
            Creating WordPress Plug-in
            (2)
• We want to add a parameter "?pdf" to all
  WordPress URLs to produce PDF output
• We hook the WordPress action "init"
 add_action('init', 'init_action_handler');

• In the action handler invoke the C#
  based Web2Pdf converter
 $web2pdf =
   new COM("Web2Pdf.WebPageToPDFGenerator");
 $pdf_base64 = $web2pdf-
 >GeneratePdfFromWebPageAsBase64($url);
Creating WordPress Plug-In
in C# - Web2Pdf Converter
          Live Demo
         Phalanger
The PHP Language Compiler for .NET
            Phalanger Project: Running
            PHP in .NET Framework
• Phalanger Project
  • Open source PHP compiler for .NET CLR
  • Web site: http://www.php-compiler.net
  • Compiles PHP code to MSIL just like C#
  • Implements all standard PHP functions
  • Two modes of execution:
    • Classical PHP execution model – run existing
      applications like phpBB, Drupal, etc.
    • ASP.NET mode – use ASP.NET with PHP
      language – mix .NET and PHP functions
 Integration between PHP
 and .NET Applications




Questions?

								
To top