COM functions in PHP4 (Windows Version)

zhaozj2021-02-12  135

Introduction

The COM function built into PHP4 is quite attractive to our development program in the Win32 environment, but there is still no much related technical documentation. This article will handle MS Office 2000 Word, Excel, Adobe Distiller, with three examples, explain how to use COM functions in PHP.

The COM technology is proposed and developed several years ago. The relevant nouns mentioned herein have OLE, OLE Automation, ActiveX, CoM, which means that these words are basically the same, representing code (objects) with a package (object) Complete some of the features of a Windows application. The PHP4 COM function can connect an object instance and use its method and properties.

If you want to use the following example source, 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 tag in PHP4

Now let's get started, use the PHP4 COM to instantiate a component, you need the NEW operator and the OLE program ID:

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

?>

Because COM is a PHP4 reserved word, it transmits the identity of this object to a constructor, and now gets an instance of this component. According to the nature of the OOP class, we can easily access its method and properties.

E.g:

$ instance -> [object] -> [Method1] -> [Method2] -> ..-> [proty];

?>

It is so simple!

The structure of the OOP cannot work in PHP, (because the PHP syntax, the name of the attribute, the value of illegal characters, points 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 at the remote computer.

$ INSTANCE = New Com (String "Component Name", string "remote_server_address);

?>

Note: This is the use of the DCOM instruction to set PHP. In the future, PHP developers provide support for DCOM under UNIX.

The logo, method and attribute identifier are a string: MS Word: "Word.Application" or "word.application.9" MS Excel: "Excel.Application" or "excel.sheet" Adobe Acrobat: "Exce.Application "or" pdfdistiller.pdfdistiller "For the last identity, I have to indicate that the correct object identification name is not an easy task. If you can't access the VBA document, you can find a Windows registry, look for your name in HKEY_CLASSES_ROOT, you can get the name of some applications. A valid object identity on your machine is placed under the CLSID subfolder. Applications generally provide documentation to indicate its COM methods and properties. In Office2000, you can run the program, open the VBA editor, select the object editor

. Enter a method name or attribute name in the application library, then use the mouse button in the following window to select a class or member name, point help, you will get a description of this class or member. You can also refer to MSDN. Examples of an Excel are as follows: http://msdn.microsoft.com/library/officededev/off2000/xltocObjectModeLApplication.htm

Operate with COM function MS Word

Now, let's start the first example:

#************************************************************ *********

# This example comes from the Zend site, slightly change

# Open a Word instance and create a document for useless test.doc

# Enter a line of text "this is a test2 ..."

#************************************************************ *********

# Instantiate an object

$ Word = New Com ("Word.Application") or Die ("Unable to Instantiate Word";

# 取 并 并 并 版 版

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 ();

# 写字

$ Word-> Selection-> Typetext ("this is a test ...");

#save

$ Word-> Documents [1] -> Saves ("Useless Test.doc");

#shut down

$ Word-> quit ();

?>

As long as you spend a few minutes to read this program and refer to the Word OLE technical documentation, you will learn almost all the operations you need in your own program.

MS Excel In the use of PHP's COM functions, like the above Word example, you should learn this example while referring to the Help document of the object browser in the Excel's Visual Basic Editor.

# Open Workbook and its Sheet, # This example uses an spreadsheet as Solvsamp.xls $ WORKBOOK = "C: Program FileSmicrosoft OfficeOfficesample"; $ Sheet = "Quick Tour"; $ Sheet = "Quick Tour"; # instantiated one Component's object $ EX = New Com ("" "DID NOT Connect"); # 取程 Name and Version Print "Application Name: {$ EX-> Application-> Value}"; Print Loaded Version: {$ EX-> Application-> Version} "; # Open Work This makes us use it $ wkb = $ ex-> Application-> Workbooks-> Open ($ WORKBOOK) or Die (" DID NOT OPEN " ); # Preserve original workbook, create a working book $ EX-> Application-> ActiveWorkbook-> Saveas ("ottest"); # $ ex-> application-> visible = 1; # 本Note Let Excel visible # 读 写 一 元 in a new worksheet # we can read this cell E11 (Advertising in the 4th. Quarter $ shop = $ wkb-> worksheets; #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: 1000 $ cell-> value = 15000; #change it to 15000 print" new value = {$ Cell-> value} "; # print the new value = 15000 # Finally, recalculate this with the new value Cell $ sheets-> Calculate; # must be calculated, manual is optional # can see the total value of the effect (E13 cell) $ Cell = $ shop-> cells (13, 5); #select the Cell (Row Column Number) $ Number = Number_Format; Print "New Total Cost = $$ Number - WAS, 732 Before."; According to the calculation formula, the advertisement affects the company's overhead, here will display , 809 # Using Excel Built-in Function # 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, 000 loan @

8% Interest / 10 Months: $ pay "; #Shold print monthly payment = $ -1, 037.03 # Optional, save $ ex-> Application-> ActiveWorkbook-> saveas (" ottest "); # 关, do not ask $ EX -> Application-> ActiveWorkbook-> Close ("false"); unset ($ EX);? This example makes your PHP work together with Excel, of course, there are more objects to use, access a self-written The OOP package is also as easy as accessing Excel. Use PHP's COM access Adobe Distiller This last example is not MS program, if your program has a PostScript file, you will be interested in this, change (distillation) it is a PDF Document. Adobe has a program called Distiller that generates an instance. The code is as follows: $ PDF = New COM ("pdfdistiller.pdfdistiller.1);?> Have a little attention, give it in Distiller's documentation This OLE identifier name "pdfdistiller" is invalid. The most basic method of distilling 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 Distiller's parameter file name, and the last two parameters are optional, the default is the same name. This PS file name is with the PDF file name, use this default Job Options file. E.g:

$ PDF-> Filetopdf ($ PSFILE, "," ""); # here $ psfile can be myFile.ps will return myFile.pdf file. ?> There are more methods and properties in Distiller. If you are interested, please refer to Adobe's technical documentation. Abort / Possible issues If you have any errors in your code, you may create an instance but don't close it properly. The worst is that this app may be held by this instance. As a result, there are multiple copies of this program in your program list, even if you are correct, you will interfere with your results. The solution is: Fixing a BUG to clear them before you start running,

And end the task. The same reason, in your code, the program is also closed in time and remove this instance.

You have some tips to handle an exception of COM_GET and COM_SET. E.g:

$ Version = com_get ($ Instance-> Application, "Version");

An error will occur in Word but will generate in Excel.

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

Why do we use it?

转载请注明原文地址:https://www.9cbs.com/read-6850.html

New Post(0)