Forum Moderators: coopster

Message Too Old, No Replies

I need to send some data from a Windows application to a site

         

Monitor

11:48 pm on Nov 15, 2008 (gmt 0)

10+ Year Member



I need to send binary data from a Windows application to a site. I am used with compiled programming but I don't know much PHP.

Is it possible for a PHP script running on a Linux server to receive a package (binary string) of data of random size and store it to disk? Can anybody post a pseudo code (mock-up) PHP program for this so I can use it as starting point?

Thanks a lot.

rob7591

12:00 am on Nov 16, 2008 (gmt 0)

10+ Year Member



I'm not sure how the fastest way is exactly, but my idea would be to save the binary data to a file and upload the file using the _FILE_ request.

Not really sure exactly how this would be done, and I don't have any time to look now, but try searching Google for posting binary data

Monitor

12:03 am on Nov 16, 2008 (gmt 0)

10+ Year Member



Hi Rob.
This upload is done with 'POST'?

---------

PS: the Internet is full of examples like:

<form action="file.php" method="POST">
Your Name:
<input type="text" name="user_name_field" id="user_name_field">
<br />
<input type="submit" value="Enter My Name in DB">
</form>

The problem is that somebody has to push the SUBMIT button after the data is filled in. Can this be done automatically? I mean without the SUBMIT button?

[edited by: Monitor at 12:06 am (utc) on Nov. 16, 2008]

cameraman

12:48 am on Nov 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you're accustomed to compiled programming, I would think the easier way to do it would be to write a program that can FTP the data to the Linux server.
To do it via HTTP POST you just have to send the right headers - no one has to actually "click" a submit button; the server just has to see all of the content that would get sent if someone had clicked a submit button.

cameraman

12:51 am on Nov 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's good examples of handling file uploads here:
Handling file uploads [us.php.net]

Monitor

1:05 am on Nov 16, 2008 (gmt 0)

10+ Year Member



"write a program that can FTP the data to the Linux server"

This will raise way too many problems like Windows firewall & co.
This is why I choose POST. Seems tho the very "light" and won't disturb the user with firewall configuration, setting/forwarding ports, etc.

Thanks for the "Handling file uploads" link.
Seems to bee what I need.

vincevincevince

4:16 am on Nov 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The easiest way is to base64_encode the data and send it as a GET:

From Windows, just make a call to:
http://www.example.com/submit.php?data=#*$!#*$!#*$!

(Where #*$!#*$!X is a base64 encoded binary string)

In PHP:

<?php
if ($_GET['data'])
{
if ($data=base64_decode($_GET['data'])
file_put_contents("saved_data",$data);
}
?>

Be careful with this; by default an Apache server will only accept 8190 bytes of data in this way. The LimitRequestLine directive allows you to increase this if required.

The way around the length limit is to use POST, you can handle the POST data in much the same way as the GET data in my example above. It becomes more complex from your Windows application.

From Windows, you will need to open a socket and write the POST data directly to the server. What language will you use there?

Monitor

10:32 am on Nov 16, 2008 (gmt 0)

10+ Year Member



Hi vincevincevince.

I am using Delphi. I can already GET and POST messages with Delphi. This is no problem.

I want to send just a very small package. If it is possible, I want to send this data package without having my program blocked by the anti-virus/firewall or without forcing the user to to enter any Internet settings (IP, port,etc).
In this perspective, which method is more "lighter"? GET or POST ?

Thanks

vincevincevince

9:43 pm on Nov 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



GET would be 'lighter'; but there is very little in it. I do hope this is a legitimate application you are building!

Monitor

11:09 pm on Nov 16, 2008 (gmt 0)

10+ Year Member



Don't worry. I just want to make the program to download a temporary license from my server - some kind of trial limitation based on time. This is why I want to send a very small package of data (about 80 bytes of real data + 200 bytes of free space for possible future stuff I may want to add).

Monitor

1:43 am on Nov 17, 2008 (gmt 0)

10+ Year Member



I am advancing.
However, I need to debug the POST package I send from my windows app. I think it is not properly formated because I get this:

[16-Nov-2008 17:22:23] PHP Warning: Missing boundary in multipart/form-data POST data in Unknown on line 0

Where/how can I see the HTTP POST request that my app sends?

Monitor

11:18 am on Nov 17, 2008 (gmt 0)

10+ Year Member



Somebody suggested this code to see the POST but is working only partially:

if($_POST){
$str = '';
foreach($_POST as $P=>$p){
$str .= "$P => $p\r\n";
}
$str .= "====================\r\n";
$f = fopen('post.txt', 'a+');
fwrite($f, $str);
fclose($f);
print('POST request saved to disk as post.txt<br>');
}
else { print('No POST received!'); }

Monitor

11:58 pm on Nov 20, 2008 (gmt 0)

10+ Year Member



Problem solved. Thank you very much for help.