unable to open spreadsheets with Zend_Gdata?

本小妞迷上赌 提交于 2019-12-13 20:14:57

问题


<?php
include('Zend/Gdata.php');
include('Zend/Gdata/Spreadsheets.php');
include('Zend/Gdata/ClientLogin.php');


$service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME;
$client = Zend_Gdata_ClientLogin::getHttpClient('myemail@gmail.com', 'password', $service);
$spreadsheetService = new Zend_Gdata_Spreadsheets($client);
$sheets = $spreadsheetService->getSpreadsheetFeed();
foreach ($sheets as $sheet) {
    //echo get_class($sheet) . '<br>'; exit;
    echo $sheet->getContent() . '<br>';
    echo $sheet->getId() . '<br>';

    $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
    $query->setSpreadsheetKey($sheet->getId());
    $feed = $spreadsheetService->getWorksheetFeed($query);
    echo '<pre>';
    print_r($feed); exit;
}

Here's the error I get when I do that:

Fatal error: Uncaught exception 'Zend_Gdata_App_HttpException' with message 'Expected response code 200, got 400 Invalid request URI'

That doesn't make a lot of sense to me. If they spreadsheet key was invalid why would $sheet->getId() be returning it?


回答1:


EDIT: Have updated this to make it more in line with what I think you want - let me know if it's off the mark but this may help. I'm assuming you're using an MVC pattern with Zend Framework so you may need to tweak accordingly otherwise Try this class:

class SpreadsheetAdapter
{
    protected $_spreadsheetService;

    public function __construct()
    {
        $client = 
            Zend_Gdata_ClientLogin::getHttpClient($yourUsername,$yourPassword,
                Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME);

        $this->_spreadsheetService = new Zend_Gdata_Spreadsheets($client);
    }

    public function getSheets()
    {
        $sheetArray = array();
        $totalSheets = count($this->_spreadsheetService->getSpreadsheetFeed()->entries[0]->link);

        for($i = 0; $i <= $totalSheets; $i++)
        {
            $link{$i} = $this->_spreadsheetService->getSpreadsheetFeed()->entries[$i]->link[1]->href;
            $title{$i} = $this->_spreadsheetService->getSpreadsheetFeed()->entries[$i]->getTitleValue();
            $sheetArray[$link{$i}] = $title{$i};
        }

        return $sheetArray; 
    }

    public function getRows($spreadsheetKey)
    {
        // returns a multidimensional array filled with data from populated rows in a Google Docs Spreadsheet
        $query = new Zend_Gdata_Spreadsheets_DocumentQuery();
        $query->setSpreadsheetKey($spreadsheetKey);
        $feed = $this->_spreadsheetService->getWorksheetFeed($query);
        return $feed->entries[0]->getContentsAsRows();
    }

}

Then this in your chosen controller:

public function spreadAction()
{
    $adapter = new SpreadsheetAdapter();
    $sheetArray = $adapter->getSheets();
    $this->view->sheetArray = $sheetArray;      
}

Then this in your view:

<h2>Spreadsheets</h2>
<ul>
<?php 
foreach($this->sheetArray as $sheet['link'] => $sheet['title'])
{
    echo '<li><a href="' . $sheet['link'] . '">' . $sheet['title'] . '</a></li>';
}

?>
</ul>

NOTE: While there are probably other ways of doing this you could get hold of just the spreadsheet key by using something like this (I've just modified what would be in the view):

<h2>Spreadsheets</h2>
<ul>
<?php 
foreach($this->sheetArray as $sheet['link'] => $sheet['title'])
{
    $linkChop = explode('=',$sheet['link']);
    $spreadsheetKey = end($linkChop);
    echo '<li><a href="http://yourdomain/yourmodule/yourcontroller/youraction/' . $spreadsheetKey . '">' . $sheet['title'] . '</a></li>';
}

?>
</ul>

In the above example I was trying to illustrate how you might use just the key. If I was doing this in ZF myself I'd bind $spreadsheetKey as a parameter in the url when routing to an action where I'd want to use a method such as getRows(). If you want an example I could put one together?

Appreciate I'm guessing a bit as to what you want to achieve but hopefully this helps.

Cheers,

Dave

ABOUT 2 STEP AUTH: Something slightly off topic which may be helpful is to make sure 2-step authentication is turned off on the Google account as it will stop you from connecting this way.



来源:https://stackoverflow.com/questions/9014164/unable-to-open-spreadsheets-with-zend-gdata

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!