2010-07-15 54 views
0

使用PHP腳本將查詢結果本質上轉儲爲.csv,然後上傳到我們的企業Google文檔上的特定文件夾。將CSV發佈到Google文檔文件夾

該文件被創建併發送到文檔就好了。但是,它不會被放置在正確的目錄中。

這是我使用的地方$file$folder$newtoken就像'dump.csv''0B0rVKeOmIwGXMGU2MzYyN2EtZWV...'[auth token from Google]字符串的函數。

function insert($file, $folder, $newtoken){ 
    $mime = "Media multipart posting\n--END_OF_PART\n"; 
    $mime .= "Content-Type: application/atom+xml\n\n"; 
    $xml="<?xml version='1.0' encoding='UTF-8'?> 
    <entry xmlns=\"http://www.w3.org/2005/Atom\" xmlns:docs=\"http://schemas.google.com/docs/2007\"> 
     <category scheme=\"http://schemas.google.com/g/2005#kind\" 
     term=\"http://schemas.google.com/docs/2007#document\"/> 
     <title>example document</title> 
     <docs:writersCanInvite value=\"true\" /> 
    </entry> 
    "; 
    $mime.=$xml."\n--END_OF_PART\n"; 

    $document = "Content-Type: text/plain\n\n"; 
    $handle = fopen($file, "r"); 
    $contents = fread($handle, filesize($filename)); 
    fclose($handle); 
    $document.=$contents; 
    $itemURL = "https://docs.google.com/feeds/default/private/full"; 
    $mime .= $document."\n--END_OF_PART--\n"; 

    $headers = array(); 
    $headers[] = "POST /feeds/default/private/full/folder%3A".$folder."/contents HTTP/1.1"; 
    $headers[] = "Host: docs.google.com"; 
    $headers[] = "GData-Version: 3.0"; 
    $headers[] = "Authorization: GoogleLogin auth=".$newtoken.""; 
    $headers[] = "Content-Length: ".strlen($mime); 
    $headers[] = "Content-Type: multipart/related; boundary=END_OF_PART"; 
    $headers[] = "Slug: Test"; 

    $curl = curl_init(); 
    curl_setopt($curl,CURLOPT_URL,$itemURL); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); 
    curl_setopt($curl,CURLOPT_POSTFIELDS, $mime); 
    $result = curl_exec($curl); 
    curl_close($curl); 

    echo 'Response: '.$result; 
} 

也許我的文件夾ID錯誤(剛剛從Google文檔中查看時從URL中複製)?
獎勵:我可以操縱內容類型標題讓Google將文件轉換爲電子表格嗎?

回答

0

你有

$headers[] = "POST /feeds/default/private/full/folder%3A".$folder."/contents HTTP/1.1"; 

然後:

curl_setopt($curl,CURLOPT_URL,$itemURL); 

下定決心。我認爲第一個是錯誤的;您無法通過CURLOPT_HTTPHEADER選項更改URL。將$itemURL設置爲完整URL(包括/feeds/default/private/full/folder%3A".$folder."/contents)並刪除第一行。

相關問題