将 CSV 发布到 Google 文档文件夹

发布于 2024-09-09 16:27:55 字数 2080 浏览 0 评论 0原文

使用 PHP 脚本将查询结果转储到 .csv 中,然后上传到我们企业 Google 文档上的特定文件夹。

该文件已创建并发送到文档就好了。但是,它没有被放置在正确的目录中。

这是我正在使用的函数,其中 $file$folder$newtoken 是类似于 'dump.csv' 的字符串'0B0rVKeOmIwGXMGU2MzYyN2EtZWV...'[来自 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 将文件转换为电子表格吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

誰認得朕 2024-09-16 16:27:55

你已经然后

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

You have

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

and then:

curl_setopt($curl,CURLOPT_URL,$itemURL);

Make up your mind. I think the first one is wrong; you can't change the URL via the CURLOPT_HTTPHEADER option. Set $itemURL to the full URL (including /feeds/default/private/full/folder%3A".$folder."/contents) and remove the first line.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文