2017-12-27 20 views
-1

개인 프로젝트를 위해 웹 사이트의 서비스를 사용하고 있습니다. 그래서 내가 오늘까지 알고 무엇을XML 파일로 PHP 요청을 생성합니다.

<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> 

편집 :

나는이 같은 XML 파일이 난이 URL에 대한 요청을 보내 하나입니다 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 

나는 의사 소통을 얻지 못한다.

$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

DOMDocument를 예를 들어, XML 문서를 조작하기위한 좋은 작품 : 여기에 내가 쓴 PHP 코드의 조금

$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 - 그 서버가 지원하는 것에 따라 여러 가지 방법으로 수행 할 수 있습니다. 가장 일반적인 방법은

$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); 

또 다른 인기있는 같이 수행 할 수 있습니다, (그런데, 웹 브라우저는 주로 stackoverflow.com 웹 사이트와 통신 할 때 사용하는 프로토콜이다) HTTP 프로토콜을 통해 통신하는 것입니다 방법은 너무 다른 프로토콜의 많음, OFC (and curl supports a great deal of them)가

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

같이 수행 할 수 있습니다 (http 프로토콜을 기반으로 구축되어있는) TCP 프로토콜을 사용하는 것입니다, 그러나 이들은 가장입니다 일반적인 것들. 다시 말하지만, 실제로 목표 서버가 지원하는 것에 달려 있습니다. 그래서 우리는 모릅니다. 의사 소통하려는 사람들에게 물어보십시오.

(위의 예에서 오류 검사가 생략되었습니다. 또한 질문이 너무 광범위하고 세부적인 내용이 없기 때문에 downvoted하고 닫으려는 경향이 있습니다. 어떤 프로토콜을 사용할지 예상 설명이 필요하지 않습니다. I am trying for 2 hours to get this working but no luck. 음, 무엇을 시도 했습니까? 어떻게 실패 했습니까?)