Page 1 of 1

To Know about php://input

Posted: Sat Nov 29, 2008 12:58 am
by Ravisankar
Dear Developers,

I have used php to read raw post data from URL which is called from Java. For that I have used file_get_contents('php://input'). From Java I sent byte array as a URL request, In that, first 4 bytes tells me how many bytes to be read from Input stream. I used to get the first 4 bytes and convert that in to Integer. So Now i know How many bytes remaining from 4th bytes in the byte array. I could not able to read further remaining bytes when I run loop second time based on offset,maxlen of bytes.

In this case file_get_contents('document.txt',false,null,offset,maxlen) works fine in a loop. but
file_get_contents('php://input',false,null,offset,maxlen) does not works fine in a loop.

Can any one suggest a solution?

Here my code
<php
$start=0;
$how_much=4;
$string = file_get_contents('php://input',false,null,$start,$how_much);
$first_4bytes = str_split($string);
$total_no_bytes = byteArrayToInt($first_4bytes,0);
$log = "Total No. of. bytes is $total_no_bytes";
write_log($logfile,$filename,$log,"");

while(true)
{
$start = ($start+$how_much);
$how_much=4;
$log ='';
$log .= "\nStart position is $start and $how_much bytes suppose to be get";
$string = file_get_contents('php://input',false,null,$start,$how_much);
$second_4bytes = str_split($string);
$app_len = byteArrayToInt($second_4bytes,0);
$log .= "\nAppointment length is $app_len";
if($app_len == 0)
{
$log .= " So Going to Beak the loop";
write_log($logfile,$filename,$log,"");
break;
}


$start=($start+$how_much);
$how_much=$app_len;
$log .= "\nNow, Start position is $start and $how_much bytes suppose to be get";
$string = file_get_contents('php://input',false,null,$start,$how_much);
$app_bytes = str_split($string);
write_log($logfile,$filename,$log,"");
Parse_Appointment($app_array,$mClndr_login);
if($start == $total_no_bytes)
{
$log = "All bytes have been read, So Going to Break the Loop";
write_log($logfile,$filename,$log,"");
break;
}
}
In above code, I could not able to get the second_4bytes from php inputsteam.?
What is the wrong here?

?>

Re: To Know about php://input

Posted: Sat Nov 29, 2008 2:33 am
by requinix
So the structure of the input is (record length)(record) repeated any number of times?

I don't quite follow what you have there, but I'd just read the stream byte-at-a-time.

Code: Select all

$h = fopen("php://input", "r");
while (1) {
    So the structure of the input is [i](record length)(record)[/i] repeated any number of times?
 
I don't quite follow what you have there. It looks like you keep re-reading the input stream but you don't have any logic to extract the part that you want.
 
Anyways, here's what I'd do:
[php]$h = fopen("php://input", "r");
while (1) {
    $bytes = fread($h, 4); // first four bytes are the record length
    if (strlen($bytes) < 4) { /* end of data, or corrupt */ }
    $len = byteArrayToInt($bytes, 0); // convert to number
    echo "length=$len\n";
 
    $bytes = fread($h, $len); // read the record
    if (strlen($bytes) < $len) { /* corrupt */ }
    $bytes = str_split($bytes); // split the string into bytes
    Parse_Appointment($bytes, $mClndr_login);
}

Re: To Know about php://input

Posted: Sat Nov 29, 2008 5:16 am
by Ravisankar
Dear tasairis,

Thank you for your help. Now I can able to read all bytes in loop after using your suggestion.
Thank you.