开发者

Delete document from Google Docs using Zend Gdata

开发者 https://www.devze.com 2023-02-27 03:32 出处:网络
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

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(
    'my@googleDocsEmail.address', 
    '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开发者_StackOverflow中文版 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号