在google中,您可以看到和导航thorugh文档大纲。我试图通过Google访问这个大纲,但是我找不到相关的文档。这是我现在的代码:
//authenticate
$this->authenticate();
$Service = new Google_Service_Drive($this->Client);
$File = $Service->files->get($FileID);
return $File;我得到了document对象,但是找不到任何返回outline的函数。我需要大纲链接从我的应用程序访问文档的特定部分。有什么办法可以做到吗?
发布于 2016-10-07 06:30:42
我最终解决了这个问题,DaImTo为我指明了正确的方向。在获得文件资源后,我使用它获取文档的HTML代码的导出链接,然后使用该链接使用Google_Http_Request检索该文档的HTML。(本部为Google文档)
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属性,这些属性用作锚点链接。我检索了所有标头的id (h1到h6),并将其与文档编辑url连接起来。这给了我所有的大纲链接。下面是解析和连接部分:
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;
}
}发布于 2016-10-05 07:27:04
File.get返回一个文件资源 --所有的文件资源只是文件的元数据。它是关于存储在google驱动器上的文件的信息。
您需要将其加载到某个文档应用程序中,以找到任何大纲链接。元数据不包含与文件中存储的数据有关的任何内容。
https://stackoverflow.com/questions/39867636
复制相似问题