【问题标题】:How to transfer XML to a web service?如何将 XML 传输到 Web 服务?
【发布时间】:2012-07-28 04:01:17
【问题描述】:

我了解到 SOAP 是一个有用的 PHP 库,用于向 wsdl Web 服务发送信息。我正在构建要发送到 Web 服务的 XML 表。我的朋友做了网络服务,他说它需要一串字符。我正在尝试向他发送一个 XML 表的字符串,当他复制粘贴 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 服务,同时将其保留为字符串?

【问题讨论】:

  • 你为什么要从一个字符串创建一个 SimpleXMLElement 然后立即将它转回一个字符串? ...我也认为您需要将其分配给某个变量才能真正做到这一点,例如 $strXML = $xml->asXML()
  • $xml_output .="";结束,但起始标签在哪里?我的意思是 $xml_output ="";

标签: php xml web-services soap


【解决方案1】:

您不能将 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-19
    • 1970-01-01
    相关资源
    最近更新 更多