Php 如何将XML传输到web服务?
我了解到SOAP是一个有用的PHP库,用于向wsdl web服务发送信息。我正在构建一个要发送到Web服务的XML表。我的朋友制作了这个web服务,他说它需要一个字符串。我试图向他发送一个XML表字符串,当他复制粘贴XML时,这是正确的,但是当我尝试使用web服务时,他得到的只是一个空指针Php 如何将XML传输到web服务?,php,xml,web-services,soap,Php,Xml,Web Services,Soap,我了解到SOAP是一个有用的PHP库,用于向wsdl web服务发送信息。我正在构建一个要发送到Web服务的XML表。我的朋友制作了这个web服务,他说它需要一个字符串。我试图向他发送一个XML表字符串,当他复制粘贴XML时,这是正确的,但是当我尝试使用web服务时,他得到的只是一个空指针 while($row=mysql_fetch_array($result)) { //Product Id is called ProductID in the XML
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);
}
while($row=mysql\u fetch\u array($result))
{
//产品Id在XML中称为ProductID
$product_id=$row['product_id'];
//最终价格在XML中称为SalesPrice
$final_price=$row['final_price'];
回声$final_price.“
”;
//qty在XML中称为Quantity
$qty=$row['qty'];
回显$qty.“
”;
//购买成本在XML中称为PurchaseCost
$purchase_cost=$row['purchase_cost'];
echo$purchase_cost.“
”;
$xml_输出=“”;
$xml_输出。=''.$product_id';
$xml_输出=''.$qty';
$xml_输出。=''.$final_价格';
$xml_输出。=''.$purchase_成本';
//转义非法字符
$xml_输出=“”;
//$amount=$amount+$final_价格;
//$i++;
}
$xml_输出=“”;
$xml=新的simplexmlement($xml\u输出);
$xml->asXML();
echo$xml;
试试{
$client=newsoapclient(“http://*^%(@%(*#(#$%)%CreateDB?wsdl”);
$result=$client->addSalesOrder($xml);
}捕获(SoapFault$e){
var_dump(libxml_get_last_error());
var_dump($e);
}
//$result=$client->addSalesOrder($xml\u输出);
如果(是故障($result)){
触发器错误(“SOAP错误:(错误代码:{$result->faultcode},错误字符串:{$result->faultstring})”,E\u用户错误;
}
当我回显整个$xml_输出字符串时,通过检查代码,我可以看到它变成了一个xml表。无论是否使用SimpleXML库,将函数传递给web服务都不起作用。如何将字符串安全地传输到作为xml表的web服务,同时将其保留为字符串?您不能将xml作为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);
//xml内容
$xmlDocument='1
品牌名称
纸袋
';
//启动soap客户端
ini_集(“soap.wsdl_缓存已启用”,“0”);
$client=新的SoapClient(
"http://dotnetwebservice.com/Products.asmx?wsdl",
排列(
“跟踪”=>1,
“例外情况”=>1,
“soap_版本”=>soap_1_1,
'编码'=>'ISO-8859-1',
'功能'=>SOAP\u单元素\u数组
)
);
$xmlvar=新的SoapVar(
“.$xmlDocument.”,XSD_ANYXML
);
$params->xmlDocument=(对象)$xmlvar;
$save_result=$client->SaveProduct($params);
请在link上查找更多详细信息为什么要从字符串创建SimpleXMLElement,然后立即将其转换回字符串?…此外,我认为您需要将其分配给某个变量以实际执行此操作,如$strXML=$xml->asXML()$xml\u output.='';结束,但起始标记在哪里?我的意思是$xml\u output=“”;