2011-10-18 86 views
0

當我連接到web服務時,我目前遇到錯誤。這是我的PHP腳本:使用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 = "[email protected]"; 
$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>的內容。

感謝您的任何幫助。

回答

0
$simpleresult = $result->$methodResultName; 

檢查此行實際上是生產的東西,而實際上$methodResultName$result對象存在。如果它不存在,PHP將簡單地將一個null分配給$simpleresult,然後您嘗試將其饋送到DOM中,其中$simpleresult->any也將出現在null之間。

+0

我是一個非常PHP的新手。您能否告訴我如何實施您的解決方案? – mjohnston

+0

掌握一些調試語句。 var_dump($ result),var_dump($ simpleresult)等... –

+0

做一些調試我發現在饋入DOM的地方''simpleresult-> any'' simpleresult的值是: ' stdClass Object([ReturnCode] =>成功[YourOrderNumber] => [Lines] => stdClass Object([Line] => stdClass Object([ProductCode] => 25402 [ProductQuantity] => 1 [Status] => Confirmed)) [DeliveryCode] => ZZRML_NDSD)' – mjohnston