繁体   English   中英

使用PHP文件附加到特定位置

[英]Append at a specific position using PHP files

我试图在<?xml version="1.0" encoding="UTF-8"?>之后的<?xml-stylesheet type='text/xsl' href='soap.xslt'?>之后添加到xml文件,这实际上是SOAP响应,但是它只是将<?xml version="1.0" encoding="UTF-8"?>之后的数据替换为样式表的链接。
这是来自client.php的代码部分,它将soap响应写入文件并追加我想要的部分。

<?php

if(isset($_POST['search_input']))
{
try
{
    $input = $_POST['search_input'];

    $wsdl = "http://localhost/WebService/UDDI/90210Store.wsdl";

    //$options = array('cache_wsdl'=>WSDL_CACHE_NONE, 'features'=>SOAP_SINGLE_ELEMENT_ARRAYS);

    //$client = new SoapClient($wsdl, $options);

    $debugOption = array('trace'=>true, 'cache_wsdl'=>WSDL_CACHE_NONE, 'features'=>SOAP_SINGLE_ELEMENT_ARRAYS);
    $client = new SoapClient($wsdl, $debugOption);

    $response = $client->viewDressPerPrice($input);


    $soaprequest = "<strong>REQUEST:</strong><br/>" . htmlspecialchars($client->__getLastRequest()) . "<br/>";
    $soapresponse = htmlspecialchars($client->__getLastResponse());

    echo $soapresponse;

    $file = 'soap.xml';

    file_put_contents($file, $soapresponse);

    $file2 = fopen($file, "r+");
    fseek($file2, 64, SEEK_SET); //maybe the offset is not correct
    fwrite($file2, "<?xml-stylesheet type='text/xsl' href='soap.xslt'?>");
    fclose($file2);

    //rest of the code
?>

这是xml文件的内容:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type='text/xsl' href='soap.xslt'>
schemas.xmlsoap.org/soap/envelope/" //where it is not appending correctly
xmlns:ns1="http://www.shehzad.edu/webservice">
<SOAP-ENV:Body>
  <ns1:Result>
     <ns1:DressPerPrice>
        <ns1:Name>Dress 2</ns1:Name>
        <ns1:Price>20</ns1:Price>
        <ns1:Image>2.jpeg</ns1:Image>
     </ns1:DressPerPrice>
     <ns1:DressPerPrice>
        <ns1:Name>Dress 9</ns1:Name>
        <ns1:Price>20</ns1:Price>
        <ns1:Image>3.jpeg</ns1:Image>
     </ns1:DressPerPrice>
     <ns1:DressPerPrice>
        <ns1:Name>Dress 10</ns1:Name>
        <ns1:Price>20</ns1:Price>
        <ns1:Image>0905C58A0179_1.jpeg</ns1:Image>
     </ns1:DressPerPrice>
     <ns1:DressPerPrice>
        <ns1:Name>Dress 11</ns1:Name>
        <ns1:Price>20</ns1:Price>
        <ns1:Image>0905C58A0179_1.jpeg</ns1:Image>
     </ns1:DressPerPrice>
     <ns1:DressPerPrice>
        <ns1:Name>Dress 12</ns1:Name>
        <ns1:Price>20</ns1:Price>
        <ns1:Image>0905C58A0179_1.jpeg</ns1:Image>
     </ns1:DressPerPrice>
  </ns1:Result>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

好像是在剪切并添加它,而不是正确地附加它。 任何帮助将不胜感激。 谢谢。

使用DOMDocument操作XML文档,例如

$doc = new DOMDocument();
$doc->load('file.xml');
$doc->insertBefore($doc->createProcessingInstruction('xml-stylesheet', "type='text/xsl' href='soap.xslt'"), $doc->documentElement);
$doc->save('file.xml');

暂无
暂无

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

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