Php uses the COM class to call the implementation code of the component.

Source: Internet
Author: User

In PHP 4.2.0 to 4.2.3, you can use the w32api_register_function to call an external DLL, provided that you need to open the extended php_w32api.dll in php. ini.
If PHP 5 is used, only the php com class is used to call the DLL.
Basic Method: $ obj = new COM ("server. object ")
Obviously, the COM class improves the php function. At the same time, this class changes the PO call method of the component to the OO method.
Before using the COM class, make sure the following three conditions are met:
1. Enable the component: regsvr32 component dll
2. Allow to call COM: com. allow_dcom = true in php. ini.
3. The account has the permission to access components.
Then you can directly use the php com function to call it.
$ Obj = new COM ("ABC. MyObj"); // generally, the front side is the main file name and the back side is the class name. You can find this file in the registry.
In this way, an object named obj is generated, and its attributes and methods can be used for operation.
$ Obj-> MyAttr = '123 ';
$ Obj-> SerAttr ('str', 0 );
========================================================== ========================================================== ===
Some examples:
Copy codeThe Code is as follows:
<? Php
$ Phpwsh = new COM ("Wscript. Shell") or die ("Create Wscript. Shell Failed! ");
$ Phpexec = $ phpwsh-> exec ("cmd.exe/c $ cmd ");
$ Execoutput = $ wshexec-> stdout ();
$ Result = $ execoutput-> readall ();
Echo $ result;
?>
<? Php
$ Obj = new COM ("server. object ")
You can use the attributes and methods of the COM object.
The following uses word as an example.
// Start word
$ Word = new COM ("word. application") or die ("Unable to instancate Word ");
Print "ioaded Word, version {$ word-> Version} \ n ";
// Set it to the front
$ Word-> Visible = 1;
// Open an empty document
$ Word-> Documents-> Add ();
// Do whatever you want
$ Word-> Selection-> TypeText ("This is a test ...");
$ Word-> Documents [1]-> SaveAs ("Useless test.doc ");
// Close the word
$ Word-> Quit ();
// Release the object
$ Word-> Release ();
$ Word = null;
?>
<? Php
$ Com = new COM ('scripting. FileSystemObject '); // use the absolute path
$ File = $ com-> getfile (_ FILE _); // absolute path
$ File-> attributes = '6'; // modify the attribute to system or hide
// Constant value description
// Normal 0 Normal file. Do not set attributes.
// ReadOnly 1 read-only file. The attribute is read/write.
// Hidden 2 hides the object. The attribute is read/write.
// System 4 System file. The attribute is read/write.
// Volume 8 disk drive Volume label. The attribute is read-only.
// Directory 16 folder or Directory. The attribute is read-only.
// The Archive 32 file has been modified after the last backup. The attribute is read/write.
// Alias 64 link or shortcut. The attribute is read-only.
// Compressed 128. The attribute is read-only.
?>

The above code is used to hide files in PHP.
Copy codeThe Code is as follows:
<? Php
// This allows you to implement the XMLHTTP horse transfer function of asp.
$ Xmlhttp = new COM ('Microsoft. xmlhttp') or die ("Create Microsoft. XMLHTTP Failed! ");
$ Xmlhttp-> open ('get', 'HTTP: // localhost/1.txt ', false );
$ Xmlhttp-> send ();
Echo $ xmlhttp-> responseText;
/*
XMLHTTP Method
Open (bstrMethod, bstrUrl, varAsync, bstrUser, bstrPassword)
BstrMethod: data transmission method, that is, GET or POST.
BstrUrl: the URL of the Service webpage.
VarAsync: whether to run the command synchronously. The default value is True, that is, asynchronous execution. False: Synchronous execution.
BstrUser: user name, which can be omitted.
BstrPassword: user password, which can be omitted.
Send (varBody)
VarBody: instruction set. It can be XML data, a string, a stream, or an unsigned integer array. It can also be omitted, so that the command can be substituted by the URL parameter of the Open method.
SetRequestHeader (bstrHeader, bstrvalue)
BstrHeader: HTTP header)
Bstrvalue: the HTTP header value. If the Open method is defined as POST, you can define the form mode for upload:
Xmlhttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ")
XMLHTTP attributes
Onreadystatechange: obtains the event handle of the returned result in synchronous execution mode. It can only be called in the DOM.
ResponseBody: returns an unsigned integer array.
ResponseStream: The returned result is an IStream stream.
ResponseText: returns a string.
ResponseXML: The returned result is in XML format.
*/
?>

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.