2017-12-27 68 views
-1

我正在使用我的私人项目的网站服务。用xml文件创建php请求

我有一个XML文件,像这样的:

<Request Originator="xxxxx" Company="xxx"> 

    <Range Code="xx"> 

     <Item Id="xxxxxx-xxxxx-xxxxxx-xxx-7E8B94462F2C" /> 

    </Range> 

    <KeyValues> 

     <Translations> 

      <Language Value="de" /> 

      <Language Value="en" /> 

     </Translations> 

     <Regions Show="true" /> 

     <Towns Show="true" /> 

    </KeyValues> 

</Request> 

编辑 所以对于我知道,直到今天:是一个我将请求发送到这个网址:http://interface.deskline.net/DSI/KeyValue.asmx?WSDL 与该服务器我进行通信,但始终得到此错误消息:

soap:ReceiverSystem.Web.Services.Protocols.SoapException: Server was unable to process request. ---> System.Xml.XmlException: Data at the root level is invalid. Line 1, position 1. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace() at System.Xml.XmlTextReaderImpl.ParseDocumentContent() at System.Web.Services.Protocols.SoapServerProtocol.SoapEnvelopeReader.Read() at System.Xml.XmlReader.MoveToContent() at System.Web.Services.Protocols.SoapServerProtocolHelper.GetRequestElement() at System.Web.Services.Protocols.Soap12ServerProtocolHelper.RouteRequest() at System.Web.Services.Protocols.SoapServerProtocol.Initialize() at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) --- End of inner exception stack trace ---1 

我不明白它的沟通。这里是一个小的PHP代码,我写的:

$url = 'http://interfacetest.deskline.net/DSI/KeyValue.asmx'; 


$content = 0; 
if (file_exists('/mm/request.xml')) { 
    $xml = fopen('/mm/request.xml', "r"); 
    $content = fread($xml,filesize("/mm/request.xml")); 
    fclose($xml); 
} else { 
    echo 'No file, no request'; 
} 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
// Following line is compulsary to add as it is: 
curl_setopt($ch, CURLOPT_POSTFIELDS, 
    "xmlRequest=" . $content); 
$response = curl_exec($ch); 
echo $response; 

回答

1

DOM文档的伟大工程操纵XML文档,例如:

$domd=new DOMDocument(); 
$domd->formatOutput=true; 
$domd->loadXML($str,LIBXML_NOBLANKS); 
$rangeele=$domd->getElementsByTagName("Range")->item(0); 
for($i=0;$i<5;++$i){ 
    $tmp=$domd->createElement("Item"); 
    $tmp->setAttribute("Id",$i); 
    $rangeele->appendChild($tmp); 
} 
var_dump($domd->saveXML()); 

输出

... 
    <Range Code="xx"> 
    <Item Id="xxxxxx-xxxxx-xxxxxx-xxx-7E8B94462F2C"/> 
    <Item Id="0"/> 
    <Item Id="1"/> 
    <Item Id="2"/> 
    <Item Id="3"/> 
    <Item Id="4"/> 
    </Range> 
... 

报价how can I set up the communication between my server and theirs with php - 即可以通过几种方式完成,具体取决于服务器支持的内容。最常用的方法是通过HTTP协议(其中的方式,是同样的协议Web浏览器主要使用与stackoverflow.com网站进行沟通),它可以像

$ch=curl_init('http://example.org/api'); 
curl_setopt_array($ch,array(CURLOPT_POST=>1,CURLOPT_HTTPHEADER=>array('Content-Type: application/xml'),CURLOPT_POSTFIELDS=>$xml)); 
curl_exec($ch); 
curl_close($ch); 

另一种流行进行沟通方法是使用TCP协议(其中HTTP协议是建立在顶部),可以像

$sock=socket_create(AF_INET,SOCK_STREAM,SOL_TCP); 
socket_connect($sock,"example.org",1337); 
socket_write($socket,$xml); 
socket_close($sock); 

有大量的其他协议也OFC(and curl supports a great deal of them)来完成,但是这些是最常见的。再次,它真的取决于目标服务器支持什么,我们在SO不知道,问问你想与之通信的人。 (ps,在上面的例子中,我省略了错误检查,另外,我投下并投票结束,因为你的问题太简单,缺乏细节,你甚至不能解释你期望使用的协议。 I am trying for 2 hours to get this working but no luck.那么,你有什么尝试?以及它是如何失败?)