无法使用Zend_Gdata打开电子表格? [英] unable to open spreadsheets with Zend_Gdata?

查看:113
本文介绍了无法使用Zend_Gdata打开电子表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<?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;
}

这是我这样做时遇到的错误:

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

这对我来说没有多大意义.如果他们的电子表格密钥无效,为什么$ sheet-> getId()返回它?

解决方案

已对其进行了更新,使其更加符合我的想法-请告诉我它是否合乎要求但这可能会有所帮助.我假设您正在Zend Framework上使用MVC模式,因此您可能需要相应地进行调整 试试这个课程:

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();
    }

}

然后在您选择的控制器中进行以下操作:

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

然后在您看来:

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

?>
</ul>

注意::虽然可能还有其他方法,但是您可以通过使用类似这样的东西来掌握电子表格密钥(我刚刚修改了视图中的内容):

<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>

在上面的示例中,我试图说明如何使用键.如果我自己在ZF中执行此操作,则在路由到要使用诸如getRows()之类的操作的操作时,我会将$ spreadsheetKey绑定为url中的参数.如果您想举一个例子,我可以把它放在一起吗?

赞赏,我对您想要实现的目标有点猜测,但希望这会有所帮助.

干杯

戴夫

关于2步身份验证:稍微有些偏离的话题可能会有所帮助,是确保Google帐户上的2步身份验证已关闭,因为它会阻止您以这种方式进行连接.

<?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?

解决方案

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.

这篇关于无法使用Zend_Gdata打开电子表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆