使用 Zend Gdata 从 Google Docs 删除文档

发布于 2024-11-02 16:24:31 字数 903 浏览 0 评论 0原文

我在使用 Google Docs 和 Zend Framework 1.11.4 时遇到问题。

我想做的是将文档上传到 Google Docs,检索 HTML 内容并删除该文档。我正在处理 .doc、.pdf 和 .rtf 文件。

到目前为止,我的代码:

$client = Zend_Gdata_ClientLogin::getHttpClient(
    '[email protected]', 
    'MyPassword', 
    Zend_Gdata_Docs::AUTH_SERVICE_NAME
);
$gdClient = new Zend_Gdata_Docs($client);

$newDocumentEntry = $gdClient->uploadFile(
    $file, 
    null, 
    null, 
    Zend_Gdata_Docs::DOCUMENTS_LIST_FEED_URI
);

$cv = file_get_contents($newDocumentEntry->getContent()->getSrc());

$newDocumentEntry->delete();

一切正常,直到调用 ->delete() 方法,它返回异常 预期响应代码 200,得到 409

我已经在谷歌上搜索了几天了,但是找不到答案,根据Google的文档这是删除文档的正确方法。

如果有人知道我做错了什么,那么非常欢迎任何帮助。

预先非常感谢, 加里

I am having trouble working with Google Docs and Zend Framework 1.11.4.

What I am attempting to do is upload a document to Google Docs, retrieve the HTML content and delete the document. I am working with .doc, .pdf, and .rtf files.

My code so far:

$client = Zend_Gdata_ClientLogin::getHttpClient(
    '[email protected]', 
    'MyPassword', 
    Zend_Gdata_Docs::AUTH_SERVICE_NAME
);
$gdClient = new Zend_Gdata_Docs($client);

$newDocumentEntry = $gdClient->uploadFile(
    $file, 
    null, 
    null, 
    Zend_Gdata_Docs::DOCUMENTS_LIST_FEED_URI
);

$cv = file_get_contents($newDocumentEntry->getContent()->getSrc());

$newDocumentEntry->delete();

Everything works fine until the ->delete() method is called, it returns an exception Expected response code 200, got 409

I have been Googling this for a couple of days now but can find no answer, according to Googles documentation this is the correct way to delete a document.

If anyone has any idea as to what I am doing wrong then any help would be very welcome.

Many thanks in advance,
Garry

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

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

发布评论

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

评论(1

孤寂小茶 2024-11-09 16:24:31

使用 Zend_Gdata_Calendar 库时,我遇到了同样的 409 响应问题。 Zend 框架错误跟踪器上有一个未解决的错误。请参阅 http://zendframework.com/issues/browse/ZF-10194

似乎归结为缺少由 Gdata_App 类或链中的子类之一设置的“If-Match”标头。

为了修复 Calendar API 的问题,我重写了 Zend_Gdata_Calendar 类并实例化了我的类而不是该类:

class Zend_Gdata_Calendar_Fixed extends \Zend_Gdata_Calendar {
    /**
     * Overridden to fix an issue with the HTTP request/response for deleting.
     * @link http://zendframework.com/issues/browse/ZF-10194
     */
    public function prepareRequest($method,
                                   $url = null,
                                   $headers = array(),
                                   $data = null,
                                   $contentTypeOverride = null) {
        $request = parent::prepareRequest($method, $url, $headers, $data, $contentTypeOverride);

        if($request['method'] == 'DELETE') {
            // Default to any
            $request['headers']['If-Match'] = '*';

            if($data instanceof \Zend_Gdata_App_MediaSource) {
                $rawData = $data->encode();
                if(isset($rawData->etag) && $rawData->etag != '') {
                    // Set specific match
                    $request['headers']['If-Match'] = $rawData->etag;
                }
            }
        }
        return $request;
    }
}

然后使用它:

...
$gdata = new Zend_Gdata_Calendar_Fixed($httpClient);
...

我想您可以做同样的事情,但重写 Zend_Gdata_Docs 类。

I was having the same 409 response issue when using the Zend_Gdata_Calendar library. There is an open bug on the Zend framework bugtracker. See http://zendframework.com/issues/browse/ZF-10194

It seems to boil down to the lack of a "If-Match" header being set by the Gdata_App class or one of the child classes in the chain.

To fix it for the Calendar API I've overridden the Zend_Gdata_Calendar class and instantiated my class instead of that one:

class Zend_Gdata_Calendar_Fixed extends \Zend_Gdata_Calendar {
    /**
     * Overridden to fix an issue with the HTTP request/response for deleting.
     * @link http://zendframework.com/issues/browse/ZF-10194
     */
    public function prepareRequest($method,
                                   $url = null,
                                   $headers = array(),
                                   $data = null,
                                   $contentTypeOverride = null) {
        $request = parent::prepareRequest($method, $url, $headers, $data, $contentTypeOverride);

        if($request['method'] == 'DELETE') {
            // Default to any
            $request['headers']['If-Match'] = '*';

            if($data instanceof \Zend_Gdata_App_MediaSource) {
                $rawData = $data->encode();
                if(isset($rawData->etag) && $rawData->etag != '') {
                    // Set specific match
                    $request['headers']['If-Match'] = $rawData->etag;
                }
            }
        }
        return $request;
    }
}

Then use it:

...
$gdata = new Zend_Gdata_Calendar_Fixed($httpClient);
...

I imagine you could do the same thing but overriding the Zend_Gdata_Docs class instead.

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