I need to process form POST data sent to a PHP file. However the data is sent as binary, so referencing the data in the POST using variables will not work. I have included a PERL script and ASP(VBScript) script that works.
I need a PHP version, as I need to dump the POST variables into a mySQL database.
I have tried referencing:
$GLOBALS["HTTP_RAW_POST_DATA"]
$binaryResult=$HTTP_RAW_POST_DATA;
rawPOST() from Danny Shepherd <danny@kyboshed.com> snippet ID 665/997 in the HTTP code library
None of which appear to work.
Can anyone help?
PERL
#!/usr/local/bin/perl
if ($ENV{'REQUEST_METHOD'} eq 'POST') {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
}
foreach $pair (@pairs) {
local($name, $value) = split(/=/, $pair);
$name =~ tr/+/ /;
$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<!--(.|\n)*-->//g;
$Form{$name} = $value;
}
open ("LOG", ">> bmsdata.txt");
print LOG "$Form{'datetime'} , $Form{'oid'} , $Form{'total'} , $Form{'transactionstatus'} \n";
close (LOG);
exit;
ASP
<BODY>
<%
‘ Constant/Vars for log file processing
const ForAppending = 8
Dim strData
Dim fs, strPath, TestFile
‘ Create a text file for the response
strPath = Server.MapPath(“CGIResult.txt”)
Set fs = CreateObject(“Scripting.FileSystemObject”)
Set TestFile = fs.OpenTextFile(strPath,ForAppending,true)
TestFile.Write “ePDQ CPI Result data - received on: “ & Date() & “ “ & Time() & vbCRLF
Response.Write “Test file is “ & strPath
‘ Try and extract the binary representation of the data
‘ The normal Request.Form will not work here
Dim strFullPostData
Dim biRawData
Dim nIndex
Dim astrPostDataElements
Dim strPostData
‘ Put down the details we will be extracting
TestFile.Write “The size of the CPI post is: “ & vbCRLF
TestFile.Write Request.TotalBytes & vbCRLF
‘ Extract binary data
biRawData = Request.BinaryRead(Request.TotalBytes)
‘ Now change binary data into something more readable
For nIndex = 1 to LenB(biRawData)
strFullPostData = strFullPostData & Chr(AscB(MidB(biRawData,nIndex,1)))
Next
Handling the returned response
18
‘ We now have a raw string containing CPI data - first extract individual vars
astrPostDataElements = Split(strFullPostData, “&”)
‘ Finally, output the info
TestFile.Write “Data extracted from CPI post is: “ & vbCRLF
Dim astrPostElements
For Each strPostData in astrPostDataElements
‘ Output the raw data first
TestFile.Write strPostData & vbCRLF
‘ Split up and prepare the field name and data
astrPostElements = Split(strPostData, “=”)
‘ Output the details
TestFile.Write “Which translates to Field Name: “ & astrPostElements(0) & vbCRLF
TestFile.Write “and Field Data: “ & astrPostElements(1) & vbCRLF
Next
‘ And we are done - close the file
TestFile.Close
%>
</BODY>