2017-12-27 390 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.那麼,你有什麼嘗試?以及它是如何失敗?)