SoFunction
Updated on 2025-04-09

COM function in PHP4 (windows version)


COM function in PHP4 (windows version) These days I have been writing Excel to convert it into mysql. I found an article. I searched for the phpx forum. There is no such post. I reposted it as follows:


COM function in PHP4 (windows version)


introduce

The COM function built into PHP4 is quite attractive to us to develop programs in win32 environment, but there is still not much relevant technical documentation. This article will use three examples

Don't deal with MS office 2000 Word, Excel, Adobe Distiller to illustrate how to use COM functions in PHP.

COM technology was proposed and developed by Microsoft a few years ago. The related nouns mentioned in this article include OLE, OLE Automation, ActiveX, COM, and the meanings of these words are basically the same.

Likewise, they all mean that a piece of encapsulated code (object) is used to complete some functions of a Windows application. The PHP4 COM function can connect an object instance and use its methods with

property.

If you want to use the following example source code, please refer to my configuration.

Windows 98 - MS Office 2000
Apache 1.3.9 Windows
PHP4.02 Dev (08-20-00) Running as CGI


COM tags in PHP4

Now let's start, using PHP4's COM to instantiate a component, requiring the "OLE program identifier" of the new operator and the object:


$instance = new COM("$identifier");

?>

Because COM is a reserved word for PHP4, it transmits the identity of this object to a constructor, and now we get an instance of this component, according to the nature of the OOP class, we can easily

access its methods and properties.

For example:


$instance->[Object]->[method1]->[method2]->..->[property];

?>

It's that simple!

The structure of OOP does not work under PHP (due to problems with PHP syntax, the name and value of the attribute are illegal characters, such as dots and parentheses, etc.), so PHP4 provides two corresponding functions:


bool com_set(class com_object, string property name, string property_value);

mixed com_get(class com_object, string property_name);

?>

Finally, PHP4 also supports DCOM technology, which can create an object instance on a remote computer.


$Instance = new COM(string "Component name", string "remote_server_address");

?>

Note: This is to set PHP using the DCOM directive. In the future, PHP developers will provide support for DCOM under Unix.

Identification, methods, and properties

The identifier is a string as follows:

MS Word: "" or ".9"
MS Excel: "" or ""
ADOBE Acrobat: "" or ""

For the last identities, I'm going to point out that getting the correct object identities name is not an easy task. If you can't access VBA documents, you can look for Windows registration

The table, look for it in HKEY_CLASSES_ROOT and you can get some application names. The valid object identifier on your machine is placed under the CLSID subfolder.

The application will generally provide documentation to describe its COM methods and properties. In office2000, you can run the program, open the VBA Editor, and select the Object Editor. Enter the application

A method name or attribute name in the library, then right-click in the window below to select a class or member name, click Help, and you will get a description of this class or member. you also

You can refer to MSDN. An example of Excel is as follows: [url]/library/officedev/off2000/[/url]


Operate with COM function MS Word

Now let's start with the first example:


#*********************************************************
# This example comes from the Zend site, with slight changes
# Open a word instance and create a new document Useless
# Enter a line of text "This is a test2..."
#*********************************************************

#Instantiate an object

$word = new COM("") or die("Unable to instantiate Word");

#Get and display the version

print "Loaded Word, version {$word->Version}
";

#Another way to get the version

$testversion = com_get($word->application,version);

print "Version using Com_get(): $testversion
";

#Make it visible

$word->Visible = 1;

#Create a new file

$word->Documents->Add();

#Write characters

$word->Selection->TypeText("This is a test...");

#save

$word->Documents[1]->SaveAs("Useless ");

#closure

$word->Quit();

?>

You only need to spend a few minutes reading the program and refer to Word's OLE technical documentation, and you will learn almost all the operations you need in your own program.

MS Excel is using PHP COM function

Like the Word example above, you should learn this example while referring to the help documentation for the object browser in the Visual Basic editor of Excel.


#Open the workbook and its sheet,
#This example uses a spreadsheet that comes with its own when installing Excel

$workbook = "C:Program FilesMicrosoft ";
$sheet = "Quick Tour";

#Instantiate an object of a component
$ex = new COM("") or Die ("Did not connect");

#Fetch the program name and version
print "Application name:{$ex->Application->value}
" ;
print "Loaded version: {$ex->Application->version}
";

#Open the workbook so that we can use it
$wkb = $ex->application->Workbooks->Open($workbook) or Die ("Did not open");

#Presave the original workbook and create a copy of the workbook
$ex->Application->ActiveWorkbook->SaveAs("Ourtest");
#$ex->Application->Visible = 1; #This sentence comments to make Excel visible

# Read and write a cell in a new worksheet
# We can read this cell E11 (Advertising in the 4th. Quarter)
$sheets = $wkb->Worksheets($sheet); #Select the sheet
$sheets->activate; #Activate it
$cell = $sheets->Cells(11,5) ; #Select the cell (Row Column number)
$cell->activate; #Activate the cell
print "Old Value = {$cell->value}
"; #Print the value of the cell:10000
$cell->value = 15000; #Change it to 15000
print "New value = {$cell->value}
";#Print the new value=15000

# Finally, recalculate this cell with the new value
$sheets->Calculate;
#If you want to calculate it manually, it is optional
#The total value of the effect can be seen (cell E13)
$cell = $sheets->Cells(13,5) ; #Select the cell (Row Column number)
$number = Number_format($cell->value);
print "New Total cost =$$number - was $47,732 before.
";
#According to the calculation formula, advertising affects the company's overhead, $57,809 will be displayed here

#Use built-in functions in Excel
# PMT(percent/12 months,Number of payments,Loan amount)
$pay = $ex->application->pmt(0.08/12,10,10000);
$pay = sprintf("%.2f",$pay);
print "Monthly payment for $10,000 loan @8% interest /10 months: $ $pay
";

#Should print monthly payment = $ -1,037.03

#Optional, save
$ex->Application->ActiveWorkbook->SaveAs("Ourtest");
#Close, don't ask questions
$ex->application->ActiveWorkbook->Close("False");
unset ($ex);

?>

This example makes your PHP work with Excel. Of course, there are more objects to use, and accessing an OOP encapsulation class that you wrote is as easy as accessing excel.

Access Adobe Distiller with PHP COM

This last example is not an MS program anymore. If your program has a PostScript file, you will be interested in this, rewrite (distillation) it into a PDF document. Adobe has a

A program is called Distiller, which can generate an instance. The code is as follows:


$pdf = new COM(".1");

?>

One thing to note is that the OLE identifier "pdfdistiller" given in the Distiller's documentation is invalid.

The most basic way to distille a file is:


$pdf->FileToPdf ($psfile, strOutputPDF '', strJobOptions "");

?>

This $psfile is the file name of this PostScript, and strOutputPDF is the file name of the output file PDF. StrJobOptions is the parameter file name of the Distiller, the last two parameters

It is optional, and the default is the same name. This PS file name is combined with the PDF file name, and uses this default Joboptions file. For example:


$pdf->FileToPdf ($psfile, "", "");
#Here $psfile can be a file that will return.

?>

There are more methods and properties that can be used in Distiller. If you are interested, please refer to Adobe's technical documentation.


Abort/Possible Issues

If something error occurs in your code, you may have created an instance but not closed it normally. Worst of all, this application may be maintained by this instance,

If there are multiple copies of this program in your program list, even if you correct the error, it will interfere with your results. The solution is: fix a bug and remove them in time

Before you start running again, use and end the task. For the same reason, at the end of your code, you should also close the program and delete the instance in time.

You have some tricks when handling exceptions of com_get and com_set. For example:
$Version = Com_get($instance->Application,"Version");

Will work in Word but will produce an error in Excel.

Some objects cannot be instantiated in PHP4, because this program requires a custom interface, but PHP4 does not support it.


Why do we use it?

I hope these three examples can give you some clues to thinking. PHP's COM allows you to access Windows 4 programs in PHP's scripts. This code is simpler than ASP and can integrate other

PHP has powerful support for databases. Microsoft sells this COM technology vigorously in all aspects, under different names and structures, such as COM+ (Combine COM with

Microsoft Transaction Server MTS), ADO, OLE DB, OWC, Windows DNA, and more. The combination of PHP and Apache provides an open source solution.