<?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-&gt; getId()会返回它?
答案 0 :(得分:0)
编辑:已更新此内容,使其更符合我的想法 - 让我知道它是否符合标准,但这可能会有所帮助。我假设您正在使用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>
注意:虽然可能有其他方法可以通过使用类似的东西来获取电子表格键(我刚刚修改了视图中的内容):< / p>
<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步骤AUTH:稍微偏离主题可能有帮助的是确保Google帐户上的两步验证功能已关闭,因为它会阻止您以这种方式连接。