【问题标题】:creating php request with xml file使用 xml 文件创建 php 请求
【发布时间】:2017-12-27 14:46:11
【问题描述】:

我正在为我的私人项目使用来自网站的服务。

我有一个这样的 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;

【问题讨论】:

  • 你找到解决办法了吗,我也有同样的问题

标签: php xml curl concrete5


【解决方案1】:

DOMDocument 非常适合处理 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>
...

quote 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),但这些是最常见的。再说一次,这真的取决于目标服务器支持什么,我们不知道,问你想与之交流的人。

(ps,在上面的示例中,我省略了错误检查。另外,我投反对票并投票结束,因为你的问题太宽泛,缺乏细节。你甚至没有解释你希望使用什么协议。引用@ 987654327@嗯,你试过什么?怎么失败了?)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-16
    相关资源
    最近更新 更多