integration between php and .net applications

17
Integration Integration between PHP between PHP and .NET and .NET Applications Applications Svetlin Nakov Svetlin Nakov National Academy for National Academy for Software Development Software Development academy.devbg.org

Upload: geoff

Post on 06-Jan-2016

16 views

Category:

Documents


1 download

DESCRIPTION

Integration between PHP and .NET Applications. Svetlin Nakov. National Academy for Software Development. academy.devbg.org. Contents. PHP Interoperability PHP and COM Writing WordPress Plug-in in C# Phalanger: Running PHP in .NET CLR. PHP Interoperability. PHP Interoperability. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Integration between PHP and .NET Applications

Integration between Integration between PHP and .NET PHP and .NET ApplicationsApplications

Svetlin NakovSvetlin NakovNational Academy for Software National Academy for Software DevelopmentDevelopment

academy.devbg.org

Page 2: Integration between PHP and .NET Applications

ContentsContentsContentsContents

1.1. PHP InteroperabilityPHP Interoperability

2.2. PHP and COMPHP and COM

3.3. Writing WordPress Plug-in in C#Writing WordPress Plug-in in C#

4.4. Phalanger: Running PHP in .NET CLRPhalanger: Running PHP in .NET CLR

1.1. PHP InteroperabilityPHP Interoperability

2.2. PHP and COMPHP and COM

3.3. Writing WordPress Plug-in in C#Writing WordPress Plug-in in C#

4.4. Phalanger: Running PHP in .NET CLRPhalanger: Running PHP in .NET CLR

Page 3: Integration between PHP and .NET Applications

PHP InteroperabilityPHP Interoperability

Page 4: Integration between PHP and .NET Applications

PHP InteroperabilityPHP InteroperabilityPHP InteroperabilityPHP Interoperability

• What is Interoperability?What is Interoperability?

• Ability to work together with other products, Ability to work together with other products, frameworks, languages and platformsframeworks, languages and platforms

• PHP InteroperabilityPHP Interoperability

• Call external process – Call external process – shell_exec()shell_exec() function function

• Invoke native C/C++ code (Invoke native C/C++ code (.so.so / / .dll.dll libraries) libraries)

• dl()dl() function in PHP (load extension library) function in PHP (load extension library)

• SWIG – SWIG – http://www.swig.org/http://www.swig.org/

• COM operability (in Windows)COM operability (in Windows)

• Web services (REST / SOAP)Web services (REST / SOAP)

• What is Interoperability?What is Interoperability?

• Ability to work together with other products, Ability to work together with other products, frameworks, languages and platformsframeworks, languages and platforms

• PHP InteroperabilityPHP Interoperability

• Call external process – Call external process – shell_exec()shell_exec() function function

• Invoke native C/C++ code (Invoke native C/C++ code (.so.so / / .dll.dll libraries) libraries)

• dl()dl() function in PHP (load extension library) function in PHP (load extension library)

• SWIG – SWIG – http://www.swig.org/http://www.swig.org/

• COM operability (in Windows)COM operability (in Windows)

• Web services (REST / SOAP)Web services (REST / SOAP)

Page 5: Integration between PHP and .NET Applications

PHP and COMPHP and COM

Page 6: Integration between PHP and .NET Applications

What is COM?What is COM?What is COM?What is COM?

• Component Object Model (COM)Component Object Model (COM)

• Microsoft Windows built-in technologyMicrosoft Windows built-in technology

• Inter-process communication between Inter-process communication between components and applicationscomponents and applications

• Example: Display Adobe Acrobat PDF in Example: Display Adobe Acrobat PDF in Internet ExplorerInternet Explorer

• Object-oriented approachObject-oriented approach

• Allows dynamic object creation and method Allows dynamic object creation and method invocation and properties / events accessinvocation and properties / events access

• Language-neutral technologyLanguage-neutral technology

• Component Object Model (COM)Component Object Model (COM)

• Microsoft Windows built-in technologyMicrosoft Windows built-in technology

• Inter-process communication between Inter-process communication between components and applicationscomponents and applications

• Example: Display Adobe Acrobat PDF in Example: Display Adobe Acrobat PDF in Internet ExplorerInternet Explorer

• Object-oriented approachObject-oriented approach

• Allows dynamic object creation and method Allows dynamic object creation and method invocation and properties / events accessinvocation and properties / events access

• Language-neutral technologyLanguage-neutral technology

Page 7: Integration between PHP and .NET Applications

Creating COM Component Creating COM Component in .NET Framework and C#in .NET Framework and C#Creating COM Component Creating COM Component in .NET Framework and C#in .NET Framework and C#

• Define the COM dispatch interface:Define the COM dispatch interface:

• Define the COM implementation class:Define the COM implementation class:

• Define the COM dispatch interface:Define the COM dispatch interface:

• Define the COM implementation class:Define the COM implementation class:

[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)][InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]public interface ICalculator {public interface ICalculator { int Sum(int a, int b)int Sum(int a, int b)}}

namespace PlayingWithCOMnamespace PlayingWithCOM{{ [ComVisible(true)][ComVisible(true)] [Guid("38AADE7B-79B8-31A1-B365-22C6AAE5232C")][Guid("38AADE7B-79B8-31A1-B365-22C6AAE5232C")] public class Calculator : ICalculator {public class Calculator : ICalculator { public int Sum(int a, int b) { return apublic int Sum(int a, int b) { return a ++ b; }b; } }}}}

Page 8: Integration between PHP and .NET Applications

Register COM Component Register COM Component Assembly in the RegistryAssembly in the RegistryRegister COM Component Register COM Component Assembly in the RegistryAssembly in the Registry

• Sign the assembly with strong name:Sign the assembly with strong name:

• Install the assembly in the Global Install the assembly in the Global Assembly Cache (GAC):Assembly Cache (GAC):

• Register the assembly as COM Register the assembly as COM component in Windows registry:component in Windows registry:

• Sign the assembly with strong name:Sign the assembly with strong name:

• Install the assembly in the Global Install the assembly in the Global Assembly Cache (GAC):Assembly Cache (GAC):

• Register the assembly as COM Register the assembly as COM component in Windows registry:component in Windows registry:

[assembly:AssemblyKeyFile(@"[assembly:AssemblyKeyFile(@"secret-keysecret-key.snk")].snk")]

gacutil -i PlayingWithCOM.dllgacutil -i PlayingWithCOM.dll

regasm PlayingWithCOM.dllregasm PlayingWithCOM.dll

Page 9: Integration between PHP and .NET Applications

Using COM Objects from Using COM Objects from PHP ScriptPHP ScriptUsing COM Objects from Using COM Objects from PHP ScriptPHP Script

• Creating COM object and invoking Creating COM object and invoking method from PHP:method from PHP:

• Creating COM object and invoking Creating COM object and invoking method from PHP:method from PHP:

$$calccalc = new = new COM("PlayingWithCOM.COM("PlayingWithCOM.CalculatorCalculator");");$result = $calc->Sum(5, 6);$result = $calc->Sum(5, 6);echo $result;echo $result;

Page 10: Integration between PHP and .NET Applications

Writing WordPress Writing WordPress Plug-ins in C#Plug-ins in C#

Page 11: Integration between PHP and .NET Applications

WordPress on IISWordPress on IISWordPress on IISWordPress on IIS

• Running WordPress in IISRunning WordPress in IIS

• Run PHP on IIS: Run PHP on IIS:

• Install FastCGI ISAPI extension for IISInstall FastCGI ISAPI extension for IIS

• Install and configure PHP for WindowsInstall and configure PHP for Windows

• Alternatively use Web Platform InstallerAlternatively use Web Platform Installer

• Install and configure MySQL for WindowsInstall and configure MySQL for Windows

• Configure WordPress and URL rewriting:Configure WordPress and URL rewriting:

• ISAPI_Rewrite in IIS 5.1/6ISAPI_Rewrite in IIS 5.1/6

• Microsoft URL Rewrite for IIS 7Microsoft URL Rewrite for IIS 7

• Running WordPress in IISRunning WordPress in IIS

• Run PHP on IIS: Run PHP on IIS:

• Install FastCGI ISAPI extension for IISInstall FastCGI ISAPI extension for IIS

• Install and configure PHP for WindowsInstall and configure PHP for Windows

• Alternatively use Web Platform InstallerAlternatively use Web Platform Installer

• Install and configure MySQL for WindowsInstall and configure MySQL for Windows

• Configure WordPress and URL rewriting:Configure WordPress and URL rewriting:

• ISAPI_Rewrite in IIS 5.1/6ISAPI_Rewrite in IIS 5.1/6

• Microsoft URL Rewrite for IIS 7Microsoft URL Rewrite for IIS 7

Page 12: Integration between PHP and .NET Applications

Creating WordPress Plug-inCreating WordPress Plug-inCreating WordPress Plug-inCreating WordPress Plug-in

• We want to create WordPress plug-in for We want to create WordPress plug-in for downloading a blog post as PDF documentdownloading a blog post as PDF document• Converting HTML page to PDF is not Converting HTML page to PDF is not

straightforward problem!straightforward problem!

• We prefer to implement this in C#We prefer to implement this in C#

• Use Internet Explorer through COMUse Internet Explorer through COM• Open the web page URL in Internet ExplorerOpen the web page URL in Internet Explorer

• Take the entire page as graphics imageTake the entire page as graphics image

• Cut the image into pages and write them to Cut the image into pages and write them to PDF document with PdfSharpPDF document with PdfSharp

• Invoke the C# class from PHP through COMInvoke the C# class from PHP through COM

• We want to create WordPress plug-in for We want to create WordPress plug-in for downloading a blog post as PDF documentdownloading a blog post as PDF document• Converting HTML page to PDF is not Converting HTML page to PDF is not

straightforward problem!straightforward problem!

• We prefer to implement this in C#We prefer to implement this in C#

• Use Internet Explorer through COMUse Internet Explorer through COM• Open the web page URL in Internet ExplorerOpen the web page URL in Internet Explorer

• Take the entire page as graphics imageTake the entire page as graphics image

• Cut the image into pages and write them to Cut the image into pages and write them to PDF document with PdfSharpPDF document with PdfSharp

• Invoke the C# class from PHP through COMInvoke the C# class from PHP through COM

Page 13: Integration between PHP and .NET Applications

Creating WordPress Plug-in Creating WordPress Plug-in (2)(2)Creating WordPress Plug-in Creating WordPress Plug-in (2)(2)

• We want to add a parameter "We want to add a parameter "?pdf?pdf" to all " to all WordPress URLs to produce PDF outputWordPress URLs to produce PDF output

• We hook the WordPress action "We hook the WordPress action "initinit""

• In the action handler invoke the C# In the action handler invoke the C# based Web2Pdf converterbased Web2Pdf converter

• We want to add a parameter "We want to add a parameter "?pdf?pdf" to all " to all WordPress URLs to produce PDF outputWordPress URLs to produce PDF output

• We hook the WordPress action "We hook the WordPress action "initinit""

• In the action handler invoke the C# In the action handler invoke the C# based Web2Pdf converterbased Web2Pdf converter

add_action('init', 'init_action_handler');add_action('init', 'init_action_handler');

$web2pdf = $web2pdf = new COM("Web2Pdf.WebPageToPDFGenerator");new COM("Web2Pdf.WebPageToPDFGenerator");$pdf_base64 = $web2pdf-$pdf_base64 = $web2pdf->GeneratePdfFromWebPageAsBase64($url);>GeneratePdfFromWebPageAsBase64($url);

Page 14: Integration between PHP and .NET Applications

Creating WordPress Plug-In Creating WordPress Plug-In in C# - Web2Pdf Converterin C# - Web2Pdf Converter

Live DemoLive Demo

Page 15: Integration between PHP and .NET Applications

PhalangerPhalangerThe PHP Language Compiler for .NETThe PHP Language Compiler for .NET

Page 16: Integration between PHP and .NET Applications

Phalanger Project: Running Phalanger Project: Running PHP in .NET FrameworkPHP in .NET FrameworkPhalanger Project: Running Phalanger Project: Running PHP in .NET FrameworkPHP in .NET Framework

• Phalanger ProjectPhalanger Project

• Open source PHP compiler for .NET CLROpen source PHP compiler for .NET CLR

• Web site: Web site: http://www.php-compiler.nethttp://www.php-compiler.net

• Compiles PHP code to MSIL just like C#Compiles PHP code to MSIL just like C#

• Implements all standard PHP functionsImplements all standard PHP functions

• Two modes of execution:Two modes of execution:

• Classical PHP execution model – run existing Classical PHP execution model – run existing applications like phpBB, Drupal, etc.applications like phpBB, Drupal, etc.

• ASP.NET mode – use ASP.NET with PHP ASP.NET mode – use ASP.NET with PHP language – mix .NET and PHP functionslanguage – mix .NET and PHP functions

• Phalanger ProjectPhalanger Project

• Open source PHP compiler for .NET CLROpen source PHP compiler for .NET CLR

• Web site: Web site: http://www.php-compiler.nethttp://www.php-compiler.net

• Compiles PHP code to MSIL just like C#Compiles PHP code to MSIL just like C#

• Implements all standard PHP functionsImplements all standard PHP functions

• Two modes of execution:Two modes of execution:

• Classical PHP execution model – run existing Classical PHP execution model – run existing applications like phpBB, Drupal, etc.applications like phpBB, Drupal, etc.

• ASP.NET mode – use ASP.NET with PHP ASP.NET mode – use ASP.NET with PHP language – mix .NET and PHP functionslanguage – mix .NET and PHP functions

Page 17: Integration between PHP and .NET Applications

Integration between PHP Integration between PHP and .NET Applicationsand .NET ApplicationsIntegration between PHP Integration between PHP and .NET Applicationsand .NET Applications

Questions?Questions?Questions?Questions?