How to run COM objects on a remote server in PHP - php

How can I run a COM object that is located in a dll file on a remote server?
According to php.net (http://php.net/manual/en/faq.com.php#faq.com.q8):
How can I run COM object from remote server ? Exactly like you run
local objects. You only have to pass the IP of the remote machine as
second parameter to the COM constructor.
Make sure that you have set com.allow_dcom=TRUE in your php.ini.
I have com.allow_dcom enabled in my php.ini and according to phpinfo(); I do in fact have COM support, DCOM support, and .NET support enabled. I am having a hard time finding examples of how to call the remote objects.
The DLL file (pcmsrv32.dll) is located in C:\Windows on a remote server.
I need to access the object method CalcDistance() which is stored in that file.
I have tried to pass the file location and the IP to the COM class:
$obj = new COM("C:\Windows\PCMSRV32.DLL","10.86.0.21");
But that does not work. I get this error:
Fatal error: Uncaught com_exception: Failed to create COM object `C:\Windows\PCMSRV32.DLL': Moniker cannot open file in C:\Users\...\index.php:33
I have also tried using the ProgID given in the User Guide for PC*Miler|Connect and used that in my code:
$com = new COM("PCMServer.PCMServer.1","10.86.0.21");
However that gives me this error:
com_exception: Failed to create COM object `PCMServer.PCMServer.1': Invalid syntax
What am I doing wrong?

I ended up getting in touch with PC*Miler Support, and they gave me a sample code for making a basic call that can be run as a CLI script:
<?php
// Create COM Object
$pcms = new COM("PCMServer.PCMServer");
// Calculate
$dist = $pcms->CalcDistance("12345","23456");
// Returned distance is a INT that needs to be divided by 10 (or the number of decimal places specified in the PCMSServe.ini file located in C:\Windows
$properDist = ($dist/10);
echo $properDist
?>
EDIT
I am now being told that PC*Miler only offers their COM objects locally. So unless I have it installed on the same server that I am developing on, this will not work. We are installing version 30 of PC*Miler to the same server where my PHP code resides and trying that instead.
UPDATE
We had v.30 of PC*Miler installed on the same server, as well as the patch that fixes a php bug, and I was able to successfully run the following code:
try {
$pcms = new COM("PCMServer.PCMServer");
}
catch (com_exception $e) {
print $e . "\n";
}
// Calculate
$dist = $pcms->CalcDistance("Goodyear, AZ","Las Vegas, NV");
$properDist = ($dist/10);
echo $properDist;
Output of $properDist was 288 miles - which I verified was correct with Google Maps.

Related

Failed to create COM object `CrystalReports.ObjectFactory.2': Class not registered

I inherited this PHP web application that generates a PDF file from crystal reports. Now I have tried this project on a different server it seems to work properly, but when I try to view it on my development server it returns an exception:
Caught exception:
Failed to create COM object `CrystalReports.ObjectFactory.2': Class not registered
Tracing the code and it seems that it is thrown by this:
$objectFactory= new COM("CrystalReports.ObjectFactory.2") or die("Unable to Create COM");
$crApp = $ObjectFactory->CreateObject("CrystalRuntime.Application.9") or die("Unable to Create Object");
I have tried installing/re-installing CR Basic Runtime on my server, but it doesn't seem to work (I'm using a 64-bit computer).
I'm using:
PHP 5.3.10,
Crystal Reports 9,
Apache 2.4
I usually receive error "Caught exception: Failed to create COM object "Name_of_the_COM_server": Class not registered" when COM server is not registered on the server, where WEB server is running.
You can register COM server with Regsvr command.
I suppose, that Crystal Report can be registered during installation, so you can check Crystal Report documentation.

Problems using SAPI with PHP through COM, on IIS

I am attempting to get Text To Speech and Speech Recognition to work in PHP using Microsoft's SAPI through COM objects.
In the past, I already used this code to get TTS to work (Apache 2.1, PHP 5.5, on Windows 2003 Server)
// Instantiate the object
$VoiceObj = new COM("SAPI.SpVoice") or die("Unable to instantiate SAPI");
// Get the available voices
$VoicesToken=$VoiceObj->GetVoices();
$NumberofVoices=$VoicesToken->Count;
for($i=0;$i<$NumberofVoices;$i++)
{
$VoiceToken=$VoicesToken->Item($i);
$VoiceName[$i]=$VoiceToken->GetDescription();
}
// Get and print the id of the specified voice
$SelectedVoiceToken=$VoicesToken->Item(0);
$SelectedVoiceTokenid=$SelectedVoiceToken->id;
// Set the Voice
$VoiceObj->Voice=$SelectedVoiceToken;
$VoiceName=$VoiceObj->Voice->GetDescription();
$VoiceFile = new COM("SAPI.SpFileStream");
$VoiceFile->Open('./test.wav', 3, false);
// Speak to file
$VoiceObj->AudioOutputStream = $VoiceFile;
$VoiceObj->Speak("What an unbelievable test", 0);
$VoiceFile->Close();
On my new setup (IIS 7.5, PHP 7.0, Windows Server 2008R2) the same code fails at
$VoiceObj->Speak("What an unbelievable test", 0);
Fatal error: Uncaught com_exception: <b>Source:</b> Unknown<br/><b>Description:</b> Unknown in \\web\tts.php:30 Stack trace: #0 \\web\tts.php(30): com->Speak('this is a marve...', 0) #1 {main}
With such little detail (where to retrieve more?) I can't figure out what the problem may be.
Writing permissions checked.
PHP 7.0 replaced with 5.5, still not working.
Same code tested with a Win32 app, and it works flawlessly.
Any hints?
Two years later, I casually happen to find an answer to this problem.
PHP COM Objects can't perform file operations on network paths, even with all the required permissions
Once the development folders were moved on the same machine where IIS runs, a bunch of previously broken tests started working. They all had one thing in common: they were using COM interfaces to save files or something like that.

Call method from out-of-process COM component using PHP on Azure

I've written a simple PHP script which instantiates a COM object for an out-of-process (i.e. exe file) COM component and uses it to call a COM method that the component exposes. This COM method very simply quadruples the number passed as the first argument, returning the result in the second argument (passed by reference). The script shown below works successfully on my local development machine on WampServer 2.0 (Apache 2.2.11 / PHP 5.3.1). The COM component is a Win32 executable built using Delphi.
<?php
// ensure no time limit is imposed
set_time_limit(0);
// show all errors, warnings and notices whilst developing
error_reporting(E_ALL);
$numIn = 3;
$numOut = new VARIANT(1, VT_I4);
echo '----- BEFORE ---------' . '<br>';
echo 'NumIn: ' . $numIn . '<br>';
echo 'NumOut: ' . $numOut . '<br>';
echo '----------------------' . '<br>';
$oleapp = new COM("OleAutomationFeasibilityModel.Automation") or die ("Could not initialise feasibility model object.");
echo '<br />COM object created version = ' . $oleapp->Version . '<br /><br />';
$oleapp->CalculateWithVariants($numIn, $numOut);
unset($oleapp);
echo '----- AFTER ---------' . '<br>';
echo 'NumIn: ' . $numIn . '<br>';
echo 'NumOut: ' . $numOut . '<br>';
echo '----------------------' . '<br>';
?>
Note: as I understand it, one can only pass a parameter by reference to a COM method using a VARIANT type, as common data types like integers and strings won't work (see http://www.php.net/manual/en/ref.com.php#45038).
I then created and deployed an Azure Web Role (Cloud Service) with a startup script that registers the COM component successfully i.e. the appropriate registry keys appeared in the registry. To further confirm that the COM component could be interacted with, I used RDP to connect to the cloud service instance and installed Microsoft Access Runtime 2010 as I have an Access application that provides a GUI to test the methods of the COM component. I was able to run this application and successfully interacted with the COM component, using it to pass an integer to the CalculateWithVariants method and the expected quadrupled result was returned. So, I've established that the COM component is installed and can be interacted with on the Azure cloud service instance.
Next I included the above PHP script in the Web Role and deployed it on Azure. Unfortunately, calling the script from a browser results in an HTTP Error 500 (Internal Server Error) and I'm struggling to find out why. If I comment out all lines referencing $oleapp, I still get the same error. If I additionally comment out the line that instantiates a variant object, no error occurs. If I reinstate the line which instantiates the COM object and the line below it, I receive no error message but the only text echoed is from the lines preceding the COM object creation line i.e. the call to the Version method fails. So it appears to be struggling with the variant object creation and the COM object creation.
I'm a bit stuck in terms of how to resolve this issue. I would therefore be very grateful if anyone has any pointers as to a way forward.
UPDATE 1
I decided to try a different course of action on the Azure platform by...
creating an Azure Virtual Machine with a Windows Server 2008 R2 OS
installing WampServer 2.2E (Apache 2.2.22 / PHP 5.3.13 / MySQL
5.5.24) in the VM as a quick and easy way to test whether this approach would work
copying the above PHP script into the WampServer "www directory"
launching WampServer
selecting the "Put Online" option from the WampServer Menu (accessed by left-clicking the WampServer icon in the Windows Taskbar Notification area)
creating an "Inbound Rule" for the VM firewall to allow connections to port 80
...and thankfully the script ran successfully!
Ideally, I would still like to get this working as an Azure cloud service as it shouldn't be necessary for me to maintain the PHP installation in a full VM.
UPDATE 2
I tried restarting the cloud service, then remotely connecting to an instance of the cloud service and looking in the Application Event Viewer. I saw that WMI logged 1 error during startup:
Event filter with query "SELECT * FROM __InstanceModificationEvent WITHIN 60
WHERE TargetInstance ISA "Win32_Processor" AND TargetInstance.LoadPercentage > 99"
could not be reactivated in namespace "//./root/CIMV2" because of error 0x80041003
Events cannot be delivered through this filter until the problem is corrected.
I then ran the above script a couple of times and rechecked the Application Event Viewer but nothing had been logged.
I also checked the IIS logs and the Azure log, startup-tasks-log and startup-tasks-error-log files to no avail.
After giving up on solving this last year. I made another concerted effort to resolve it this week and succeeded!
I basically needed to (a) enable the php_com_dotnet.dll to allow use of COM and VARIANT classes, and (b) grant default Local Activation permission to IIS_IUSRS to allow access to the COM component. I've listed the detailed steps I took below...
Add a folder called php in the web role's bin folder
As of PHP 5.3.15 / 5.4.5, in order to use the COM and VARIANT
classes, the php_com_dotnet.dll needs to be enabled inside of
php.ini. Previous versions of PHP enabled these extensions by
default (source: http://www.php.net/manual/en/com.installation.php). In
the php folder, create a php.ini file containing only the following
lines...
[COM_DOT_NET]
extension=php_com_dotnet.dll
Create a SetDCOMPermission.reg file in the bin folder which contains the following content, to grant default Local Activation permission to IIS_IUSRS...
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\OLE]
"DefaultLaunchPermission"=hex(3):01,00,04,80,74,00,00,00,84,00,00,00,00,00,\
00,00,14,00,00,00,02,00,60,00,04,00,00,00,00,00,14,00,1F,00,00,00,01,01,00,\
00,00,00,00,05,12,00,00,00,00,00,18,00,1F,00,00,00,01,02,00,00,00,00,00,05,\
20,00,00,00,20,02,00,00,00,00,18,00,0B,00,00,00,01,02,00,00,00,00,00,05,20,\
00,00,00,38,02,00,00,00,00,14,00,1F,00,00,00,01,01,00,00,00,00,00,05,04,00,\
00,00,01,02,00,00,00,00,00,05,20,00,00,00,20,02,00,00,01,02,00,00,00,00,00,\
05,20,00,00,00,20,02,00,00
I don't know if the above registry change will work for everyone, so the process I used is documented here (it essentially involved using a program called RegFromApp to record the changes made to the registry when granting default Local Activation permissions for IIS_IUSRS in COM Security and to save the registry changes as a .reg file into the web role's bin folder).
Copy and paste the out-of-process COM component (OleAutomationFeasibilityModel.exe file) into the bin folder
Create a RegisterOleAutomationFeasibilityModel.cmd file in the bin folder to register the COM component and set the necessary permissions to launch it...
chcp 1252>NUL
OleAutomationFeasibilityModel.exe /regserver
regedit.exe /s SetDCOMPermission.reg
exit /b 0
In the ServiceDefinition.csdef file, insert a reference to the .cmd file immediately before the closing Startup tag...
<Task commandLine="RegisterOleAutomationFeasibilityModel.cmd" executionContext="elevated" />
Publish the web role
Hope that helps someone in a similar situation!

PHP SoapClient using WCF service hosted in Windows application on Windows 2008 Server

This question relates to the requirement to utilize a WCF service hosted in a Windows application on Windows 2008 Server, from PHP scripts which are hosted in IIS 7 on the same server, and from other applications hosted elsewhere.
The application has been developed and tested thoroughly on a Windows 7 machine using Visual Studio 2010, .NET Framework 4, IIS 7, and various versions of PHP.
On the Windows 7 machine, netsh was used as follows.
netsh http add urlacl url=http://localhost:8000/WCFService/ user=machinename\user
Various other ports were also successfully added and used during testing.
When the application was copied to the Windows 2008 Server, the essential parts of the application were successfully tested.
The same netsh command was used on Windows 2008 Server, yet localhost was replaced with the server IP and machinename with domain as follows:
netsh http add urlacl url=http://serveripaddress:8000/WCFService/ user=domain\user
During attempts to get the PHP SoapClient working, various user="..." options were attempted, including "everyone".
The initial PHP SoapClient script is as follows:
try {
$client = new SoapClient("http://serveripaddress:8000/WCFService/?wsdl");
} catch (Exception $e) {
echo $e->getMessage(), "\n";
exit();
}
During initial testing, the following errors were encountered:
Warning:
SoapClient::SoapClient(http://serveripaddress:8000/WCFService/?wsdl)
[soapclient.soapclient]: failed to open stream: HTTP request failed!
in C:\inetpub\wwwroot\Sites\www.myurl.com\WCF-Test.php on line 7
Warning: SoapClient::SoapClient() [soapclient.soapclient]: I/O
warning : failed to load external entity
"http://serveripaddress:8000/WCFService/?wsdl" in
C:\inetpub\wwwroot\Sites\www.myurl.com\WCF-Test.php on line 7
SOAP-ERROR: Parsing WSDL: Couldn't load from
'http://serveripaddress:8000/WCFService/?wsdl' : failed to load
external entity "http://serveripaddress:8000/WCFService/?wsdl"
After some fiddling around the error reduced to the following:
SOAP-ERROR: Parsing WSDL: Couldn't load from
'http://serveripaddress:8000/WCFService/?wsdl' : failed to load
external entity "http://serveripaddress:8000/WCFService/?wsdl"
I then extracted the wsdl into a file and used the following PHP script:
try {
$client = new SoapClient("wsdl\wcf-wsdl.wsdl");
} catch (Exception $e) {
echo $e->getMessage(), "\n";
exit();
}
The error then changed slightly to the following:
SOAP-ERROR: Parsing Schema: can't import schema from
'http://serveripaddress:8000/WCFService/?xsd=xsd0'
I am of the opinion that the issue at hand relates to the "visibility" of the service, to the client, and possibly permissions.
After reading dozens of posts, I still have not been able to find a solution to this issue.
Any assistance will be greatly appreciated.
Many thanks in advance.
If the WCF Service is something you are hosting for it to be accessed by php clients you need to have a flat wsdl. On how to generate Flat Wsdl follow the below link:
How to flatten your wsdl
The same feature would be avaiable in the framework as part of .NET 4.5. Hope that helps you out.
If you know you are working with a .NET WCF service you can just change the location you download the wsdl from .svc?wsdl to .svc?singleWsdl and the WCF server will take care of the recursion / linking work for you.
This may the answer,.net has the default config make this error.
https://bugs.php.net/bug.php?id=47761

Setting up the WatiN COM Interface from WatiN Test Record

Using PHP code
$iface=new COM("WatiN.COMInterface");
$ie = $iface->CreateIE("http://www.google.com");
$ie->TextField($iface->FindByName("q"))->TypeText("watin");
$ie->Button($iface->FindByName("btnG"))->Click();
From http://watintestrecord.sourceforge.net/WatiNCOM.html
Result in com_exception: Failed to create COM object 'WatiN.COMInterface': Invalid syntax …
Other COM like
$word = new COM("word.application") or die("Unable to instantiate Word");
is working fine .
Could someone give a step by step manual of how to make WatiN work with PHP 5.3
Sounds like you haven't registered the WatiNCOM assembly for COM interop.
Try running the following from a command line in the directory containing the downloaded files.
regasm WatiNCOM.dll
this command is documented here: regasm

Categories