用PHP解析SOAP XML feed

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用PHP解析SOAP XML feed相关的知识,希望对你有一定的参考价值。

我正试图从OCTranspo Feed获取停止数据,以包含在家中的仪表板上。 OCTranspo API允许您获取此数据,我已成功运行php

这是我得到的回复的副本:

<soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <soap:body>
    <getnexttripsforstopresponse xmlns="http://octranspo.com">
      <getnexttripsforstopresult>
        <stopno xmlns="http://tempuri.org/">9085</stopno>
        <stoplabel xmlns="http://tempuri.org/">FERNBANK BRIGHTSIDE</stoplabel>
        <error xmlns="http://tempuri.org/">
          <route xmlns="http://tempuri.org/">
            <routedirection>
              <routeno>61</routeno>
              <routelabel>Terry Fox</routelabel>
              <direction>Westbound</direction>
              <error>
                <requestprocessingtime>20171222231738</requestprocessingtime>
                <trips>
                  <trip>
                    <tripdestination>Stittsville</tripdestination>
                    <tripstarttime>21:57</tripstarttime>
                    <adjustedscheduletime>1</adjustedscheduletime>
                    <adjustmentage>0.82</adjustmentage>
                    <lasttripofschedule>false</lasttripofschedule>
                    <bustype>6LB - 60</bustype>
                    <latitude>45.248670</latitude>
                    <longitude>-75.912157</longitude>
                    <gpsspeed>48.9</gpsspeed>
                  </trip>
                  <trip>
                    <tripdestination>Stittsville</tripdestination>
                    <tripstarttime>22:27</tripstarttime>
                    <adjustedscheduletime>35</adjustedscheduletime>
                    <adjustmentage>0.47</adjustmentage>
                    <lasttripofschedule>false</lasttripofschedule>
                    <bustype>6EB - 60</bustype>
                    <latitude>45.343510</latitude>
                    <longitude>-75.820002</longitude>
                    <gpsspeed>74.1</gpsspeed>
                  </trip>
                  <trip>
                    <tripdestination>Stittsville</tripdestination>
                    <tripstarttime>22:57</tripstarttime>
                    <adjustedscheduletime>69</adjustedscheduletime>
                    <adjustmentage>0.88</adjustmentage>
                    <lasttripofschedule>false</lasttripofschedule>
                    <bustype>6EB - 60</bustype>
                    <latitude>45.419260</latitude>
                    <longitude>-75.656651</longitude>
                    <gpsspeed>40.0</gpsspeed>
                  </trip>
                </trips>
              </error>
            </routedirection>
          </route>
        </error>
      </getnexttripsforstopresult>
    </getnexttripsforstopresponse>
  </soap:body>
</soap:envelope>

我需要在PHP中读取这个响应,并输出'route label','route number'和'direction',以及每个列出的三个行程的'adjustedscheduletime'。

我可以加载响应很好,但我无法获取PHP页面来解析xml提要。

这是我到目前为止所尝试的:

<?php
$mySoapResponse=file_get_contents('https://api.octranspo1.com/v1.2/GetNextTripsForStop?appID={ID}&apiKey={key}&routeNo=61&stopNo=9085');

echo $mySoapResponse;
?>
  <br>
  <hr>
  <br>
  <?php
// Loads the XML
$soap = simplexml_load_string($mySoapResponse);
$soap->registerXPathNamespace('ns1', 'http://raspberrypi.local/');

// Grabs the trips
$trips = $soap->xpath('/ns1:getnexttripsforstopresponse/ns1:getnexttripsforstopresult/ns1:error/ns1:route/ns1:routedirection/ns1:error/ns1:trips');
$label = $soap->xpath('/ns1:getnexttripsforstopresponse/ns1:getnexttripsforstopresult/ns1:stoplabel');
echo $label[0];
//$trips = $xml->children('soap', true)->Body->getnexttripsforstopresponse->getnexttripsforstopresult->error->route->routedirection->error->trips->trip;

// Take the posts and generate some markup
foreach ($trips as $trip)
{

        echo '
                <p>' . $trip->tripstarttime . '</p>
                <p>' . $trip->adjustedscheduletime . '</p>
                <p>' . $trip->bustype . '</p>
        ';
}

?>
答案

好的,我现在知道了。基本上我只是将SOAP处理放在一起并将其转换为XML。点击这里的帖子:PHP - XML Response into Array获取有关PHP解析和瞧的XML解析的一些技巧。

这是我的最终代码:

<html>

<head>
  <link rel="stylesheet" type="text/css" href="stylish.css">
</head>

<body>
  <?php
$mySoapResponse=file_get_contents('https://api.octranspo1.com/v1.2/GetNextTripsForStop?appID={AppID}&apiKey={KEY}$
$clean_xml = str_ireplace(['SOAP-ENV:', 'SOAP:','soap:'], '', $mySoapResponse);
$xml = simplexml_load_string($clean_xml);

//print_r($xml);


// Grabs the trips
$arrTrips = $xml->Body->GetNextTripsForStopResponse->GetNextTripsForStopResult->Route->RouteDirection->Trips->Trip;
//print_r($arrTrips);

//Grabs the route info
$routeInfo=$xml->Body->GetNextTripsForStopResponse->GetNextTripsForStopResult->Route->RouteDirection;

echo '<H1>Route ' . $routeInfo->RouteNo . ' ' . $routeInfo->RouteLabel . ' ' . $routeInfo->Direction . '</H1>';
echo '<h2>Next Trips</h2>';
// Take the posts and generate some markup
$i=1;
foreach ($arrTrips as $trip)
{

        echo '<p> ' . $i . '. Arrives in ' . $trip->AdjustedScheduleTime . ' minutes at ' . $trip->TripStartTime . '</p>';
        $i=$i+1;
}

?>
</body>

</html>

以上是关于用PHP解析SOAP XML feed的主要内容,如果未能解决你的问题,请参考以下文章

如何在php中解析soap xml响应并从字符串中获取信息

php解析带有命名空间的xml

java怎么解析文本文件中的soap报文

SOAP的请求xml不包含PHP 7上的对象数组

在PHP中使用文字xml的SOAP

python中的Atom feed(XML)解析[重复]