I am bit frustrated at the lack of information about this online. Here is the issue:
I am in charge of creating a iOS application which sends sound data back and forth between the server and the app. The Audio is in small files and thus does not need to be streamed over, but rather it can be sent. Right now, I am using a TCP server I wrote to handle applications like this. However, I want to keep the system as simple as possible and writing your own server and client sockets can get a bit complex and leaves room for crashes. Overall it slows down development because I need to account for packet structure and other things.
My question is, can I write an ASPX or PHP web service that lets me pass the files back and forth through GET or POST?
1
Where are you searching for information and not finding it? Uploading a file to a PHP webpage is a topic that has been covered quite a few times on the web over the years. So much so that even w3schools has a example.
From the client side (HTML example but it is just a basic HTTP POST):
<form action="http://example/com/upload.php" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file" />
<input type="submit" name="submit" value="Submit" />
</form>
The server side of things in PHP:
<?php
if ($_FILES["file"]["error"] > 0) {
echo "Error: " . $_FILES["file"]["error"] . "<br />";
} else {
echo "Upload: " . $_FILES["file"]["name"] . "<br />";
echo "Type: " . $_FILES["file"]["type"] . "<br />";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
echo "Stored in: " . $_FILES["file"]["tmp_name"];
}
?>
As far as sending the files back to the client simply store the in a web assecible location and it is a basic GET request:
GET http://example.com/myfile.wav
Another choice would convert the file content to base64 and submit it as form field.
In PHP the code would:
<?php
$data = file_get_contents('path_to_file');
$encripData = base64_encode($data);
$filename = 'my_file.txt';
$fields = array(
'content' => $encripData,
'name' => $filename,
);
$fieldsQueryString = http_build_query($fields);
// you should have curl library install here
$ch = curl_init("http://myserver/receive.php");
curl_setopt($ch, CURLOPT_POST, count($fields));
curl_setopt($ch, CURLOPT_POSTFIELDS, $fieldsQueryString);
curl_exec($ch);
The script PHP to receive the file will be:
<?php
$content = $_POST['content'];
$name = $_POST['name'];
file_put_contents($name, base64_decode($content));
I not tested it code but is a good idea of the implement.
For the ASP side, sure you can write web services that will allow you to do what you’re suggesting. The latest and greatest of ASP.NET has some enhancements to help simplify the whole web services approach.
It’s not the most efficient route, but it will work for your requirements. This page from the ASP.NET site has a few tutorials that appear directly related to what you’re trying to do.
You’ll still need to have the client handle wrapping up the sound objects and then pushing them through the HTTP request.