10

私はcURLを使用してリクエストをSOAPサービスに送信しています.POST Bodyでパラメータを含むXMLを送信し、受け取った応答で:

Web サービス: http://lcbtestxmlv2.ivector.co.uk/soap/book.asmx?WSDL

<?xml version="1.0" encoding="UTF-8"?>
    <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>
          <SearchResponse xmlns="http://ivectorbookingxml/">
             <SearchResult>
                <ReturnStatus>
                   <Success>true</Success>
                   <Exception />
                </ReturnStatus>
                <SearchURL>http://www.lowcostholidays.fr/dl.aspx?p=0,8,5,0&amp;date=10/05/2013&amp;duration=15&amp;room1=2,1,0_5&amp;regionid=9</SearchURL>
                <PropertyResults>
                   <PropertyResult>
                      <TotalProperties>215</TotalProperties>
                      <PropertyID>1795</PropertyID>
                      <PropertyName>Hotel Gaddis</PropertyName>
                      <Rating>3.0</Rating>
                      <Country>Egypte</Country>
                      <Resort>Louxor</Resort>
                      <Strapline>Cet établissement confortable propose un très bon service à un bon rapport qualité-prix. Cet hôtel de 6 étages compte 55 chambres et comprend une terrasse, une réception avec coffre-fort et ascenseur,</Strapline>
                      <Description>Cet établissement confortable propose un très bon service à un bon rapport qualité-prix. Cet hôtel de 6 étages compte 55 chambres et comprend une terrasse, une réception avec coffre-fort et ascenseur,...</Description>
                      <CMSBaseURL>http://lcbtestxml1.ivector.co.uk/content/DataObjects/Property/Image/</CMSBaseURL>
                      <MainImage>image_1795_v1.jpg</MainImage>
                      <MainImageThumbnail>imagethumb_1795_v1.jpg</MainImageThumbnail>
                      <SearchURL>http://www.lowcostholidays.fr/dl.aspx?p=0,8,5,0&amp;date=10/05/2013&amp;duration=15&amp;room1=2,1,0_5&amp;regionid=9&amp;propertyid=1795</SearchURL>
                      <RoomTypes>
                         <RoomType>
                            <Seq>1</Seq>
                            <PropertyRoomTypeID>690039000</PropertyRoomTypeID>
                            <MealBasisID>3</MealBasisID>
                            <RoomType>Twin/double Room</RoomType>
                            <RoomView />
                            <MealBasis>Petit Déjeuner</MealBasis>
                            <NonRefundableRates>false</NonRefundableRates>
                            <SubTotal>150.58</SubTotal>
                            <Discount>0</Discount>
                            <Total>150.58</Total>
                            <Adults>2</Adults>
                            <Children>1</Children>
                            <Infants>0</Infants>
                            <Errata />
                         </RoomType>
                         <RoomType>
                            <Seq>1</Seq>
                            <PropertyRoomTypeID>690039001</PropertyRoomTypeID>
                            <MealBasisID>7</MealBasisID>
                            <RoomType>Twin/double Room</RoomType>
                            <RoomView />
                            <MealBasis>Demi-Pension</MealBasis>
                            <NonRefundableRates>false</NonRefundableRates>
                            <SubTotal>291.64</SubTotal>
                            <Discount>0</Discount>
                            <Total>291.64</Total>
                            <Adults>2</Adults>
                            <Children>1</Children>
                            <Infants>0</Infants>
                            <Errata />
                         </RoomType>
                         <RoomType>
                            <Seq>1</Seq>
                            <PropertyRoomTypeID>690039002</PropertyRoomTypeID>
                            <MealBasisID>5</MealBasisID>
                            <RoomType>Double/twin Room</RoomType>
                            <RoomView />
                            <MealBasis>Pension Complète</MealBasis>
                            <NonRefundableRates>false</NonRefundableRates>
                            <SubTotal>529.22</SubTotal>
                            <Discount>0</Discount>
                            <Total>529.22</Total>
                            <Adults>2</Adults>
                            <Children>1</Children>
                            <Infants>0</Infants>
                            <Errata />
                         </RoomType>
                      </RoomTypes>
                   </PropertyResult>
                </PropertyResults>
             </SearchResult>
          </SearchResponse>
       </soap:Body>
    </soap:Envelope>

XML データに関する十分な経験がありません。XML 応答を PHP オブジェクトまたは配列に変換しようと何時間も費やしましたが、成功しませんでした。

すべての PropertyResults を読み取る必要があります。

PHP コード:

$xml = simplexml_load_string($soap_xml_result);

$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$xml->registerXPathNamespace('xsd', 'http://www.w3.org/2001/XMLSchema');

$test = (string) $xml->Body->SearchResponse->SearchResult->SearchURL;
var_export($test);
4

4 に答える 4

10

bksi のヒントはそれほど間違っていませんが、技術的にはこれは XML であるため、名前空間を持つ要素に適切にアクセスするだけで済みます。これは、XPath 式を使用し、namspace-uri を独自のプレフィックスに登録することで、より簡単に機能します。

$soap = simplexml_load_string($soapXMLResult);
$soap->registerXPathNamespace('ns1', 'http://ivectorbookingxml/');
$test = (string) $soap->xpath('//ns1:SearchResponse/ns1:SearchResult/ns1:SearchURL[1]')[0];
var_dump($test);

出力:

string(100) "http://www.lowcostholidays.fr/dl.aspx?p=0,8,5,0&date=10/05/2013&duration=15&room1=2,1,0_5&regionid=9"

XPath を使用したくない場合は、トラバース中に名前空間を指定する必要があります。要素自体にプレフィックスが付いていない場合は、要素自体の名前空間内の子のみが直接使用できます。ルート要素にはプレフィックスが付けられているため、最初に応答までトラバースする必要があります。

$soap     = simplexml_load_string($soapXMLResult);
$response = $soap->children('http://schemas.xmlsoap.org/soap/envelope/')
                     ->Body->children()
                         ->SearchResponse
;

次に、$responseご存知のように変数を利用できます。

$test = (string) $response->SearchResult->SearchURL;

その要素には接頭辞が付いていないためです。より複雑な結果が返されるため、すべての応答値に簡単にアクセスできるため、おそらくこれが最適です。

あなたの質問は次のようになります。

そこにあるコード/説明も役立つかもしれません。

于 2013-04-09T08:29:06.797 に答える
2

うーん。そのためには、SOAP リクエストを送信するだけでなく、SOAP クライアントを使用する必要があります。PHP には SOAP 機能が統合されています http://php.net/manual/en/book.soap.php

NuSOAP http://sourceforge.net/projects/nusoap/のようなカスタム SOAP ライブラリがあります。

ほとんどの PHP フレームワークには SOAP ライブラリも含まれています。

于 2013-04-09T03:15:27.357 に答える