PHP SOAP - 如何将XML传输到Web服务?

问题描述:

我了解到SOAP是向wsdl Web服务发送信息的有用PHP库。我正在构建一个XML表格以发送到web服务。我的朋友制作了网络服务,他说这需要一串字符。我试图给他发送一个XML表单的字符串,当他复制粘贴XML时,这是正确的,但是当我尝试使用Web服务时,他所得到的只是一个空指针。PHP SOAP - 如何将XML传输到Web服务?

while($row=mysql_fetch_array($result)) 
    { 

     //Product Id is called ProductID in the XML 
     $product_id = $row['product_id']; 


     //Final price is called SalesPrice in the XML 
     $final_price = $row['final_price']; 
     echo $final_price.'<br>'; 
     //qty is called Quantity in the XML 
     $qty = $row['qty']; 
     echo $qty.'<br>'; 
     //Purchase cost is called PurchaseCost in the XML 
     $purchase_cost = $row['purchase_cost']; 
     echo $purchase_cost.'<br>'; 

     $xml_output .="<SalesOrderLine>"; 
     $xml_output.='<ProductID>'.$product_id.'</ProductID>'; 
     $xml_output.='<Quantity>'.$qty.'</Quantity>'; 
     $xml_output.='<SalesPrice>'.$final_price.'</SalesPrice>'; 
     $xml_output.='<PurchaseCost>'.$purchase_cost.'</PurchaseCost>'; 

     // Escaping illegal characters 

     $xml_output.='</SalesOrderLine>'; 
     //$amount = $amount + $final_price; 
     //$i++; 
    } 


    $xml_output .="</SalesOrder>"; 

    $xml = new SimpleXMLElement($xml_output); 
    $xml->asXML(); 
    echo $xml; 

    try { 
    $client = new SoapClient("http://*&#^%(@%(*#(#$%)%CreateDB?wsdl"); 
    $result = $client->addSalesOrder($xml); 

} catch (SoapFault $e) { 
    var_dump(libxml_get_last_error()); 
    var_dump($e); 
} 

    //$result = $client->addSalesOrder($xml_output); 
    if (is_soap_fault($result)) { 
    trigger_error("SOAP Fault: (faultcode: {$result->faultcode}, faultstring: {$result->faultstring})", E_USER_ERROR); 
} 

当我回显出整个$ xml_output字符串时,我可以通过检查代码来看到它成为一个XML表。无论是否使用SimpleXML库,将该函数传递给Web服务都不起作用。如何安全地将字符串传输到作为XML表的Web服务,同时将其保留为字符串?

+0

更多的细节你为什么要创建一个字符串,一个SimpleXMLElement,然后立刻把它放回一个字符串? ...我想你需要把它分配给一些变量来实际执行,例如$ strXML = $ xml-> asXML() – Onheiron 2012-07-30 13:17:11

无法将XML作为Soap函数的参数发送。如果你想操作XML作为输入,使用以下方式

// xml content 

$xmlDocument = '<Result> 

    <Product id="12345" language="fr-BE"> 

     <Data> 

      <Brand>BrandName</Brand> 

      <ProductName>Bag</ProductName> 

     </Data> 

    </Product> 

</Result>'; 

// initiate soap client 

ini_set("soap.wsdl_cache_enabled", "0"); 

$client = new SoapClient(
     "http://dotnetwebservice.com/Products.asmx?wsdl", 

     array(
      'trace' => 1, 
      'exceptions' => 1, 
      'soap_version' => SOAP_1_1, 
      'encoding' => 'ISO-8859-1', 
      'features' => SOAP_SINGLE_ELEMENT_ARRAYS 
     ) 
); 

$xmlvar = new SoapVar(

     "<ns1:xmlDocument>'.$xmlDocument.'</ns1:xmlDocument>", XSD_ANYXML 
); 

$params->xmlDocument = (object)$xmlvar; 

$save_result = $client->SaveProduct($params); 

请找到链接http://www.php.net/manual/en/soapvar.soapvar.php