UPS API が返す XML を正しく読み取る方法を何日も探していました。私は最終的に、パッケージから送信する料金を請願する方法を見つけました。そして今、応答を含む XML を取得しました。
私は XML にあまり詳しくありませんが、簡単な例で XML がどのように機能するかを理解できます。
XML 応答は次のとおりです。
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<rate:RateResponse xmlns:rate="http://www.ups.com/XMLSchema/XOLTWS/Rate/v1.1">
<common:Response xmlns:common="http://www.ups.com/XMLSchema/XOLTWS/Common/v1.0">
<common:ResponseStatus>
<common:Code>1</common:Code>
<common:Description>Success</common:Description>
</common:ResponseStatus>
<common:Alert>
<common:Code>110971</common:Code>
<common:Description>Your invoice may vary from the displayed reference rates</common:Description>
</common:Alert>
<common:TransactionReference/>
</common:Response>
<rate:RatedShipment>
<rate:Service>
<rate:Code>11</rate:Code>
<rate:Description/>
</rate:Service>
<rate:RatedShipmentAlert>
<rate:Code>110971</rate:Code>
<rate:Description>Your invoice may vary from the displayed reference rates</rate:Description>
</rate:RatedShipmentAlert>
<rate:BillingWeight>
<rate:UnitOfMeasurement>
<rate:Code>KGS</rate:Code>
<rate:Description>Kilograms</rate:Description>
</rate:UnitOfMeasurement>
<rate:Weight>3.0</rate:Weight>
</rate:BillingWeight>
<rate:TransportationCharges>
<rate:CurrencyCode>EUR</rate:CurrencyCode>
<rate:MonetaryValue>21.85</rate:MonetaryValue>
</rate:TransportationCharges>
<rate:ServiceOptionsCharges>
<rate:CurrencyCode>EUR</rate:CurrencyCode>
<rate:MonetaryValue>1.40</rate:MonetaryValue>
</rate:ServiceOptionsCharges>
<rate:TotalCharges>
<rate:CurrencyCode>EUR</rate:CurrencyCode>
<rate:MonetaryValue>23.25</rate:MonetaryValue>
</rate:TotalCharges>
<rate:RatedPackage>
<rate:Weight>1.0</rate:Weight>
</rate:RatedPackage>
<rate:RatedPackage>
<rate:Weight>2.0</rate:Weight>
</rate:RatedPackage>
</rate:RatedShipment>
</rate:RateResponse>
</soapenv:Body>
</soapenv:Envelope>
値を取得する例を試してみたところsimplexml_load_file();
、タグから値を取得できました (非常に単純な例)。しかし、それを試してみると、オブジェクトタイプではないというエラーが表示されるため、何も取得できません。
誰かがその XML の読み方を知っていて、その方法を教えてくれたらとてもありがたいです。
これを読んでくれてありがとう!
PS:簡単な例を試したとき、これを試してうまくいきました:
$school = simplexml_load_file('XOLTResult.xml'); //where is the xml
echo $school->students->student[0]; //finding the first student nam
これは適切に機能しましたが、たとえば、Response->RatedShipment[0]->Service->Code; を取得しようとすると、*/最初のものを取得するには/* エラーが表示されます。