B
B
Big_person2015-11-27 13:03:22
PHP
Big_person, 2015-11-27 13:03:22

How to get an array of objects from SOAP Response from Russian Post using php?

Russian Post provides an opportunity to receive information about the postal item through the API.

$request = '<?xml version="1.0" encoding="UTF-8"?>
                <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:oper="http://russianpost.org/operationhistory" xmlns:data="http://russianpost.org/operationhistory/data" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
                <soap:Header/>
                <soap:Body>
                   <oper:getOperationHistory>
                      <data:OperationHistoryRequest>
                         <data:Barcode>?</data:Barcode>  
                         <data:MessageType>0</data:MessageType>
                         <data:Language>RUS</data:Language>
                      </data:OperationHistoryRequest>
                      <data:AuthorizationHeader soapenv:mustUnderstand="1">
                         <data:login>mylogin</data:login>
                         <data:password>mypassword</data:password>
                      </data:AuthorizationHeader>
                   </oper:getOperationHistory>
                </soap:Body>
             </soap:Envelope>';

$client = new SoapClient("https://tracking.russianpost.ru/rtm34?wsdl",  array('trace' => 1, 'soap_version' => SOAP_1_2));

echo '<textarea>'.$client->__doRequest($request, "https://tracking.russianpost.ru/rtm34", "getOperationHistory", SOAP_1_2).'</textarea>';

How to get an array of objects from Response using php?
Answer example:
<S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope">
   <S:Body>
      <ns7:getOperationHistoryResponse xmlns:ns2="http://russianpost.org/sms-info/data" xmlns:ns3="http://russianpost.org/operationhistory/data" xmlns:ns4="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns5="http://www.russianpost.org/custom-duty-info/data" xmlns:ns6="http://www.russianpost.org/RTM/DataExchangeESPP/Data" xmlns:ns7="http://russianpost.org/operationhistory">
         <ns3:OperationHistoryData>
            <ns3:historyRecord>
               <ns3:AddressParameters>
                  <ns3:DestinationAddress>
                     <ns3:Index>663300</ns3:Index>
                     <ns3:Description>Норильск Почтамт</ns3:Description>
                  </ns3:DestinationAddress>
                  <ns3:OperationAddress>
                     <ns3:Index>111555</ns3:Index>
                     <ns3:Description>Москва 555</ns3:Description>
                  </ns3:OperationAddress>
                  <ns3:MailDirect>
                     <ns3:Id>643</ns3:Id>
                     <ns3:Code2A>RU</ns3:Code2A>
                     <ns3:Code3A>RUS</ns3:Code3A>
                     <ns3:NameRU>Российская Федерация</ns3:NameRU>
                     <ns3:NameEN>Russian Federation</ns3:NameEN>
                  </ns3:MailDirect>
                  <ns3:CountryOper>
                     <ns3:Id>643</ns3:Id>
                     <ns3:Code2A>RU</ns3:Code2A>
                     <ns3:Code3A>RUS</ns3:Code3A>
                     <ns3:NameRU>Российская Федерация</ns3:NameRU>
                     <ns3:NameEN>Russian Federation</ns3:NameEN>
                  </ns3:CountryOper>
               </ns3:AddressParameters>
               <ns3:FinanceParameters>
                  <ns3:Payment>2500000</ns3:Payment>
                  <ns3:Value>2500000</ns3:Value>
                  <ns3:MassRate>152000</ns3:MassRate>
                  <ns3:InsrRate>25000</ns3:InsrRate>
                  <ns3:AirRate>0</ns3:AirRate>
                  <ns3:Rate>0</ns3:Rate>
               </ns3:FinanceParameters>
               <ns3:ItemParameters>
                  <ns3:Barcode>EA123456789RU</ns3:Barcode>
                  <ns3:ValidRuType>true</ns3:ValidRuType>
                  <ns3:ValidEnType>false</ns3:ValidEnType>
                  <ns3:MailRank>
                     <ns3:Id>0</ns3:Id>
                     <ns3:Name>Без разряда</ns3:Name>
                  </ns3:MailRank>
                  <ns3:PostMark>
                     <ns3:Id>0</ns3:Id>
                     <ns3:Name>Без отметки</ns3:Name>
                  </ns3:PostMark>
                  <ns3:MailType>
                     <ns3:Id>7</ns3:Id>
                     <ns3:Name>Отправление EMS</ns3:Name>
                  </ns3:MailType>
                  <ns3:MailCtg>
                     <ns3:Id>4</ns3:Id>
                     <ns3:Name>С объявленной ценностью и наложенным платежом</ns3:Name>
                  </ns3:MailCtg>
                  <ns3:Mass>2281</ns3:Mass>
               </ns3:ItemParameters>
               <ns3:OperationParameters>
                  <ns3:OperType>
                     <ns3:Id>1</ns3:Id>
                     <ns3:Name>Прием</ns3:Name>
                  </ns3:OperType>
                  <ns3:OperAttr>
                     <ns3:Id>1</ns3:Id>
                     <ns3:Name>Единичный</ns3:Name>
                  </ns3:OperAttr>
                  <ns3:OperDate>2015-07-09T18:08:00.000+03:00</ns3:OperDate>
               </ns3:OperationParameters>
               <ns3:UserParameters>
                  <ns3:SendCtg>
                     <ns3:Id>1</ns3:Id>
                     <ns3:Name>Население</ns3:Name>
                  </ns3:SendCtg>
                  <ns3:Sndr>ИВАНОВ И Н</ns3:Sndr>
                  <ns3:Rcpn>ПЕТРОВ Н И</ns3:Rcpn>
               </ns3:UserParameters>
            </ns3:historyRecord>
             <ns3:historyRecord>
             …
             </ns3:historyRecord>
             …
         </ns3:OperationHistoryData>
      </ns7:getOperationHistoryResponse>
   </S:Body>
</S:Envelope>

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
alexnikitis, 2015-11-30
@Big_person

function soap_parser($response)
{
  $res = array();
  $xml = simplexml_load_string($soap);
  
  $xml->registerXPathNamespace('nspost', 'http://russianpost.org/operationhistory/data');
  foreach ($xml->xpath('//nspost:OperationHistoryData/nspost:historyRecord') as $item)
  {
    $item->registerXPathNamespace('nspost', 'http://russianpost.org/operationhistory/data');
    $index_x = $item->xpath('.//nspost:OperationAddress/nspost:Index');
    $address_x = $item->xpath('.//nspost:OperationAddress/nspost:Description');
    $date_x = $item->xpath('.//nspost:OperationParameters/nspost:OperDate');
    $type_id_x = $item->xpath('.//nspost:OperationParameters/nspost:OperType/nspost:Id');
    $type_x = $item->xpath('.//nspost:OperationParameters/nspost:OperType/nspost:Name');
    $attr_id_x = $item->xpath('.//nspost:OperationParameters/nspost:OperAttr/nspost:Id');
    $attr_x = $item->xpath('.//nspost:OperationParameters/nspost:OperAttr/nspost:Name');
    $res[] = array(
      'index' => (int)$index_x[0],
      'address' => (string)$address_x[0],
      'type_id' => (int)$type_id_x[0],
      'type' => (string)$type_x[0],
      'attr_id' => (int)$attr_id_x[0],
      'attr' => (string)$attr_x[0],
      'datetime' => date("Y-m-d H:i:s", strtotime((string)$date_x[0])),
      'date' => date("Y-m-d", strtotime((string)$date_x[0])),
      'time' => date("H:i:s", strtotime((string)$date_x[0])),
      );
  }
  return $res;
}

D
Dmitry Skorik, 2015-11-27
@Omutik

For example using SoapClient

A
Anton Shamanov, 2015-11-27
@SilenceOfWinter

If there is no support for this extension, then you can try php libraries, for example, pear.php.net/package/SOAP

D
d_voronoff, 2017-01-17
@d_voronoff

$client = new SoapClient( $wsdl );
    try {
        $request = $client->getOperationHistory( array( "OperationHistoryRequest" => array( "Barcode" => ?, "MessageType" => 0, "Language" => "RUS" ), "AuthorizationHeader" => array( "login" => $login, "password" => $pass ) ) );
    } catch ( Exception $e ) {
        return json_encode( array( "result" => false, "reason" => $e->getMessage() ) );
    }

Can do
$arr = json_decode( json_encode( $request ), true );

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question