2016-10-05 12 views
0

Googleドキュメントでは、あなたはぼんやりしたドキュメントの概要を確認してナビゲートできます。 GoogleドライブAPIを使用してこの概要にアクセスしようとしていますが、ドキュメントを見つけることができません。これは今の私のコードです:GoogleドライブAPI - ドキュメントの概要を取得

//authenticate 
    $this->authenticate(); 

    $Service = new Google_Service_Drive($this->Client); 
    $File = $Service->files->get($FileID); 

    return $File; 

私はドキュメントオブジェクトを取得しますが、アウトラインを返す関数は見つかりません。私は概要リンクが私のアプリケーションからドキュメントの特定の部分にアクセスする必要があります。これはどのようにして実現できますか?

答えて

0

最後に私はDaImToを正しい方向に向けてこの問題を解決しました。 file resourceを取得した後、ドキュメントのHTMLコードのエクスポートリンクを取得し、そのリンクを使用してGoogle_Http_RequestでそのドキュメントのHTMLコンテンツを取得しました。その後

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; 
    } 
} 

Google documentationこの部分のために)私はDOMDocumentを使用してHTMLコンテンツを解析されました。すべてのヘッダーにはid属性があり、これはanchorリンクとして使用されます。すべてのヘッダー(h1〜h6)のIDを取得し、ドキュメントの編集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; 
} 

EDIT: 私はretrieve_file_outlineに機能retrieve_file_outlineとテストを合併し、私はリンクやIDを持つ文書の見出しの配列を返す関数だ:

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; 
    } 
} 
1

File.getは、file resourceを返します。すべてのファイルリソースは、ファイルのメタデータです。 Googleドライブに保存されているファイルに関する情報。

アウトラインリンクを見つけるには、ドキュメントアプリケーションでロードする必要があります。メタデータには、ファイルに格納されているデータに関する情報は含まれていません。

関連する問題