COM functions in PHP4 (Windows Version)

xiaoxiao2021-03-06  73

COM functions in PHP4 (Windows Version)

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 be divided into three examples

Don't process MS Office 2000 Word, Excel, Adobe Distiller to explain how to use COM functions in PHP.

The COM technology is made and developed by Microsoft, and the relevant nouns mentioned herein have OLE, OLE Automation, ActiveX, COM, these words mean

All kinds of packages (objects) are used to complete some of the features of a Windows application with a packaged code (object). The PHP4 COM function can connect an object instance and use it.

Attributes.

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 I get an instance of this component. According to the nature of the OOP class, we can be easy.

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 registration of Windows

Table, look for 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 an application

A method name or attribute name in the library, then, right-click in the following window Select a class or member name, point help, you will get a description of this class or member. you also

You can 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 $ 47,732 Before."; The company's overhead will be shown in $ 57,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 $ 10,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. Access Adobe Distiller with PHP COM

This last example is not the MS program. If your program has a PostScript file, you will be interested in, rewritten (distillation) it becomes a PDF document. Adobe has one

A program called Distiller, which 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 parameter file name of Distiller, the last two parameters

It is 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.

If you have 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

Use and end the task before you start running. 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 function for the database. Microsoft has vigorously sell 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-93303.html

New Post(0)