0

Working on a PHP script to essentially dump the results of a query into a .csv and then upload to a specific folder on our enterprise Google Docs.

The file is created and sent over to Docs just fine. However, it isn't being placed in the proper directory.

This is the function I'm using where $file, $folder, and $newtoken are strings like '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;
}

Maybe I have the folder ID wrong (just copied from URL while viewing in Google Docs)?
Bonus: Can I manipulate the content-type headers to get Google to convert the file to a spreadsheet?

4

1 回答 1

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)并删除第一行。

于 2010-07-15T17:32:02.320 回答