我终于用DaImTo解决了这个问题,指着我朝着正确的方向前进。获得file resource后,我用它获取文档HTML代码的导出链接,然后使用该链接使用Google_Http_Request检索该文档的HTML内容。 (Google documentation这部分)
public function retrive_file_outline($FileID) {
//authenticate
$this->authenticate();
$Service = new Google_Service_Drive($this->Client);
$File = $Service->files->get($FileID);
$DownloadUrl = $File->getExportLinks()["text/html"];
if ($DownloadUrl) {
$Request = new Google_Http_Request($DownloadUrl, 'GET', null, null);
$HttpRequest = $Service->getClient()->getAuth()->authenticatedRequest($Request);
if ($HttpRequest->getResponseHttpCode() == 200) {
return array($File, $HttpRequest->getResponseBody());
} else {
// An error occurred.
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}
之后,我解析使用DOMDocument HTML内容。所有标题都有id属性,用作anchor链接。我检索了所有标题(h1到h6)的id,并将其与我的文档编辑网址连接起来。这给了我所有的大纲链接。这里是解析和连接部分:
public function test($FileID) {
$File = $this->model_google->retrive_file_outline($FileID);
$DOM = new DOMDocument;
$DOM->loadHTML($File[1]);
$TagNames = ["h1", "h2", "h3", "h4", "h5", "h6"];
foreach($TagNames as $TagName) {
$Items = $DOM->getElementsByTagName($TagName);
foreach($Items as $Item) {
$ID = $Item->attributes->getNamedItem("id");
echo "<a target='_blank' href='" . $File[0]->alternateLink ."#heading=". $ID->nodeValue . "'>" . $Item->nodeValue . "</a><br />";
}
}
//echo $File;
}
编辑: 我合并功能retrieve_file_outline和考不上retrieve_file_outline,我得到一个返回的链接和IDS文档标题的阵列功能:
public function retrive_file_outline($FileID) {
//authenticate
$this->authenticate();
$Service = new Google_Service_Drive($this->Client);
$File = $Service->files->get($FileID);
$DownloadUrl = $File->getExportLinks()["text/html"];
if ($DownloadUrl) {
$Request = new Google_Http_Request($DownloadUrl, 'GET', null, null);
$HttpRequest = $Service->getClient()->getAuth()->authenticatedRequest($Request);
if ($HttpRequest->getResponseHttpCode() == 200) {
$DOM = new DOMDocument;
$DOM->loadHTML($HttpRequest->getResponseBody());
$TagNames = ["h1", "h2", "h3", "h4", "h5", "h6"];
$Headings = array();
foreach($TagNames as $TagName) {
$Items = $DOM->getElementsByTagName($TagName);
foreach($Items as $Item) {
$ID = $Item->attributes->getNamedItem("id");
$Heading = array(
"link" => $File->alternateLink . "#heading=" . $ID->nodeValue,
"heading_id" => $ID->nodeValue,
"title" => $Item->nodeValue
);
array_push($Headings, $Heading);
}
}
return $Headings;
} else {
// An error occurred.
return null;
}
} else {
// The file doesn't have any content stored on Drive.
return null;
}
}