Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #4749 > unrolled thread

Headers in fpassthru() output

Started byDAVISM@ecr6.ohio-state.edu (Michael T. Davis)
First post2012-02-07 02:40 +0000
Last post2012-02-08 10:10 +0100
Articles 9 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  Headers in fpassthru() output DAVISM@ecr6.ohio-state.edu (Michael T. Davis) - 2012-02-07 02:40 +0000
    Re: Headers in fpassthru() output "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-02-07 09:48 +0100
    Re: Headers in fpassthru() output "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-02-07 09:56 +0100
    Re: Headers in fpassthru() output "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2012-02-07 10:34 +0100
      Re: Headers in fpassthru() output DAVISM@ecr6.ohio-state.edu (Michael T. Davis) - 2012-02-07 16:36 +0000
        Re: Headers in fpassthru() output "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-02-07 17:50 +0100
          Re: Headers in fpassthru() output DAVISM@ecr6.ohio-state.edu (Michael T. Davis) - 2012-02-07 17:35 +0000
            Re: Headers in fpassthru() output "M. Strobel" <sorry_no_mail_here@nowhere.dee> - 2012-02-08 11:38 +0100
        Re: Headers in fpassthru() output "Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid> - 2012-02-08 10:10 +0100

#4749 — Headers in fpassthru() output

FromDAVISM@ecr6.ohio-state.edu (Michael T. Davis)
Date2012-02-07 02:40 +0000
SubjectHeaders in fpassthru() output
Message-ID<jgq2u7$unn$1@news1.cse.ohio-state.edu>
	So far, I've tested a call to fopen followed by a call to fpassthru
on a remote (http://...) JPEG file with PHP v4.4.7 in the "OSU Web Server"
running under OpenVMS V8.3 on an Alpha, and with PHP v5.3.4 in Apache v2.2.17
running under Mac OS X v10.6.8.  The JPEG file is served by a Webcam.  The
documentation (on php.net) for "http://" says the stream opened by fopen only
provides access to the body of the response.  Why is it, then, that when I
call on fpassthru for the (JPEG URL) stream, I see the headers included in the
data returned?

	The code looks basically like this:

 $image = fopen ( "http://...jpg", "r" );
 foreach ( $http_response_header as $header )
 {
   echo "{$header}\r\n";
 }
 echo "\r\n";
 fpassthru ( $image );
 fclose ( $image );

With the above, the headers are clearly visible at the beginning of the data,
rendering a lot of meaningless code in the browser window, rather than an
actual image.  One work-around I've developed is...

 $image = fopen ( "http://...jpg", "r" );
 $inHeaders = 1;
 while ( $inHeaders && ! feof ( $image ) )
 {
   $line = fgets ( $image );
   echo $line;
   $inHeaders = ( $line != "\r\n" );
 }
 if ( ! feof ( $image ) )
   fpassthru ( $image );
 fclose ( $image );

Can anyone explain why I'm seeing behavior counter to the documentation with
"fopen ( 'http://...' )" and "fpassthru (...)"?  (FWIW, the purpose of the
code is to "proxy" a Webcam image that isn't directly available, so that as
far as the browser is concerned, the image is served by the web server
hosting this PHP code.)

Thanks,
Mike

[toc] | [next] | [standalone]


#4753

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-02-07 09:48 +0100
Message-ID<9pc6r5FtnsU1@mid.uni-berlin.de>
In reply to#4749
Am 07.02.2012 03:40, schrieb Michael T. Davis:
> 	So far, I've tested a call to fopen followed by a call to fpassthru
> on a remote (http://...) JPEG file with PHP v4.4.7 in the "OSU Web Server"
> running under OpenVMS V8.3 on an Alpha, and with PHP v5.3.4 in Apache v2.2.17
> running under Mac OS X v10.6.8.  The JPEG file is served by a Webcam.  The
> documentation (on php.net) for "http://" says the stream opened by fopen only
> provides access to the body of the response.  Why is it, then, that when I
> call on fpassthru for the (JPEG URL) stream, I see the headers included in the
> data returned?
> 
> 	The code looks basically like this:
> 
>  $image = fopen ( "http://...jpg", "r" );
>  foreach ( $http_response_header as $header )
>  {
>    echo "{$header}\r\n";
>  }
>  echo "\r\n";
>  fpassthru ( $image );
>  fclose ( $image );
> 
> With the above, the headers are clearly visible at the beginning of the data,
> rendering a lot of meaningless code in the browser window, rather than an
> actual image.  One work-around I've developed is...
> 
>  $image = fopen ( "http://...jpg", "r" );
>  $inHeaders = 1;
>  while ( $inHeaders && ! feof ( $image ) )
>  {
>    $line = fgets ( $image );
>    echo $line;
>    $inHeaders = ( $line != "\r\n" );
>  }
>  if ( ! feof ( $image ) )
>    fpassthru ( $image );
>  fclose ( $image );
> 
> Can anyone explain why I'm seeing behavior counter to the documentation with
> "fopen ( 'http://...' )" and "fpassthru (...)"?  (FWIW, the purpose of the
> code is to "proxy" a Webcam image that isn't directly available, so that as
> far as the browser is concerned, the image is served by the web server
> hosting this PHP code.)
> 
> Thanks,
> Mike

I can't confirm the problem - no headers here:

strobel@s114-intel:~> php -a
Interactive shell

php > $h = fopen('http://php.net','r');
php > fpassthru ($h);
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
 <title>PHP: Hypertext Preprocessor</title>
...

Tested on an ubuntu 10.04 server.

/Str.

[toc] | [prev] | [next] | [standalone]


#4754

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-02-07 09:56 +0100
Message-ID<9pc79eF1loU1@mid.uni-berlin.de>
In reply to#4749
Am 07.02.2012 03:40, schrieb Michael T. Davis:
> 	So far, I've tested a call to fopen followed by a call to fpassthru
> on a remote (http://...) JPEG file with PHP v4.4.7 in the "OSU Web Server"
> running under OpenVMS V8.3 on an Alpha, and with PHP v5.3.4 in Apache v2.2.17
> running under Mac OS X v10.6.8.  The JPEG file is served by a Webcam.  The
> documentation (on php.net) for "http://" says the stream opened by fopen only
> provides access to the body of the response.  Why is it, then, that when I
> call on fpassthru for the (JPEG URL) stream, I see the headers included in the
> data returned?
> 
> 	The code looks basically like this:
> 
>  $image = fopen ( "http://...jpg", "r" );
>  foreach ( $http_response_header as $header )
>  {
>    echo "{$header}\r\n";

You should not echo the headers, this will send your web server’s headers, and as
body your echoed headers.

Use header() function instead -  http://de.php.net/manual/de/function.header.php

/Str.

[toc] | [prev] | [next] | [standalone]


#4755

From"Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid>
Date2012-02-07 10:34 +0100
Message-ID<jgqr6d$dlh$1@dont-email.me>
In reply to#4749
El 07/02/2012 3:40, Michael T. Davis escribió/wrote:
> 	So far, I've tested a call to fopen followed by a call to fpassthru
> on a remote (http://...) JPEG file with PHP v4.4.7 in the "OSU Web Server"
> running under OpenVMS V8.3 on an Alpha, and with PHP v5.3.4 in Apache v2.2.17
> running under Mac OS X v10.6.8.  The JPEG file is served by a Webcam.  The
> documentation (on php.net) for "http://" says the stream opened by fopen only
> provides access to the body of the response.  Why is it, then, that when I
> call on fpassthru for the (JPEG URL) stream, I see the headers included in the
> data returned?
>
> 	The code looks basically like this:
>
>   $image = fopen ( "http://...jpg", "r" );
>   foreach ( $http_response_header as $header )
>   {
>     echo "{$header}\r\n";
>   }
>   echo "\r\n";
>   fpassthru ( $image );
>   fclose ( $image );
>
> With the above, the headers are clearly visible at the beginning of the data,
> rendering a lot of meaningless code in the browser window, rather than an
> actual image.

I don't know for sure what manual page you are quoting (you just point 
to the PHP site) but the full quote at the page for the HTTP wrapper [1] is:

«The stream allows access to the body of the resource; the headers are 
stored in the $http_response_header variable.»

... and that's exactly what you are doing: printing the values of the 
$http_response_header variable [2].

[1] http://es2.php.net/manual/en/wrappers.http.php
[2] http://es2.php.net/manual/en/reserved.variables.httpresponseheader.php





>  One work-around I've developed is...
>
>   $image = fopen ( "http://...jpg", "r" );
>   $inHeaders = 1;
>   while ( $inHeaders&&  ! feof ( $image ) )
>   {
>     $line = fgets ( $image );
>     echo $line;
>     $inHeaders = ( $line != "\r\n" );
>   }
>   if ( ! feof ( $image ) )
>     fpassthru ( $image );
>   fclose ( $image );
>
> Can anyone explain why I'm seeing behavior counter to the documentation with
> "fopen ( 'http://...' )" and "fpassthru (...)"?

Neither http://es.php.net/manual/en/function.fopen.php nor 
http://es.php.net/fpassthru claim that $http_response_header should not 
get populated...



-- 
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

[toc] | [prev] | [next] | [standalone]


#4757

FromDAVISM@ecr6.ohio-state.edu (Michael T. Davis)
Date2012-02-07 16:36 +0000
Message-ID<jgrju2$uqv$1@news1.cse.ohio-state.edu>
In reply to#4755
In article <jgqr6d$dlh$1@dont-email.me>,
=?ISO-8859-1?Q?=22=C1lvaro_G=2E_Vicario=22?=<alvaro.NOSPAMTHANX@demogracia.com.invalid> writes:

>El 07/02/2012 3:40, Michael T. Davis escribió/wrote:
>> 	So far, I've tested a call to fopen followed by a call to fpassthru
>> on a remote (http://...) JPEG file with PHP v4.4.7 in the "OSU Web Server"
>> running under OpenVMS V8.3 on an Alpha, and with PHP v5.3.4 in Apache
> v2.2.17
>> running under Mac OS X v10.6.8.  The JPEG file is served by a Webcam.  The
>> documentation (on php.net) for "http://" says the stream opened by fopen
> only
>> provides access to the body of the response.  Why is it, then, that when I
>> call on fpassthru for the (JPEG URL) stream, I see the headers included in
> the
>> data returned?
>>
>> 	The code looks basically like this:
>>
>>   $image = fopen ( "http://...jpg", "r" );
>>   foreach ( $http_response_header as $header )
>>   {
>>     echo "{$header}\r\n";
>>   }
>>   echo "\r\n";
>>   fpassthru ( $image );
>>   fclose ( $image );
>>
>> With the above, the headers are clearly visible at the beginning of the
> data,
>> rendering a lot of meaningless code in the browser window, rather than an
>> actual image.
>
>I don't know for sure what manual page you are quoting (you just point
>to the PHP site) but the full quote at the page for the HTTP wrapper [1] is:
>
>«The stream allows access to the body of the resource; the headers are
>stored in the $http_response_header variable.»
>
>... and that's exactly what you are doing: printing the values of the
>$http_response_header variable [2].
>
>[1] http://es2.php.net/manual/en/wrappers.http.php
>[2] http://es2.php.net/manual/en/reserved.variables.httpresponseheader.php

	I'm referencing the English version of the documentation that you
are citing.

>
>
>
>
>
>>  One work-around I've developed is...
>>
>>   $image = fopen ( "http://...jpg", "r" );
>>   $inHeaders = 1;
>>   while ( $inHeaders&&  ! feof ( $image ) )
>>   {
>>     $line = fgets ( $image );
>>     echo $line;
>>     $inHeaders = ( $line != "\r\n" );
>>   }
>>   if ( ! feof ( $image ) )
>>     fpassthru ( $image );
>>   fclose ( $image );
>>
>> Can anyone explain why I'm seeing behavior counter to the documentation with
>> "fopen ( 'http://...' )" and "fpassthru (...)"?
>
>Neither http://es.php.net/manual/en/function.fopen.php nor
>http://es.php.net/fpassthru claim that $http_response_header should not
>get populated...

	 I didn't claim that $http_response_header was invalid.  As I
mentioned in the parenthetical at the end of the original post, I'm trying
to serve the JPEG image of a network Webcam from the host where I have the
PHP script, in order to make it appear as if the JPEG image comes from the
PHP-running host.  This means I need to send the headers that come from
the response of the Webcam to the browser.  I can certainly leverage
$http_response_header and push the contents to the broswer, but if I do
that--given the behavior I'm seeing--it seems I would still need to parse
the headers from the fopen call and "drop them on the floor" before sending
the "actual" body/image of what comes from the Webcam.  If I'm going to
parse the headers, I figured I might as well do something with them, which
is why the work-around doesn't call on $http_response_header.

>
>
>
>--
>-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
>-- Mi sitio sobre programación web: http://borrame.com
>-- Mi web de humor satinado: http://www.demogracia.com
>--

Regards,
Mike

[toc] | [prev] | [next] | [standalone]


#4758

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-02-07 17:50 +0100
Message-ID<9pd32eFlgeU1@mid.uni-berlin.de>
In reply to#4757
Am 07.02.2012 17:36, schrieb Michael T. Davis:

> 	 I didn't claim that $http_response_header was invalid.  As I
> mentioned in the parenthetical at the end of the original post, I'm trying
> to serve the JPEG image of a network Webcam from the host where I have the
> PHP script, in order to make it appear as if the JPEG image comes from the
> PHP-running host.  This means I need to send the headers that come from
> the response of the Webcam to the browser.  I can certainly leverage
> $http_response_header and push the contents to the broswer, but if I do
> that--given the behavior I'm seeing--it seems I would still need to parse
> the headers from the fopen call and "drop them on the floor" before sending
> the "actual" body/image of what comes from the Webcam.  If I'm going to
> parse the headers, I figured I might as well do something with them, which
> is why the work-around doesn't call on $http_response_header.
--cut
> 
> Regards,
> Mike

This does not sound like you read and understood my answers.

Drop your workaround, and start sending the headers with the header() function using
$http_response_header.

/Str.

[toc] | [prev] | [next] | [standalone]


#4759

FromDAVISM@ecr6.ohio-state.edu (Michael T. Davis)
Date2012-02-07 17:35 +0000
Message-ID<jgrnca$13b$1@news1.cse.ohio-state.edu>
In reply to#4758
In article <9pd32eFlgeU1@mid.uni-berlin.de>, "M. Strobel"
<sorry_no_mail_here@nowhere.dee> writes:

>Am 07.02.2012 17:36, schrieb Michael T. Davis:
>
>> 	 I didn't claim that $http_response_header was invalid.  As I
>> mentioned in the parenthetical at the end of the original post, I'm trying
>> to serve the JPEG image of a network Webcam from the host where I have the
>> PHP script, in order to make it appear as if the JPEG image comes from the
>> PHP-running host.  This means I need to send the headers that come from
>> the response of the Webcam to the browser.  I can certainly leverage
>> $http_response_header and push the contents to the broswer, but if I do
>> that--given the behavior I'm seeing--it seems I would still need to parse
>> the headers from the fopen call and "drop them on the floor" before sending
>> the "actual" body/image of what comes from the Webcam.  If I'm going to
>> parse the headers, I figured I might as well do something with them, which
>> is why the work-around doesn't call on $http_response_header.
>--cut
>>
>> Regards,
>> Mike
>
>This does not sound like you read and understood my answers.
>
>Drop your workaround, and start sending the headers with the header()
> function u

>
sing
>$http_response_header.

	OK, this was my original code:

 $image = fopen ( "http://...jpg", "r" );
 foreach ( $http_response_header as $header )
 {
   echo "{$header}\r\n";
 }
 echo "\r\n";
 fpassthru ( $image );
 fclose ( $image );

The issue was apparently my use of `echo' to send the headers to the
browser.  I took your direction literally, and it seems to work:

 $image = fopen ( "http://...jpg", "r" );
 foreach ( $http_response_header as $header )
 {
   header ( $header );
 }
 fpassthru ( $image );
 fclose ( $image );

As I thought about it, my mistake was clear.

>
>/Str.

Thanks,
Mike

[toc] | [prev] | [next] | [standalone]


#4761

From"M. Strobel" <sorry_no_mail_here@nowhere.dee>
Date2012-02-08 11:38 +0100
Message-ID<9pf1lqFmecU1@mid.uni-berlin.de>
In reply to#4759
Am 07.02.2012 18:35, schrieb Michael T. Davis:
> In article <9pd32eFlgeU1@mid.uni-berlin.de>, "M. Strobel"
> <sorry_no_mail_here@nowhere.dee> writes:
> 
>> Am 07.02.2012 17:36, schrieb Michael T. Davis:
>>
>>> 	 I didn't claim that $http_response_header was invalid.  As I
>>> mentioned in the parenthetical at the end of the original post, I'm trying
>>> to serve the JPEG image of a network Webcam from the host where I have the
>>> PHP script, in order to make it appear as if the JPEG image comes from the
>>> PHP-running host.  This means I need to send the headers that come from
>>> the response of the Webcam to the browser.  I can certainly leverage
>>> $http_response_header and push the contents to the broswer, but if I do
>>> that--given the behavior I'm seeing--it seems I would still need to parse
>>> the headers from the fopen call and "drop them on the floor" before sending
>>> the "actual" body/image of what comes from the Webcam.  If I'm going to
>>> parse the headers, I figured I might as well do something with them, which
>>> is why the work-around doesn't call on $http_response_header.
>> --cut
>>>
>>> Regards,
>>> Mike
>>
>> This does not sound like you read and understood my answers.
>>
>> Drop your workaround, and start sending the headers with the header()
>> function u
> 
>>
> sing
>> $http_response_header.
> 
> 	OK, this was my original code:
> 
>  $image = fopen ( "http://...jpg", "r" );
>  foreach ( $http_response_header as $header )
>  {
>    echo "{$header}\r\n";
>  }
>  echo "\r\n";
>  fpassthru ( $image );
>  fclose ( $image );
> 
> The issue was apparently my use of `echo' to send the headers to the
> browser.  I took your direction literally, and it seems to work:
> 
>  $image = fopen ( "http://...jpg", "r" );
>  foreach ( $http_response_header as $header )
>  {
>    header ( $header );
>  }
>  fpassthru ( $image );
>  fclose ( $image );
> 
> As I thought about it, my mistake was clear.
> 
>>
>> /Str.
> 
> Thanks,
> Mike

:-0

Once you've got this you could proceed to optimize the headers / write your own -
cache? content-type?

/Str.

[toc] | [prev] | [next] | [standalone]


#4760

From"Álvaro G. Vicario" <alvaro.NOSPAMTHANX@demogracia.com.invalid>
Date2012-02-08 10:10 +0100
Message-ID<jgte5j$ulf$1@dont-email.me>
In reply to#4757
El 07/02/2012 17:36, Michael T. Davis escribió/wrote:
>> I don't know for sure what manual page you are quoting (you just point
>> to the PHP site) but the full quote at the page for the HTTP wrapper [1] is:
>>
>> «The stream allows access to the body of the resource; the headers are
>> stored in the $http_response_header variable.»
>>
>> ... and that's exactly what you are doing: printing the values of the
>> $http_response_header variable [2].
>>
>> [1] http://es2.php.net/manual/en/wrappers.http.php
>> [2] http://es2.php.net/manual/en/reserved.variables.httpresponseheader.php
>
> 	I'm referencing the English version of the documentation that you
> are citing.

Well, the documentation I'm citing is already in English :)



>> Neither http://es.php.net/manual/en/function.fopen.php nor
>> http://es.php.net/fpassthru claim that $http_response_header should not
>> get populated...
>
> 	I didn't claim that $http_response_header was invalid.  As I
> mentioned in the parenthetical at the end of the original post, I'm trying
> to serve the JPEG image of a network Webcam from the host where I have the
> PHP script, in order to make it appear as if the JPEG image comes from the
> PHP-running host.  This means I need to send the headers that come from
> the response of the Webcam to the browser.  I can certainly leverage
> $http_response_header and push the contents to the broswer, but if I do
> that--given the behavior I'm seeing--it seems I would still need to parse
> the headers from the fopen call and "drop them on the floor" before sending
> the "actual" body/image of what comes from the Webcam.  If I'm going to
> parse the headers, I figured I might as well do something with them, which
> is why the work-around doesn't call on $http_response_header.

I think you already got it, according to your latest post in this 
thread. You were probably getting confused by some CGI modules that send 
the raw output as generated by the script, so you have to start printing 
the HTTP headers plus a blank line. It's not the case in PHP: the output 
of echo statements always goes to the response body.



-- 
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web