$obj = new COM("Server.Object"); // ProgID:通常為「程式庫.類別」
$obj->Property = $value; // 設定屬性
$obj->Method($arg); // 呼叫方法
<?php
$wsh = new COM("WScript.Shell") or die("WScript.Shell 建立失敗");
$exec = $wsh->Exec("cmd /c dir"); // 執行命令
$output = $exec->StdOut->ReadAll(); // 讀取標準輸出
echo "$output
";
?>
<?php
$word = new COM("Word.Application") or die("Word 啟動失敗");
echo "Word 版本: {$word->Version}\n";
$word->Visible = 1; // 顯示視窗(伺服器端建議 0)
$word->Documents->Add(); // 新增空白文件
$word->Selection->TypeText("這是 COM 自動化測試內容。");
$word->Documents[1]->SaveAs("C:/temp/com_test.doc");
$word->Quit();
$word->Release();
$word = null; // 釋放參考
?>
<?php
$fso = new COM("Scripting.FileSystemObject");
$file = $fso->GetFile(__FILE__); // 必須使用絕對路徑
$file->Attributes = 6; // 2(Hidden) + 4(System) = 6
// 常數對照:
// 0 Normal, 1 ReadOnly, 2 Hidden, 4 System, 8 Volume, 16 Directory, 32 Archive, 64 Link, 128 Compressed
?>
<?php
$xmlhttp = new COM("Microsoft.XMLHTTP") or die("XMLHTTP 建立失敗");
$xmlhttp->open("GET", "https://localhost/1.txt", false); // 同步
$xmlhttp->send();
echo $xmlhttp->responseText;
// 方法:open(method, url, async, user, pwd)
// send(body) | setRequestHeader(header, value)
// 屬性:responseText, responseXML, responseBody, responseStream, status, statusText
?>
僅適用於 Windows + PHP 環境,Linux 請改用 Wine 或跨平台替代方案。