【发布时间】:2021-11-24 12:33:02
【问题描述】:
当我尝试从 Google 云端硬盘获取电子表格的所有标题时,我收到了这条消息 The string could not be parsed as XML:
ERROR -> SimpleXMLElement::__construct(): Entity: line 1: parser error : Start tag expected, '<' not found in C:\....
ERROR-> SimpleXMLElement::__construct(): { in C:\....
ERROR-> SimpleXMLElement::__construct(): ^ in C:\...
我尝试在谷歌驱动器上连接,我成功了,但我需要在谷歌驱动器上获取电子表格的名称,我以前使用 API v3 现在我需要使用 V4。
这是调用方法getSpreadsheets()的代码;
$serviceRequest = new DefaultServiceRequest($token->access_token, $token->token_type);
ServiceRequestFactory::setInstance($serviceRequest);
$spreadsheetService = new Google\Spreadsheet\SpreadsheetService();
$spreadsheetFeed = $spreadsheetService->getSpreadsheets();
这是getSpreadsheets方法的代码:
public function getSpreadsheets()
{
return new SpreadsheetFeed(
ServiceRequestFactory::getInstance()->get('v4/spreadsheets/1u7WYzJOYMX7uH3AIM70yVLOaHBy8p_uifuJe_Saa2T4?fields=sheets.properties.title')
);
}
这是 SpreadsheetFeed 类,错误原因在哪里:
namespace Google\Spreadsheet;
use ArrayIterator;
use SimpleXMLElement;
/**
* Spreadsheet feed.
*
* @package Google
* @subpackage Spreadsheet
* @author Asim Liaquat <asimlqt22@gmail.com>
*/
class SpreadsheetFeed extends ArrayIterator
{
/**
* The spreadsheet feed xml object
*
* @var \SimpleXMLElement
*/
protected $xml;
/**
* Initializes the the spreadsheet feed object
*
* @param string $xml the raw xml string of a spreadsheet feed
*/
public function __construct($xml)
{
$this->xml = new SimpleXMLElement($xml);
$spreadsheets = array();
foreach ($this->xml->entry as $entry) {
$spreadsheets[] = new Spreadsheet($entry);
}
parent::__construct($spreadsheets);
}
/**
* Gets a spreadhseet from the feed by its title. i.e. the name of
* the spreadsheet in google drive. This method will return only the
* first spreadsheet found with the specified title.
*
* @param string $title
*
* @return \Google\Spreadsheet\Spreadsheet|null
*/
public function getByTitle($title)
{
foreach($this->xml->entry as $entry) {
if($entry->title->__toString() == $title) {
return new Spreadsheet($entry);
}
}
return null;
}
}
我试图找到一个解决方案如何传递这个错误! 任何帮助,如何解决这个错误? 谢谢!
【问题讨论】:
-
你能指定
DefaultServiceRequest类的来源吗?如果您正在使用这个library,似乎是not adapted 与Sheets API v4 一起使用。作为建议,您应该使用 Google 提供的 libraries 重写您的代码。 -
您好埃梅尔,感谢您的反馈!是的,我正在使用那个库,你有一个如何做到这一点的例子吗?你有适应 API v4 的库吗?许多tnx
-
嗨,Emel,我正在研究适应库,我做了一些更改,但现在我遇到了上面评论中描述的错误! DefaultServiceRequest 类没问题,我没有从他们那里得到错误,我调整了他们!
-
如果您尝试将库移植到 Sheets API v4,最好的去处是库 repository。如果您尝试执行其他操作,例如获取所有电子表格标题,您可以使用此answer 作为指导。
-
正如我在另一条评论中指出的那样,最好的办法是使用Google 提供的库重写您的代码。