PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP解析SOAP响应问题大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

我创建了以下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>';  
}

解决方法:

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

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

在SearchProperties()调用之后和foreach之前添加代码.

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

此后,$values-> SearchPropertiesResult-> PropertyInfo现在是一个数组,而不管它只有一个或多个属性.这样您的foreach就可以了.

大佬总结

以上是大佬教程为你收集整理的PHP解析SOAP响应问题全部内容,希望文章能够帮你解决PHP解析SOAP响应问题所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: