首页 > 解决方案 > 无法向 PHPGraphLib 的远程 API 添加和呈现多集数据

问题描述

我正在为库使用远程 API。

我的数据结构

$graph = new \stdclass;
$graph->width = 500;
$graph->height = 350;
$data1 = array('gross'=>23, 'taxed'=>45, 'discount'=>20, 'revenue'=>32, 'due'=>14);
$data2 = array('gross'=>15, 'taxed'=>23, 'discount'=>23,'revenue'=>12, 'due'=>17);
$data3 = array('gross'=>43, 'taxed'=>23, 'discount'=>34, 'revenue'=>16, 'due'=>20);
$graph->data = array($data1,$data2,$data3);
$graph->setBarColor = array('blue','green','yellow');
$graph->setLegend = 'true';
$graph->setLegendTitle = array('Today','MTD','YTD');
$graph->setTitle = 'Revenue Stats';
$graph->setTitleLocation = 'left';
//JSON encode graph object
$encoded = urlencode(json_encode($graph));
//retrieve XML
$target = 'http://www.ebrueggeman.com/phpgraphlib/api/?g=' . $encoded . '&type=xml';`

错误

(2/2) Exception
String could not be parsed as XML
(1/2) ErrorException
SimpleXMLElement::__construct():
Start tag expected, '<' not found

使用单个数据集可以正常工作,但我无法加载多个数据集。

有什么建议么?

标签: phplaravelphpgraphlib

解决方案


不,API 不支持它,它坏了.. 对URL的简单检查会产生一些无效的 XML。

$this->data[0],$this->data[1],$this->data[2]<br><br>$graph->addData($this->data[0],$this->data[1],$this->data[2]);<br><?xml version="1.0"?>
<phpgrahphlib>
    <imageName>230_0046831d980a56de52331b09b7eb7efc.png</imageName>
    <imageLocation>http://www.ebrueggeman.com/sites/www.ebrueggeman.com/files/phpgraphlib_api/230_0046831d980a56de52331b09b7eb7efc.png</imageLocation>
    <imageTag><![CDATA[<img width="500" height="350" src="http://www.ebrueggeman.com/sites/www.ebrueggeman.com/files/phpgraphlib_api/230_0046831d980a56de52331b09b7eb7efc.png" />]]></imageTag>
    <error></error>
    <version>2.30</version>
</phpgrahphlib>

但确实会生成图表:

在此处输入图像描述

因此,您可以尝试在<?使用simplexml_load_string之前删除所有内容。可能想报告一个错误,以便修复它。

<?php
$xml = file_get_contents($target);

$target = strstr($xml, '<?');

$xml_object =  simplexml_load_string($target);

echo 'Image Tag: '. $xml_object->imageTag.PHP_EOL;
echo 'Errors: '. $xml_object->error.PHP_EOL;

https://3v4l.org/abHqm


推荐阅读