【问题标题】:Issue with PHP parssing SOAP responsePHP 解析 SOAP 响应的问题
【发布时间】:2013-07-08 12:50:43
【问题描述】:

我创建了以下 PHP 脚本来显示我们使用的 SOAP API 的属性列表。

当我们有多个属性被宣传时,脚本可以正常工作,但当我们只有一个属性被宣传时,脚本不会显示任何内容。

谁能告诉我我做错了什么或简单的检查可以解决问题?

我的代码是:

$wsdl = "http://portal.letmc.com/PropertySearchService.asmx?WSDL";

$client = new SoapClient($wsdl, array ("trace"=>1, "exceptions"=>0));

$strClientID = "{xxxx-xxxx-xxxx-xxxx}";
$strBranchID = "{xxxx-xxxx-xxxx-xxxx}";                         
$nMaxResults = "5";
$nRentMinimum = 100;
$nRentMaximum = 900;
$nMaximumTenants = 5;                           

$parameters = array(    "strClientID"=>$strClientID, 
                    "strBranchID"=>$strBranchID, 
                    "nMaxResults"=>$nMaxResults,
                    "nRentMinimum"=>$nRentMinimum,
                    "nRentMaximum"=>$nRentMaximum,
                    "nMaximumTenants"=>$nMaximumTenants
                );                          

 $values = $client->SearchProperties($parameters);


if($values != '')
{
echo "<table>";
        echo '<tr>
                <th>Apartment</th>
                <th class="center">Bedrooms</th>
                <th>Rent</th>
                <th>Description</th>
            </tr>';

    foreach ($values->SearchPropertiesResult->PropertyInfo as $message)
    {
        $address = $message->Address1;
        $rooms = $message->MaxTenants;
        $rent = $message->Rent;
        $description = $message->Description;

        echo '<tr>';        
        echo '<td>'. $address .'</td>
                  <td class="center">'. $rooms .'</td>
              <td>'. $rent .'</td>
              <td>'. $description .'</td>';
        echo '</tr>';

    }
    echo '</table>'; 
}

else 
{
echo '<p><strong>Sorry, we have no properties available.</strong></p> <p>Please register your details on the right and we will let you know as soon as an apartment comes available.</p>';  
}

【问题讨论】:

    标签: php soap wsdl


    【解决方案1】:

    您可以将 PHP SoapClient 配置为不将单个元素数组转换为元素本身。使用选项参数中的“功能”键并将其设置为SOAP_SINGLE_ELEMENT_ARRAYS,如下所示:

    $options = array('features' => SOAP_SINGLE_ELEMENT_ARRAYS);
    $client = new SoapClient("wsdl", $options);
    

    这样您就不必检查单个元素或数组,而可以简单地假设存在一个数组。

    【讨论】:

      【解决方案2】:

      这在 .NET Web 服务上很常见。如果有多个结果,则它是一个数组,但如果只有一个结果,而不是一个具有一个结果的数组,您将在PropertyInfo 中获得结果本身。

      解决方案是测试它是否是一个数组,如果不是,则将对象移动到一个数组中,以便您可以以相同的方式使用它,用于单个结果和结果数组。

      在您的 SearchProperties() 调用之后和 foreach 之前添加此代码。

      if(!is_array($values->SearchPropertiesResult->PropertyInfo))
      {
          $values->SearchPropertiesResult->PropertyInfo = array($values->SearchPropertiesResult->PropertyInfo);
      }
      

      在此之后,$values-&gt;SearchPropertiesResult-&gt;PropertyInfo 现在是一个数组,无论它只有一个属性还是多个属性。所以你的 foreach 会起作用。

      【讨论】:

      • 辛苦了 - 谢谢!
      猜你喜欢
      • 2012-09-10
      • 2016-09-03
      • 2014-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多