简体   繁体   English

使用SOAP时发生PHP错误

[英]PHP error when using SOAP

I'm currently experiencing an error when connecting to a webservice. 我目前在连接到Web服务时遇到错误。 Here is my PHP script: 这是我的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];
}
?>

I use a near identical SOAP request successfully in other areas of the site however when making the above request I receive this error: 我在网站的其他区域成功使用了几乎相同的SOAP请求,但是在发出上述请求时,我收到此错误:

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

The response I am requesting should be: 我要求的回复应该是:

<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>

The line I need to retrieve is the contents of <ReturnCode> . 我需要检索的行是<ReturnCode>的内容。

Thanks for any help. 谢谢你的帮助。

    $simpleresult = $result->$methodResultName;

Check that this line is actually producing something, and that $methodResultName actually exists in your $result object. 检查此行是否确实产生了某些东西,以及$methodResultName确实存在于$result对象中。 if it doesn't exist, PHP will simply assign a null to $simpleresult , which you then try to feed into DOM with $simpleresult->any , which would also come out null . 如果不存在,PHP会简单地为$simpleresult分配一个null ,然后您尝试使用$simpleresult->any将其馈入DOM,这也会出现null

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM