对于你们中的一些人来说这可能很常见,但我不熟悉使用PHP的XML Data Parsing!
情况
我的客户公司(电子商务)与物流公司有合作关系 它自己的API用于跟踪货件/包裹。现在我的客户想要 自动跟踪他们为订单发货的所有包裹 使用API与物流提供的凭证集成 公司。
我们提供了访问Live API的链接,Live API以XML格式响应数据。我已经解决了以下类型的XML响应/数据 -
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<ShipmentData>
<Shipment>
<Origin>New Delhi (110001)</Origin>
<Status>
<Status>Pending</Status>
<StatusLocation>Ahmedabad(Gujarat)</StatusLocation>
<StatusDateTime>2017-06-13T09:53:28.063000</StatusDateTime>
<RecievedBy/>
<Instructions>Reattempt - As per NDR instructions</Instructions>
<StatusType>UD</StatusType>
</Status>
</Shipment>
</ShipmentData>
为了我的方便&amp;理解我已将上述结果标记为命名 XML字段,因为每一行都是单独命名的
但是从这个新的物流公司收到的结果是不同的,因为它有 属性 ,如名称,类型,它们是每一行。
XML响应如下 -
This XML file does not appear to have any style information associated with it. The document tree is shown below.
<bd-objects version="1.0">
<object pk="1" model="awb">
<field type="BigIntegerField" name="awb_number">120420921</field>
<field type="CharField" name="orderid">123456</field>
<field type="FloatField" name="actual_weight">0.5</field>
<field type="CharField" name="origin">DELHI - DLU</field>
<field type="CharField" name="destination">BANGALORE - BAN</field>
<field type="CharField" name="current_location_name">DELHI - DEP</field>
<field type="CharField" name="current_location_code">DEP</field>
<field type="CharField" name="customer">ABC TRADING COMPANY</field>
<field type="CharField" name="consignee">John Doe</field>
<field type="CharField" name="pickupdate">13-Jun-2017</field>
<field type="CharField" name="status">Bagging completed</field>
<field type="CharField" name="tracking_status">Connected</field>
<field type="CharField" name="reason_code"/>
<field type="CharField" name="reason_code_description"/>
<field type="CharField" name="reason_code_number">001</field>
<field type="CharField" name="receiver"/>
<field type="CharField" name="expected_date">20-Jun-2017</field>
<field type="CharField" name="last_update_date">18-Jun-2017</field>
<field type="CharField" name="delivery_date"/>
<field type="CharField" name="ref_awb">None</field>
<field type="CharField" name="rts_shipment">0</field>
<field type="CharField" name="system_delivery_update"/>
<field type="CharField" name="rts_system_delivery_status"/>
<field type="CharField" name="rts_reason_code_number"/>
<field type="CharField" name="rts_last_update"/>
<field type="CharField" name="pincode">560001</field>
<field type="CharField" name="city">BANGALORE</field>
<field type="CharField" name="state">Karnataka</field>
<field name="scans">...</field>
</object>
</bd-objects>
现在我希望以下每个命名的字段(如 awb_number,orderid,origin,consignee )存储在不同的变量中,例如 $ awb_number,$ orderid, $ origin,$ consignee ,以便我可以将其用于我的进一步处理代码,该代码根据服务器的请求更新货件的状态。
对于命名的字段XML响应,我使用了以下代码 -
$url= "https://track.bd.com/api/packages/xml/?token=xxxxxxxxxxxxxxxx&format=xml&output=xml&waybill="."$awb";
$dh = curl_init();
curl_setopt($dh, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($dh, CURLOPT_URL, $url); //Get the URL Contents
$data = curl_exec ($dh); // Execute Curl Request
$xml = simplexml_load_string($data);
foreach ($xml -> Shipment as $row) {
$status_type = $row -> StatusType;
$PickUpDate = $row -> PickUpDate;
$Destination = $row -> Destination;
$PickDate = date("Y-m-d H:i:s",strtotime($PickUpDate));
$PickedDate = date("d M Y",strtotime($PickUpDate));
$ReferenceNo = $row -> ReferenceNo;
$DispatchCount = $row -> DispatchCount;
$OrderType = $row -> OrderType;
}
foreach ($xml -> Shipment->Status as $row) {
$status = $row -> Status;
$status_type = $row -> StatusType;
$Instructions = $row -> Instructions;
$status_time = $row -> StatusDateTime;
$statustime = date("j M Y",strtotime($status_time));
}
foreach ($xml -> Shipment->Consignee as $row) {
$Name = $row -> Name;
$PinCode = $row -> PinCode;
$Telephone1 = $row -> Telephone1;
$Address1 = $row -> Address1;
$ReverseInTransit = $row -> ReverseInTransit;
$ReturnedDate = $row -> ReturnedDate;
}
curl_close($dh);
上面的代码就像魅力一样,因为字段命名为但是在第二个结果集中我无法弄清楚应该使用哪些代码来存储结果值XML到PHP变量。
答案 0 :(得分:0)
DOMDocument救援!
<?php
declare(strict_types=1);
$domd=@DOMDocument::loadHTML(getXML()); // PS,. if its strict xml, you should probably replace loadHTML with loadXML
foreach($domd->getElementsByTagName("object")->item(0)->getElementsByTagName("field") as $field){
$name=$field->getAttribute("name");
$type=$field->getAttribute("type");
$text=$field->textContent;
echo 'name: '.$name.PHP_EOL;
echo 'type: '.$type.PHP_EOL;
echo 'text: '.$text.PHP_EOL;
}
function getXML():string{
$xml=<<<'XML'
<bd-objects version="1.0">
<object pk="1" model="awb">
<field type="BigIntegerField" name="awb_number">120420921</field>
<field type="CharField" name="orderid">123456</field>
<field type="FloatField" name="actual_weight">0.5</field>
<field type="CharField" name="origin">DELHI - DLU</field>
<field type="CharField" name="destination">BANGALORE - BAN</field>
<field type="CharField" name="current_location_name">DELHI - DEP</field>
<field type="CharField" name="current_location_code">DEP</field>
<field type="CharField" name="customer">ABC TRADING COMPANY</field>
<field type="CharField" name="consignee">John Doe</field>
<field type="CharField" name="pickupdate">13-Jun-2017</field>
<field type="CharField" name="status">Bagging completed</field>
<field type="CharField" name="tracking_status">Connected</field>
<field type="CharField" name="reason_code"/>
<field type="CharField" name="reason_code_description"/>
<field type="CharField" name="reason_code_number">001</field>
<field type="CharField" name="receiver"/>
<field type="CharField" name="expected_date">20-Jun-2017</field>
<field type="CharField" name="last_update_date">18-Jun-2017</field>
<field type="CharField" name="delivery_date"/>
<field type="CharField" name="ref_awb">None</field>
<field type="CharField" name="rts_shipment">0</field>
<field type="CharField" name="system_delivery_update"/>
<field type="CharField" name="rts_system_delivery_status"/>
<field type="CharField" name="rts_reason_code_number"/>
<field type="CharField" name="rts_last_update"/>
<field type="CharField" name="pincode">560001</field>
<field type="CharField" name="city">BANGALORE</field>
<field type="CharField" name="state">Karnataka</field>
<field name="scans">...</field>
</object>
</bd-objects>
XML;
return $xml;
}
输出:
name: awb_number
type: BigIntegerField
text: 120420921
name: orderid
type: CharField
text: 123456
name: actual_weight
type: FloatField
text: 0.5
name: origin
type: CharField
text: DELHI - DLU
name: destination
type: CharField
text: BANGALORE - BAN
name: current_location_name
type: CharField
text: DELHI - DEP
name: current_location_code
type: CharField
text: DEP
name: customer
type: CharField
text: ABC TRADING COMPANY
name: consignee
type: CharField
text: John Doe
name: pickupdate
type: CharField
text: 13-Jun-2017
name: status
type: CharField
text: Bagging completed
name: tracking_status
type: CharField
text: Connected
name: reason_code
type: CharField
text:
name: reason_code_description
type: CharField
text:
name: reason_code_number
type: CharField
text: 001
name: receiver
type: CharField
text:
name: expected_date
type: CharField
text: 20-Jun-2017
name: last_update_date
type: CharField
text: 18-Jun-2017
name: delivery_date
type: CharField
text:
name: ref_awb
type: CharField
text: None
name: rts_shipment
type: CharField
text: 0
name: system_delivery_update
type: CharField
text:
name: rts_system_delivery_status
type: CharField
text:
name: rts_reason_code_number
type: CharField
text:
name: rts_last_update
type: CharField
text:
name: pincode
type: CharField
text: 560001
name: city
type: CharField
text: BANGALORE
name: state
type: CharField
text: Karnataka
name: scans
type:
text: ...