COM functions in PHP4 (Windows version)

Source: Internet
Author: User
Tags functions connect ole php and version window access adobe distiller
Introduction to window| Functions

COM functions built into PHP4 are quite attractive for us to develop programs in the Win32 environment, but there is still not much relevant technical documentation. This article will deal with MS office Word, Excel, and Adobe Distiller in three examples to illustrate how to use COM functions in PHP.

COM technology was presented and developed by Microsoft a few years ago, and the terminology mentioned in this article is OLE, OLE Automation, ActiveX, COM, which means basically the same thing, all meaning to use a piece of encapsulated code (object) to complete a Windows Some of the features of the application. The PHP4 COM function can connect to an object instance and use its methods and properties.

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 with PHP4 COM to instantiate a component that requires the new operator and the object's "OLE program ID":


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

?>

Since COM is a PHP4 reserved word that transmits the identity of this object to a constructor, it now obtains an instance of the component, which, based on the nature of the OOP class, makes it easy to access its methods and properties.

For example:


$instance->[object]->[method1]->[method2]-> ->[property];

?>

It's so simple!

OOP's structure does not work under PHP (due to the problem of PHP syntax, the name of the attribute. Values are illegal characters, such as points and parentheses), 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 allows you to create an object instance on a remote computer.


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

?>

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



Identities, methods, and properties

The identity is a string such as the following:

MS Word: "Word.Application" or "Word.application.9"
MS Excel: "Excel.Application" or "Excel.Sheet"
ADOBE Acrobat: "Exch.application" or "Pdfdistiller.pdfdistiller"

For the last ID, I want to point out that getting the right object ID is not an easy task. If you can't access the VBA document, you can find the Windows registry, look for it in the HKEY_CLASSES_ROOT, and you'll get some application names. The valid object ID on your machine is placed under the CLSID subfolder.

The application typically provides documentation describing its COM methods and properties. In office2000, you can run the program, open the VBA editor, and select the Object Editor. Enter a method or property name in the application library, and then, using the right mouse button to select a class or member name in the following window, you will get a description of the class or member. You can also refer to MSDN. An example of Excel is as follows: http://msdn.microsoft.com/library/officedev/off2000/xltocobjectmodelapplication.htm


Manipulating MS Word with COM functions

Now, let's start with the first example:


#*********************************************************
# This example is from the Zend site, slightly changed
# Open an instance of Word and create a new document useless Test.doc
# Enter a line of text ' This is a test2 ... '
#*********************************************************

#实例化一个对象

$word = new COM ("Word.Application") or Die ("Unable to instantiate word");

#取得并显示版本

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

#另一种方法去取得版本

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

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

#使其可见

$word->visible = 1;

#创建新文件

$word->documents->add ();

#写字符

$word->selection->typetext ("This is a test ...");

#保存

$word->documents[1]->saveas ("Useless test.doc");

#关闭

$word->quit ();

?>

Just take a few minutes to read this program and refer to Word's OLE technical documentation, and you'll learn almost all the things you need in your own program.


MS Excel uses PHP's COM function

As with the word example above, you should take this example and refer to the Help documentation for the Object Browser in the Excel Visual Basic Editor.


#打开workbook和它的sheet,
#本例使用一个电子表格是Excel安装时自带的SOLVSAMP. XLS

$workbook = "C:Program filesmicrosoft Officeofficesamplessolvsamp.xls";
$sheet = "Quick Tour";

#实例化一个组件的对象
$ex = new COM ("Excel.Sheet") or Die ("Did not Connect");

#取程序名称和版本
Print "Application name:{$ex->application->value}
" ;
print ' Loaded version: {$ex->application->version}
";

#打开工作本使我们可使用它
$WKB = $ex->application->workbooks->open ($workbook) or Die ("Did not Open");

#预保存原来的工作本, create a copy of the work book
$ex->application->activeworkbook->saveas ("Ourtest");
# $ex->application->visible = 1; #本句去注释让Excel可见

# 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

#最后, recalculate the cell with the new value
$sheets->calculate;
#必须的如果要计算, manual is optional
#可看到效果总价值 (E13 cell)
$cell = $sheets->cells (13,5); #Select the cell (Row Column number)
$number = Number_format ($cell->value);
Print "New Total cost =$ $number-was, 732 before.
";
#根据计算公式, advertising affects the company's overhead, which will show that 809

#使用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 loan @8% INTEREST/10 months: $ $pay
";

#Should Print Monthly payment = $ -1,037.03

#可选, save
$ex->application->activeworkbook->saveas ("Ourtest");
#关闭, no questions.
$ex->application->activeworkbook->close ("False");
Unset ($EX);

?>

This example lets your PHP work with Excel, and of course, there are more objects to use, and accessing a self written OOP wrapper is as easy as accessing Excel.
Use PHP com to access Adobe Distiller

This last example is not a MS program, and if your program has a PostScript file, you will be interested in this, rewriting (distilling) it into a PDF document. Adobe has a program called Distiller, which can generate an instance. The code is as follows:


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

?>

One thing to note is that the OLE identifier named "Pdfdistiller" given in the distiller document is invalid.

The most basic way to distill a file is to:


$pdf->filetopdf ($psfile, Stroutputpdf ', strjoboptions ");

?>

This $psfile is the filename of this PostScript, Stroutputpdf is the file name of the output file PDF. Strjoboptions is the distiller parameter filename, the last two arguments are optional, and the default is the same name. This PS file name with the PDF filename, use this default job options file. For example:


$pdf->filetopdf ($psfile, "", "");
#这儿 $psfile can be myfile.ps will return myfile.pdf files.

?>

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


Suspension/possible problems

If something is wrong with your code, you may create an instance, but do not close it properly. Worst of all, this application may be maintained by this instance, and as a result, there are multiple copies of the program in your list of programs, even if you correct the error and interfere with your results. The workaround is to clean up the bugs in a timely manner before you start running again and end the task. The same reason, at the end of your code, is to close the program in time and delete the instance.

You have some tips for handling com_get and Com_set exceptions. For example:
$Version = Com_get ($instance->application, "Version");

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

There are some objects that cannot be instantiated in PHP4 because the program wants a custom interface, but PHP4 does not support it.


Why do we have to use it?

I hope that these three examples can give you some clues to think about, 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 functions for the database. Microsoft is aggressively selling this COM technology in various ways, under different names and structures, such as COM + (Combine com with Microsoft Transaction Server MTS), ADO, OLE DB, OWC, Windo WS DNA, and so on. The combination of PHP and Apache provides an open source solution.




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.