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--
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:
'data' => ...
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.