如何创建SOAP请求 - PHP

时间:2018-02-26 14:48:52

标签: php soap soap-client

我的客户使用使用SOAP作为服务的提供商,我对此一无所知。我已经阅读了文档,SoapClient等等。

我怎样才能让它发挥作用?

示例请求

POST /itravel/API/WebService/iTravelAPI_3_0.asmx HTTP/1.1
Host: divingtravel.itravelsoftware.com
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Header>
    <AuthHeader xmlns="http://tempuri.org/">
      <Username>string</Username>
      <Password>string</Password>
    </AuthHeader>
  </soap12:Header>
  <soap12:Body>
    <GetRegions xmlns="http://tempuri.org/">
      <getRegionsParameters>
        <CountryID>int</CountryID>
        <ObjectTypeID>unsignedByte</ObjectTypeID>
        <ObjectTypeGroupID>unsignedByte</ObjectTypeGroupID>
        <CategoryID>int</CategoryID>
        <LanguageID>string</LanguageID>
        <SeasonID>int</SeasonID>
      </getRegionsParameters>
    </GetRegions>
  </soap12:Body>
</soap12:Envelope>

示例回复

HTTP/1.1 200 OK
Content-Type: application/soap+xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <GetRegionsResponse xmlns="http://tempuri.org/">
      <GetRegionsResult>
        <Region>
          <CountryID>int</CountryID>
          <RegionID>int</RegionID>
          <RegionName>string</RegionName>
          <RegionNameTranslationList>
            <Translation xsi:nil="true" />
            <Translation xsi:nil="true" />
          </RegionNameTranslationList>
          <Description>string</Description>
          <DescriptionTranslationList>
            <Translation xsi:nil="true" />
            <Translation xsi:nil="true" />
          </DescriptionTranslationList>
          <RegionCode>string</RegionCode>
          <CountryCode>string</CountryCode>
          <PhotoList>
            <Photo xsi:nil="true" />
            <Photo xsi:nil="true" />
          </PhotoList>
          <ShortDescription>string</ShortDescription>
          <ShortDescriptionTranslationList>
            <Translation xsi:nil="true" />
            <Translation xsi:nil="true" />
          </ShortDescriptionTranslationList>
          <Title>string</Title>
          <TitleTranslationList>
            <Translation xsi:nil="true" />
            <Translation xsi:nil="true" />
          </TitleTranslationList>
          <SEODescription>string</SEODescription>
          <SEODescriptionTranslationList>
            <Translation xsi:nil="true" />
            <Translation xsi:nil="true" />
          </SEODescriptionTranslationList>
          <KeyWords>string</KeyWords>
          <KeyWordsTranslationList>
            <Translation xsi:nil="true" />
            <Translation xsi:nil="true" />
          </KeyWordsTranslationList>
        </Region>
      </GetRegionsResult>
    </GetRegionsResponse>
  </soap12:Body>
</soap12:Envelope>

目前我一直在尝试关注this link,但它并没有写出任何内容。

我当前的代码,尝试按照上面的链接:

<?php
$client = new SoapClient("http://divingtravel.itravelsoftware.com/itravel/API/WebService/iTravelAPI_3_0.asmx", array('soap_version' => SOAP_1_2));
$result = $client('GetCategories');

var_dump($result);
?>

提前致谢!

编辑:更新了链接并提供了当前代码。

1 个答案:

答案 0 :(得分:1)

您需要将请求创建为对象。你也可以用数组来做。

$getRegionsRequest = new \stdClass();
$getRegionsRequest->getRegionsParameters = new \stdClass();
$getRegionsRequest->getRegionsParameters->CountryID = 123;
$getRegionsRequest->getRegionsParameters->ObjectTypeID= 123;
$getRegionsRequest->getRegionsParameters->ObjectTypeGroupID = 123;
$getRegionsRequest->getRegionsParameters->CategoryID = 123;
$getRegionsRequest->getRegionsParameters->LanguageID = 'something';
$getRegionsRequest->getRegionsParameters->SeasonID = 123;

$client = new SoapClient("http://divingtravel.itravelsoftware.com/itravel/API/WebService/iTravelAPI_3_0.asmx", array('soap_version' => SOAP_1_2));
$getRegionsResponse = $client->GetRegions($getRegionsRequest);

var_dump($getRegionsResponse);