简体   繁体   English

将xml数据添加到http请求

[英]add xml data to http request

I am new in php programming and I would like to add data to my http request in xml format. 我是php编程的新手,我想以xml格式向我的http请求添加数据。 I use curl to generate HTTP request using this PHP code : 我使用curl使用此PHP代码生成HTTP请求:

<?php
      $fields = array(            'SERIAL_NUMBER ' => urlencode('123456'),
                            'HARDWARE_ID ' => urlencode('455FFG'),
                            'CODE ' => urlencode('99'),
                            //field goes here 
                );

      foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
      rtrim($fields_string, '&');

        $ch = curl_init("http://192.168.1.110:10000");
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);                       
        $output = curl_exec($ch);
        curl_close($ch);
        echo $output;   

?>

I would like to add fields array [ SERIAL_NUMBER and HARDWARE_ID and CODE ] as data in http request in format xml 我想将字段数组[SERIAL_NUMBER和HARDWARE_ID和CODE]添加为xml格式的http请求中的数据

the format of the desired http request is : 所需的http请求的格式为:

POST / HTTP/1.1
Host: 192.168.1.110:10000
Accept: */*
Content-Length: 52
Content-Type: application/x-www-form-urlencoded

HTTP/1.1 100 Continue //server responce

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <Device Parameters>
     <SERIAL_NUMBER>123456</SERIAL_NUMBER>
     <HARDWARE_ID>455FFG</HARDWARE_ID>
     <CODE>99</CODE>
    </Device Parameters>

Note : my server listen to port 10000 @ 192.168.1.110 注意:我的服务器监听端口10000 @ 192.168.1.110

any help is appreciated 任何帮助表示赞赏

You can use SimpleXMLElement. 您可以使用SimpleXMLElement。 Represents an element in an XML document. 表示XML文档中的元素。

http://php.net/manual/en/class.simplexmlelement.php http://php.net/manual/zh/class.simplexmlelement.php

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

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