我正在尝试构建一个xml文件数组。 每个获取请求给我100个产品,我需要获得大约800个产品。
所以我尝试构建一个基于变量ts_d的循环,你可以使用它来获取xml的下一页。
public function getXml($division, $topic, $tsp)
{
$array = array();
$i = 0;
$x = 1;
while ($x = 1) {
$headers = ['Authorization' => 'Bearer '.$this->getAccessToken()];
$client = new Client([
'base_uri' => 'https://start.exactonline.nl/docs/',
]);
try {
$response = $client->request('GET', 'XMLDownload.aspx', [
'query' => ['Topic' => $topic, '_Division_' => $division, 'TSPaging' => $tsp],
'headers' => $headers,
]);
$string = new \SimpleXMLElement((string) $response->getBody());
$tspaging = $string->Topics->Topic->attributes()->{'ts_d'};
$array[$i]=$string;
echo $tspaging . ' ' . $tsp;
}
catch (\Exception $e) {}
if (!isset($tspaging)) {
$x = 0;
}
$i++;
$tsp = $tspaging
unset($string);
}
return $array;
}
我用以下方法调用此函数:
$stockPositions = $connection->getXml(1310477, 'StockPositions', '');
但是这个while循环是无限的,echo返回:
0x000000000F5753AB 0x000000000F5753AB 0x000000000F5753AB0x000000000F5753AB 0x000000000F5753AB0x000000000F5753AB 0x000000000F5753AB0x000000000F5753AB 0x000000000F5753AB0x000000000F5753AB
你们能帮助我们摆脱这种无限循环吗?
答案 0 :(得分:0)
问题是$ tspaging返回一个simpleXmlObject字符串而不是普通字符串。解决方案是在该元素上添加strval:
$tspaging = strval($string->Topics->Topic->attributes()->{'ts_d'});