使用SOAP时出现PHP错误

时间:2011-10-18 20:04:36

标签: php web-services soap

我在连接到网络服务时遇到错误。这是我的PHP脚本:

<?php

//initialise test SOAP client
$wdsl = myaddress;
$soapClient = new SoapClient("$wdsl");
$apiKey = myapikey;
$clientID = myclientid;

//assign customer information that would normally be pulled from database
$strVendorTxCode = "thepartyshack-0000000001-0000000001";
$strEmail = "cutomer@email.com";
$strPhone = "07901888408";
$strDelivery = "ZZRML_NDSD";

//Build the XML string that will be sent to Smiffys
$strXML = $strXML . "<?xml version='1.0' encoding='Windows-1252'?><Order>";
//Customer Reference Number.
$strXML = $strXML . "<YourOrderNumber>" . $strVendorTXCode . "</YourOrderNumber>";
//More XML That does not change
$strXML = $strXML . "<Recipient>";
//Add in customer email
$strXML = $strXML . "<Email>" . $strEmail . "</Email>";
//Add in our customer telepone number
$strXML = $strXML . "<Telephone>" . $strPhone . "</Telephone>";
//Add in customer name
$strXML = $strXML . "<Recipient>" . "Mrs Customer" . "</Recipient>";
//Add in customer address line
$strXML = $strXML . "<AddressLine>" . "1 High Street" . "</AddressLine>";
//Add in customer city
$strXML = $strXML . "<City>" . "Anyplace" . "</City>";
//Add in customer postcode
$strXML = $strXML . "<PostCode>" . "AA1 1AA" . "</PostCode>";
//Add in customer county
$strXML = $strXML . "<County>" . "Anywhere" ."</County>";
//Add in customer country
$strXML = $strXML . "<CountryCode>" . "GB" . "</CountryCode>";
//Another line not to be changed
$strXML = $strXML . "</Recipient>";
//Add delivery
$strXML = $strXML . "<DeliveryCode>" . $strDelivery . "</DeliveryCode>";
//Another line not to be changed
$strXML = $strXML . "<Lines>";
//Add in the basket in the form of 'line'
$strXML = $strXML . "<Line><ProductCode>25402</ProductCode><ProductQuantity>1</ProductQuantity></Line>";
//Close all remaing tags XML done.
$strXML = $strXML . "</Lines></Order>";

//Make the SOAP request and get the response
$stockParameters = array('apiKey'=>$apiKey,'clientID'=>$clientID,'orderXml'=>$strXML);
$SubmitOrder=GetXml($soapClient,'SubmitOrder',$stockParameters,'Order');
$ResultSets = array( array('name' =>'SubmitOrder','ResultSet' => $SubmitOrder) );
$myresults = array();
foreach($ResultSets as $result){ printTableRecords($result,10); };

function GetXml($soapClient,$method,$parameters,$resultSetTag){
    try {
        $methodResultName = $method."Result";
        $result = $soapClient->$method($parameters);
        $simpleresult = $result->$methodResultName;
    return getElementsFromResult($resultSetTag,$simpleresult);
    }catch (SoapFault $exception) {
        echo $exception;
    }
}
function getElementsFromResult($elementName,$simpleresult){
    $dom = new DOMDocument;
    $dom->preserveWhiteSpace = FALSE;
    if($simpleresult==null) {
        echo 'null';
        return null;
    }else{
        $dom->loadXML($simpleresult->any);
        return $dom->getElementsByTagName($elementName);
    }
}
function printTableRecords($xmlNodeList,$count) {
    foreach ($xmlNodeList['ResultSet'] as $node) {
        $myvar = 0;
        foreach($node->childNodes as $childNode){
            $entity = htmlentities($childNode->nodeValue);
            $myresults[$myvar] = $entity;
            $myvar ++;
        }
    }
    echo $myresults[1];
}
?>

我在网站的其他区域成功使用了几乎相同的SOAP请求,但在提出上述请求时,我收到此错误:

Warning: DOMDocument::loadXML() [domdocument.loadxml]: Empty string supplied as input in /home/content/89/8236789/html/test.php on line 72

我要求的回复应该是:

<OrderResultBase>
    <ReturnCode>Successful</ReturnCode>
    <YourOrderNumber>thepartyshack-0000000001-0000000001</YourOrderNumber>
    <Lines>
        <Line>
            <ProductCode>25402</ProductCode>
            <ProductQuantity>1</ProductQuantity>
            <Status>Confirmed</Status>
        </Line>
    </Lines>
    <DeliveryCode>ZZRML_NDSD</DeliveryCode>
</OrderResultBase>

我需要检索的行是<ReturnCode>的内容。

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

    $simpleresult = $result->$methodResultName;

检查此行是否实际产生了某些内容,$methodResultName对象中实际存在$result。如果它不存在,PHP只会将null分配给$simpleresult,然后您尝试使用$simpleresult->any将其输入DOM,这也会出现null