PHP4 and COM components

xiaoxiao2021-03-06  37

Author: Alain M. Samoun Source: Phpbuilder 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.

Identification, method and attribute

The logo is a string as follows:

Ms Word: "Word.Application" or "word.application.9" MS Excel: "Excel.Application" or "excel.sheet" Adobe Acrobat: "Excel.Application" or "pdfdistiller.pdfdistiller"

For the last identification, 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 changed # Opens 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 is using a COM function using PHP

As with the example above, you should learn this example while referring to the Help document for the object browser in the Excel Visual Basic Editor.

# Open Workbook and its Sheet, # This example uses a spreadsheet as the Solvsamp.xls that comes with Excel installation

$ WORKBOOK = "C: program filesmicrosoft officeofficesamplesamples"; $ sheet = "quick tour";

# Instantiate a component of the object $ EX = New Com ("Excel.Sheet") or Die ("DID Not Connect");

# 取程 Name and Version Print "Application Name: {$ EX-> Application-> Value}"; Print "loaded version: {$ ex-> Application-> version}"; # 打 工作 工作 本 使 我们 用WKB = $ EX-> Application-> Workbooks-> Open ($ Workbook) or Die ("DID NOT OPEN");

# Prepare the original workbook, create a working book $ EX-> Application-> ActiveWorkbook-> Saveas ("otest"); # $ ex-> application-> visible = 1; # 本 去 注 注Excel visible

# 读 写 一 一 元 in a new worksheet # we can read this cell E11 (Advertising in the 4th. Quarter) $ shop = $ wkb-> worksheet; #select the sheet $ shop $ shop > 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 = {$ cart-> value} "; # print the new value = 15000

# Finally, recalculate this cell with the new value $ 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 ($ Cell-> Value); Print "New Total Cost = $$ Number - WAS, 732 Before."; Impact the company's overhead, here will be displayed, 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 ";

#SHOULD 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, accessing a self-written OOP package class is also as easy as accessing Excel. Use PHP's COM access Adobe Distiller This last example is not a MS program. If your program has a PostScript file, you will be interested in, to write (distilled) it into a PDF document. Adobe has a program called Distiller, It can generate an instance. code show as below:

$ PDF = New Com ("pdfdistiller.pdfdistiller.1);

?>

One thing to note is that this OLE identifier name "PDFDistiller" given in Distiller's documentation is invalid.

The most basic method of distilling a document 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 problem

If you have any errors in your code, you may create an instance but don't close it normally. 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 in time before you resume running, use 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. For example: $ 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?

I hope that these three examples can give you some thoughts, PHP COM allows you to access Windows4 programs in PHP scripts. This code is simpler than ASP and can integrate other PHP's powerful support features. Microsoft has strongly sold this COM technology in various aspects, under different names and structures, such as Comb (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

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

New Post(0)