dtuzjzs3853 2019-08-02 21:28
浏览 93

将xml文件数据发布到网址

I have a very basic HTML form. When you hit submit it creates an xml file with the data from the form. What I would like to do next is take the data from the xml file that was created and post it to a server. What I have kind of does that, but there is no data with the post.

I'm still fairly new to programing and everything I've learned thus far has come from this site, books and mostly trial and error. Any help would be appreciated!

<?php

$ch = curl_init();

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

curl_setopt($ch, CURLOPT_URL, 'http://192.168.4.4:51080/test/notify');

$postData = array(
    'data' => '@/Users/home/Desktop/test.xml',
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

$response = curl_exec($ch);

?>

This is what I receive on the server...

--------------------------d3712de4c2d492ec
Content-Disposition: form-data; name="data"

@/Users/home/Desktop/b.xml
--------------------------d3712de4c2d492ec--
  • 写回答

1条回答 默认 最新

  • dongzaliang4492 2019-08-02 22:46
    关注

    Welcome to Stack Overflow, and to programming!

    In point of fact, there is data in the POST, and that data is exactly what you are POSTing. Change the value between either one or both sets of 'single quotes' in this line to see what I mean:

    $postData = array(
        'data' => '@/Users/home/Desktop/test.xml',
    );
    

    In other words, your POST data consists of a single element, whose key is "data" and whose value is literally "@/Users/home/Desktop/test.xml". The output from the server that you show is showing you exactly this.

    What I believe you want is for the contents of that XML file to be what is POSTed. You have a few choices; here are some main ones:

    1. POST just the raw contents of the XML file.
    2. POST a single-element POST body like you have, but use a string whose value is the file contents on the right-hand side of 'data' => ...
    3. skip over creating the XML file entirely, and instead populate a structured POST body with the data elements that currently are used to create the XML file

    For #1, you need to POST differently; search for "php send raw post data" or similar. PHP's curl_* functions should help you with this option.

    For #1 and #2, you have to read the contents of the XML file. PHP has facilities for reading files, and they are well-documented. Search for "PHP file reading" or similar.

    (For programming learning in general, with many searches you can tack on "example code" and turn up blog posts and other guides where people have written small working examples to illustrate specific programming tasks.)

    Unless you have a specific reason to be exchanging information via XML, I recommend #3. In order to use the data on the server side, you are going to need to unpack it from inside the XML, so why not just take XML out of it, and transmit the data in a structured POST body? Again, maybe having the XML is important for one of your goals, but recognize that XML is just one choice for organizing data, and there are other choices.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大