[英]PHP - collect data from paginated API results (in XML)
我正在使用 PHP 从提供 XML 的 API 访问数据,其检索格式和 URL 如下所示:
$response = $oauth->get('https://example.com/Main/1');
在这种情况下,“ 1 ”是页码。 它将返回前 100 个结果。 (我已经全部工作了。)
但是如果有更多结果,我目前无法自动访问它们。 (我必须手动更改网址。)
返回的 XML 将列出有多少页的<Links><rel>last</rel><href>https://example.com/Main/3</href></Links>
。 (在这种情况下,有3页可用。)
以下是返回的 XML 示例:
<?xml version="1.0" encoding="UTF-8"?>
<Fleet xmlns="http://standards.iso.org/iso/15143/-3" version="2" snapshotTime="2020-01-13T20:12:55.224Z">
<Links>
<rel>self</rel>
<href>https://example.com/Main/1</href>
</Links>
<Links>
<rel>last</rel>
<href>https://example.com/Main/3</href>
</Links>
<Equipment>
<EquipmentHeader>
<OEMName>CAT</OEMName>
<Model>D6</Model>
<EquipmentID>1111111</EquipmentID>
<SerialNumber>1111111</SerialNumber>
<PIN>1111111</PIN>
</EquipmentHeader>
<CumulativeOperatingHours datetime="2018-07-29T18:15:30.000Z">
<Hour>1111</Hour>
</CumulativeOperatingHours>
</Equipment>
// ... and so on - 100 results...
</Fleet>
有没有一种简单的方法来检查最后一页的给定值,然后循环检索每一页(从第一页到最后一页)的数据?
(因为在返回第一个请求结果之前我不知道有多少页。)
更新我想出了这个来查找页数:
$total_pages = NULL;
$xml = simplexml_load_string($response);
// Get used name space, and use that
$namespaces = $xml->getDocNamespaces();
if(isset($namespaces[''])) {
$defaultNamespaceUrl = $namespaces[''];
$xml->registerXPathNamespace('default', $defaultNamespaceUrl);
$nsprefix = 'default:';
} else {$nsprefix = '';}
$nodes = $xml->xpath('//'.$nsprefix.'Links');
foreach($nodes as $node) {
if($node->rel == 'last'){
$last_page_url = $node->href;
$pos = strrpos($last_page_url, '/'); // position of last slash in url
$total_pages = $pos === false ? 0 : substr($last_page_url, $pos + 1); // if slash doesn't exist, then 0, otherwise the value after the last slash
} // end if
} // end foreach
echo $total_pages;
所以现在我需要弄清楚如何遍历请求......
首先,您可以使用DOMXPath简化最后一页的查找:
$domDocument = new \DOMDocument();
$domDocument->loadXML($response);
$xpath = new \DOMXPath($domDocument);
$xpath->registerNamespace('d', 'http://standards.iso.org/iso/15143/-3');
$lastPageHref = $xpath->evaluate('string(//d:Links/d:rel[text()="last"]/following-sibling::d:href)');
$lastPage = (int)basename($lastPageHref);
这将获得一个href
元素,它是文本内容为"Last"
的rel
元素的直接跟随同级元素,该元素本身是文档中任何位置的Links
元素的子元素。
然后使用basename
获取该 URL 的最后一部分,并将其转换为整数。
演示: https : //3v4l.org/urfU3
从那里,您可以简单地执行以下操作(其中OAuthClass
将被替换为$oauth
所属的类):
function fetchPage(YourOAuthClass $oauth, int $page): \DOMDocument
{
$xml = $oauth->get("https://example.com/Main/$page");
$domDocument = new \DOMDocument();
$domDocument->loadXML($xml);
return $domDocument;
}
$domDocument = fetchPage($oauth, 1);
// Here, do the code above to grab $lastPage
// Also do stuff with $domDocument (handle page 1)
for ($page = 2; $page <= $lastPage; $page++) {
$domDocument = fetchPage($oauth, $page);
// Do stuff with $domDocument (handle current page)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.