我一直在使用Zend Gdata一段时间了,今天我得到了一个错误
Notice: Undefined offset: ClientLogin.PHP on line 150
通过PHP,这已经工作了一段时间,今天没有改变任何它停止工作,我猜测一些弃用的服务代表谷歌与zend gdata可能Zend_Gdata_ClientLogin :: getHttpClient()方法或其他什么,可以任何一个确认或帮我解决这个问题.我用来连接的代码如下:
require_once('Zend/Loader.PHP'); Zend_Loader::loadClass('Zend_Gdata'); Zend_Loader::loadClass('Zend_Gdata_ClientLogin'); Zend_Loader::loadClass('Zend_Gdata_Docs'); Zend_Loader::loadClass('Zend_Gdata_Spreadsheets'); require_once 'Zend/Gdata.PHP'; require_once 'Zend/Gdata/AuthSub.PHP'; require_once 'Zend/Gdata/Spreadsheets.PHP'; require_once 'Zend/Gdata/Spreadsheets/DocumentQuery.PHP'; require_once 'Zend/Gdata/Spreadsheets/ListQuery.PHP'; require_once 'Zend/Loader.PHP'; $sourceUser = "myemail"; $sourcePass = "mysuperawesomepassword"; $service = Zend_Gdata_Spreadsheets::AUTH_SERVICE_NAME; $sourceClient = Zend_Gdata_ClientLogin::getHttpClient($sourceUser,$sourcePass,$service); $connection = new Zend_Gdata_Spreadsheets($sourceClient);
我正在使用zend gdata和谷歌电子表格
错误也指向此行
$sourceClient = Zend_Gdata_ClientLogin::getHttpClient($sourceUser,$service);
就像我说的那样,我现在已经使用了一段时间了,我的结局没有任何改变
最后,我结束了这样的事情(到目前为止非常原始的代码,但是对于那些正在寻求解决方案的人来说也是如此.你需要
https://github.com/asimlqt/php-google-spreadsheet-client的PHP google电子表格客户端).这是在我的电子表格中插入一行的一个小例子(对我的代码感到抱歉,但只显示工作示例)
感谢bram brambring显示更好的授权方式 – answer by bram brambring
原文链接:https://www.f2er.com/php/136797.html感谢bram brambring显示更好的授权方式 – answer by bram brambring
<?PHP /* * Google Spreadsheet class to work with google spreadsheets obvIoUsly ;D [using OAuth 2.0,as Zend Gdata is not anymore working] */ require_once('/Google/Spreadsheet/ServiceRequestInterface.PHP'); require_once('/Google/Spreadsheet/DefaultServiceRequest.PHP'); require_once('/Google/Spreadsheet/ServiceRequestFactory.PHP'); require_once('/Google/Spreadsheet/Spreadsheet.PHP'); require_once('/Google/Spreadsheet/SpreadsheetFeed.PHP'); require_once('/Google/Spreadsheet/SpreadsheetService.PHP'); require_once('/Google/Spreadsheet/Exception.PHP'); require_once('/Google/Spreadsheet/UnauthorizedException.PHP'); require_once('/Google/Spreadsheet/Spreadsheet.PHP'); require_once('/Google/Spreadsheet/Util.PHP'); require_once('/Google/Spreadsheet/Worksheet.PHP'); require_once('/Google/Spreadsheet/WorksheetFeed.PHP'); require_once('/Google/Spreadsheet/ListFeed.PHP'); require_once('/Google/Spreadsheet/ListEntry.PHP'); require_once('/Google/Spreadsheet/CellFeed.PHP'); require_once('/Google/Spreadsheet/CellEntry.PHP'); require_once('/Google/Config.PHP'); require_once('/Google/Client.PHP'); require_once('/Google/Auth/Abstract.PHP'); require_once('/Google/Auth/OAuth2.PHP'); require_once('/Google/Http/Request.PHP'); require_once('/Google/Utils.PHP'); require_once('/Google/IO/Abstract.PHP'); require_once('/Google/IO/Curl.PHP'); require_once('/Google/Http/CacheParser.PHP'); require_once('/Google/Logger/Abstract.PHP'); require_once('/Google/Logger/Null.PHP'); require_once('/Google/Exception.PHP'); require_once('/Google/Auth/Exception.PHP'); require_once('/Google/Auth/AssertionCredentials.PHP'); require_once('/Google/Cache/Abstract.PHP'); require_once('/Google/Cache/File.PHP'); require_once('/Google/Signer/Abstract.PHP'); require_once('/Google/Signer/P12.PHP'); use Google\Spreadsheet\DefaultServiceRequest; use Google\Spreadsheet\ServiceRequestFactory; class Google_Spreadsheet { private $default = array( 'worksheetCols' => 12,'worksheetRows' => 25 ); private $spreadsheetKey; private $spreadsheetName; private $worksheetName; private $spreadsheetFeed; public $initialized = true; public function __construct($spreadsheetKey,$worksheetName,$spreadsheetName = '') { $this->spreadsheetKey = $spreadsheetKey; $this->worksheetName = $worksheetName; $this->spreadsheetName = $spreadsheetName; $this->initialized = $this->initialize(); return true; } private function getToken() { $client_email = '318977712937456456454656563tcfjblgoi@developer.gserviceaccount.com'; $private_key = file_get_contents('API Project-f10e456456b60.p12'); $scopes = array('https://spreadsheets.google.com/Feeds'); $credentials = new Google_Auth_AssertionCredentials( $client_email,$scopes,$private_key,'notasecret',// Default P12 password 'http://oauth.net/grant_type/jwt/1.0/bearer' // Default grant type ); $client = new Google_Client(); $client->setAssertionCredentials($credentials); if ($client->getAuth()->isAccessTokenExpired()) { $client->getAuth()->refreshTokenWithAssertion(); } $tokenData = json_decode($client->getAccessToken()); return $tokenData->access_token; } public function initialize(/*$reInitialized = false*/) { // load OAuth2 token data - exit if false $tokenData = $this->getToken(); $serviceRequest = new DefaultServiceRequest($tokenData); ServiceRequestFactory::setInstance($serviceRequest); $spreadsheetService = new Google\Spreadsheet\SpreadsheetService(); try { $spreadsheetFeed = $spreadsheetService->getSpreadsheets(); } catch (\Google\Spreadsheet\UnauthorizedException $e) { Google_Spreadsheet::warnAdmin($e->getMessage()); return false; } $this->spreadsheetFeed = $spreadsheetFeed; return true; } public function insertRow($rowData,$default_fields = array()) { $spreadsheetFeed = $this->spreadsheetFeed; $spreadsheet = $this->spreadsheetKey ? $spreadsheetFeed->getByKey($this->spreadsheetKey) : $spreadsheetFeed->getByTitle($this->spreadsheetName); if(!$spreadsheet && !empty($this->spreadsheetName)) { $spreadsheet = $spreadsheetFeed->getByTitle($this->spreadsheetName); } if(!$spreadsheet) { Google_Spreadsheet::warnAdmin('No spreadsheet',serialize($rowData)); return false; } $worksheetFeed = $spreadsheet->getWorksheets(); $worksheet = $worksheetFeed->getByTitle($this->worksheetName); if(!$worksheet) { //create worksheet if not exist $worksheet = $spreadsheet->addWorksheet($this->worksheetName,$this->default['worksheetRows'],$this->default['worksheetCols']); $cellFeed = $worksheet->getCellFeed(); for( $i= 1 ; $i <= $this->default['worksheetCols']; $i++ ) { if(isset($default_fields[$i])) { $cellFeed->editCell(1,$i,$default_fields[$i]); } else { $cellFeed->editCell(1,"head"); } $cellFeed->editCell(2,"content"); } } if(!$worksheet) { Google_Spreadsheet::warnAdmin('No worksheet',serialize($rowData)); return false; } $listFeed = $worksheet->getListFeed(); $data = array(); foreach ($listFeed->getEntries() as $entry) { $values = $entry->getValues(); $data[] = $values; break; //only first row needed,as we need keys } $keys = array(); if(!count($data)) { Google_Spreadsheet::warnAdmin('No data',serialize($rowData)); return false; } foreach ($data[0] as $key => $value) { $keys[] = $key; } $newRow = array(); $count = 0; foreach($keys as $key) { if(isset($rowData[$count])) { $newRow["$key"] = $rowData[$count]; } else { $newRow["$key"] = ''; } $count++; } $listFeed->insert($newRow); return true; } static function warnAdmin($reason = '',$content = '') { //temporal function to warn myself about all the stuff happening wrong :) } }
在我使用的主模型中:
$spreadsheet = new Google_Spreadsheet("spreadsheet name or ID",$worksheetname,"My spreadsheet name"); if(!$spreadsheet->initialized) { Google_Spreadsheet::warnAdmin('cannot initialize spreadsheet',serialize($rowValues)); } if(!$spreadsheet->initialized || !$spreadsheet->insertRow($rowValues,$this->default_fields)) { Google_Spreadsheet::warnAdmin('Failed to insert row '); }
@Edit,感谢bram brambring他的令牌解决方案,似乎比我更容易.现在像一个魅力一样工作,我希望他的方式通常会刷新令牌.谢谢老兄!