How to "Post" data to the web server ?

Post your questions about Web Server usage and Apache + PHP + MySQL/SQLite web applications.
Locked
User avatar
marc.assin
Getting the hang of things
Posts: 54
Joined: Wed Mar 02, 2011 4:15 pm
Location: Midi-Pyrénées, France
Contact:

How to "Post" data to the web server ?

Post by marc.assin »

Hi,

I'm integrating a Cisco IP camera on the NAS.
So far, so good, I can see the stream.

The camera allows for motion detection, when such an event happens, some actions can take place.
So far I've got email and ftp working.
I'm mainly interested in the "http notification" but I'm unable to make it work. I understand the cam uses a http_post method to send the event data to the web server, but there it stops.
Any help, please ?
Are there any pre-requisites ?
Any modifs to php.ini or apache.conf ?

Thank you
TS-419U+, 4x Seagate Barracuda ST 31000528AS, 1 TB, RAID 5, FW 4.1.4, Apache, PHP, KNX eibd
Greetings from Midi-Pyrénées, France
TonyPh12345
Been there, done that
Posts: 738
Joined: Tue Jul 13, 2010 11:53 pm

Re: How to "Post" data to the web server ?

Post by TonyPh12345 »

Not sure what you're asking.

Basically, if it's using a POST, then you need to have a CGI on the server side to catch the post and interpret the data that's POSTed.
User avatar
marc.assin
Getting the hang of things
Posts: 54
Joined: Wed Mar 02, 2011 4:15 pm
Location: Midi-Pyrénées, France
Contact:

Re: How to "Post" data to the web server ?

Post by marc.assin »

Hi Tony,

Thank you for taking the time to reply.
TonyPh12345 wrote:Not sure what you're asking.
Sorry for beeing so fuzzy, I'm a beginner.
TonyPh12345 wrote:Basically, if it's using a POST, then you need to have a CGI on the server side to catch the post and interpret the data that's POSTed.
My first assumption was (as nothing worked) that something needed to be enabled at the NAS side. That's obviously wrong. I slowly realize what you are saying, that I need to do some programming to catch the data posted by the cam. I'm going to do it in PHP, but for a non-programmer it's not easy.

Thanks
TS-419U+, 4x Seagate Barracuda ST 31000528AS, 1 TB, RAID 5, FW 4.1.4, Apache, PHP, KNX eibd
Greetings from Midi-Pyrénées, France
P3R
Guru
Posts: 13187
Joined: Sat Dec 29, 2007 1:39 am
Location: Stockholm, Sweden (UTC+01:00)

Re: How to "Post" data to the web server ?

Post by P3R »

If you got FTP working, the pictures (or movies) can be put where you want them (maybe in the web directory to be displayed on your web?) and with the email you get notifications of the events.

What more do you expect to achieve with web postings?
RAID have never ever been a replacement for backups. Without backups on a different system (preferably placed at another site), you will eventually lose data!

A non-RAID configuration (including RAID 0, which isn't really RAID) with a backup on a separate media protects your data far better than any RAID-volume without backup.

All data storage consists of both the primary storage and the backups. It's your money and your data, spend the storage budget wisely or pay with your data!
User avatar
marc.assin
Getting the hang of things
Posts: 54
Joined: Wed Mar 02, 2011 4:15 pm
Location: Midi-Pyrénées, France
Contact:

Re: How to "Post" data to the web server ?

Post by marc.assin »

Hi P3R,
P3R wrote:If you got FTP working, the pictures (or movies) can be put where you want them
Indeed, that part is OK
P3R wrote:What more do you expect to achieve with web postings?
Well, I didn't tell the whole story.
The cameras are part of a larger domotics project. I'm mainly interested in the asynch aspect of a trigger.
Suppose the cam is hanging on the ceiling of the hall, with a predefined motion detection area focused on the front door... and the cam triggers a 03:00 ...
I want to capture that event (the video is already saved) with a PHP script and trigger a KNX alarm, just like a standard KNX fire alarm or presence detector.
The camera is setup for HTTP notification, with URL base set to /Cam/Cam1/cam1.php.
When the camera detecs a motion, the PHP script is executed, but I'm unable to get any data from the POST.
I'm a beginner in this field, a short piece of code, supposed to run on a QNAP would be much appreciated
Here is my code

Code: Select all

echo "Hello World";
$file=fopen("kk.txt","w");
fwrite($file,"IP camera1 notification".chr(13).chr(10));
foreach ($arr as $key => $value) { 
    $toFile = "Key: $key; Value: $value \n"; 
fwrite($file, "$toFile") or die('Could not write to file');
} 
fclose($file);
TS-419U+, 4x Seagate Barracuda ST 31000528AS, 1 TB, RAID 5, FW 4.1.4, Apache, PHP, KNX eibd
Greetings from Midi-Pyrénées, France
TonyPh12345
Been there, done that
Posts: 738
Joined: Tue Jul 13, 2010 11:53 pm

Re: How to "Post" data to the web server ?

Post by TonyPh12345 »

If it's using POST, your PHP probably needs to be reading the contents out of the $_POST special variable.

So try, instead (noting that I substituted only a variable name...)

Code: Select all

echo "Hello World";
$file=fopen("kk.txt","w");
fwrite($file,"IP camera1 notification".chr(13).chr(10));
foreach ($_POST as $key => $value) { 
    $toFile = "Key: $key; Value: $value \n"; 
    fwrite($file, "$toFile") or die('Could not write to file');
} 
fclose($file);
See what that gets you.

Oh, btw, you might want to use the open mode "a" instead of "w" since "w" will OVERWRITE your file, and "a" will APPEND to your file.
User avatar
marc.assin
Getting the hang of things
Posts: 54
Joined: Wed Mar 02, 2011 4:15 pm
Location: Midi-Pyrénées, France
Contact:

Re: How to "Post" data to the web server ?

Post by marc.assin »

TonyPh12345 wrote:If it's using POST, your PHP probably needs to be reading the contents out of the $_POST special variable.
Yes, most probably, that's what I'm trying since days....
TonyPh12345 wrote:See what that gets you.
No luck, the file has just the top string, no post data

What would the code look like if it wasn't a POST but a GET ?

Thank you for your time
TS-419U+, 4x Seagate Barracuda ST 31000528AS, 1 TB, RAID 5, FW 4.1.4, Apache, PHP, KNX eibd
Greetings from Midi-Pyrénées, France
TonyPh12345
Been there, done that
Posts: 738
Joined: Tue Jul 13, 2010 11:53 pm

Re: How to "Post" data to the web server ?

Post by TonyPh12345 »

Same thing, but using the variable $_GET. :)

That's a good way to troubleshoot, because you make all the get queries you want using your own browser...

http://your.webserver.name/cam/cam1/cam ... le2=value2 ... etc

There's actually ANOTHER variable $_REQUEST that will work with BOTH GET and POST methods.

I Googled some stuff and also came across this interesting tidbit:

http://www.bradino.com/php/empty-post-array/
User avatar
marc.assin
Getting the hang of things
Posts: 54
Joined: Wed Mar 02, 2011 4:15 pm
Location: Midi-Pyrénées, France
Contact:

Re: How to "Post" data to the web server ?

Post by marc.assin »

TonyPh12345 wrote:Same thing, but using the variable $_GET.
This code

Code: Select all

foreach ($_GET as $key => $value) { 
    $toFile = "Key: $key; Value: $value" .chr(13).chr(10);
    fwrite($file, "$toFile") or die('Could not write to file');
}  
works fine and gives this result

Code: Select all

Key: action; Value: update
Key: DeviceName; Value: Cam-NO
Key: IPAddress; Value: 10.0.1.242
Key: MacAddress; Value: 00:1D:E5:EA:81:81
Key: DateTime; Value: 05302011 21:23:12
Key: ActivePostCount; Value: 1
Key: EventType; Value: 2
Key: EventState; Value: 1
Key: EventDescription; Value: 
Key: RegionIndex1; Value: 1
Key: DetectionThreshold1; Value: 50
This is great, but its an enum. Is it possible to get the count of elements and extract just 1 from the list ?
Also, this works only when the flag "Post" from the camera is NOT set.

I tried the Post method with the flag set, and was unable to get anything, any idea ? maybe a crummy doc ?
Thanks for the tip
I tried this with the cam flag Post, set

Code: Select all

$data=file_get_contents('php://input'); 
fwrite($file, $data);
guess what ?

Code: Select all

<?xml version="1.0" encoding="UTF-8"?><DeviceInfo><version>0.0.1</version><EventNotificationAlert><deviceName>Cam-NO</deviceName><ipAddress>10.0.1.242</ipAddress><macAddress>00:1D:E5:EA:81:81</macAddress><dateTime>05302011 21:37:15</dateTime><activePostCount>1</activePostCount><eventType>2</eventType><eventState>1</eventState><eventDescription></eventDescription><DetectionRegionList><DetectionRegionEntry><regionIndex>1</regionIndex><detectionThreshold>50</detectionThreshold></DetectionRegionEntry></DetectionRegionList></EventNotificationAlert></DeviceInfo>
What I'm still missing is a way to extract individual parameters.

Thank you for your precious help with a PHP newbee
TS-419U+, 4x Seagate Barracuda ST 31000528AS, 1 TB, RAID 5, FW 4.1.4, Apache, PHP, KNX eibd
Greetings from Midi-Pyrénées, France
TonyPh12345
Been there, done that
Posts: 738
Joined: Tue Jul 13, 2010 11:53 pm

Re: How to "Post" data to the web server ?

Post by TonyPh12345 »

Yes, you can select any specific "variable" via the method

$myvariable = $_GET["action"];

which should set $myvariable to the value "update" given the info you posted before.

you can find out how many elements are in the array using the function "count", as in

echo count($_GET);

It appears that with POST, it's actually sending an XML file instead of a set of elements & values. You'll have to code for that. If it were me, I'd probably stay with GET. :)
User avatar
marc.assin
Getting the hang of things
Posts: 54
Joined: Wed Mar 02, 2011 4:15 pm
Location: Midi-Pyrénées, France
Contact:

Re: How to "Post" data to the web server ?

Post by marc.assin »

TonyPh12345 wrote:Yes, you can select any specific "variable" via the method
$myvariable = $_GET["action"];
Fine
TonyPh12345 wrote:you can find out how many elements are in the array using the function "count", as in
echo count($_GET);
Great !

I guess I'm all set now to start doing something usefull

A big many thanks.
Greetings from Midi-Pyrénées
TS-419U+, 4x Seagate Barracuda ST 31000528AS, 1 TB, RAID 5, FW 4.1.4, Apache, PHP, KNX eibd
Greetings from Midi-Pyrénées, France
User avatar
marc.assin
Getting the hang of things
Posts: 54
Joined: Wed Mar 02, 2011 4:15 pm
Location: Midi-Pyrénées, France
Contact:

Re: How to "Post" data to the web server ?

Post by marc.assin »

TonyPh12345 wrote:Yes, you can select any specific "variable" via the method
$myvariable = $_GET["action"];
Mmmm, it took the newbee some time to realize what you are saying, but got it (finally)

I'm still puzzled. Given that the response of $_GET is an array, as in
$arr= $_GET;
I wonder why I can't exploit this array in the classical way, with an index, as in
echo $arr[0]
Maybe this array is "special" ?
TS-419U+, 4x Seagate Barracuda ST 31000528AS, 1 TB, RAID 5, FW 4.1.4, Apache, PHP, KNX eibd
Greetings from Midi-Pyrénées, France
User avatar
marc.assin
Getting the hang of things
Posts: 54
Joined: Wed Mar 02, 2011 4:15 pm
Location: Midi-Pyrénées, France
Contact:

Re: How to "Post" data to the web server ?

Post by marc.assin »

marc.assin wrote:I'm still puzzled. Given that the response of $_GET is an array, as in
$arr= $_GET;
I wonder why I can't exploit this array in the classical way, with an index, as in
echo $arr[0]
It's an associative array !
The newbee should read the doc more carefully
TS-419U+, 4x Seagate Barracuda ST 31000528AS, 1 TB, RAID 5, FW 4.1.4, Apache, PHP, KNX eibd
Greetings from Midi-Pyrénées, France
TonyPh12345
Been there, done that
Posts: 738
Joined: Tue Jul 13, 2010 11:53 pm

Re: How to "Post" data to the web server ?

Post by TonyPh12345 »

Heheh... Yeah, that's the classic "Potato" vs "PoTAHto" thing... (an English Idiom, sorry. :)

Perl calls such things HASHES, which are indexed through Keys. ARRAYs are indexed numerically. They're two different beasts entirely.

Depending on how deep you want to get into PHP, I strongly recommend the book "Learning PHP 5" from O'Reilly Press.
Locked

Return to “Web Server & Applications (Apache + PHP + MySQL / SQLite)”