PHP Classes

download file with post

Recommend this page to a friend!

      PHP HTTP protocol client  >  All threads  >  download file with post  >  (Un) Subscribe thread alerts  
Subject:download file with post
Summary:download a xls file with post
Messages:6
Author:Fernando
Date:2006-05-16 06:21:55
Update:2006-05-17 07:48:48
 

  1. download file with post   Reply   Report abuse  
Picture of Fernando Fernando - 2006-05-16 06:21:55
Hi:
I'm traying to get a xls file through post, i made changes to a vars $url,$arguments["PostValues"] and $arguments["Referer"] and it's working fine(gracias Manuel :=) ), but i need to download the file so need your help, tell me how can i convert response o how get response as a file.

thank you!

those are response headers and response body(a part of it)

Response headers:
http/1.1 200 ok:
cache-control: must-revalidate, post-check=0,pre-check=0
pragma: public
content-type: application/vnd.ms-excel
expires: 0
server: Microsoft-IIS/6.0
x-powered-by: PHP/4.3.10
x-powered-by: ASP.NET
content-disposition: attachment; filename=precios Aji.xls
microsoftofficewebserver: 5.0_Pub
date: Tue, 16 May 2006 05:56:52 GMT
connection: close

Response body:
ࡱ> )* l 
PreciosAji=%r8X"1Arial1Arial1Arial1Arial1Arial1 Arial1 Arial1Arial                   H  H 83ffff̙̙3f3̙fff3f3f33333f33333
PreciosAji
l *+"dXX??
Y
XPltimo Precio pagado por los distintos poderes compradores para el producto: AjiRegin ProvinciaPoder Comprador
FechaVariedadCalidad
Unidad MedidaMoneda
Precio Mximo 
Precio Mnimo
 Precio Comn
IV
Elqui$Feria Mayorista Las Palmeras
2001-11-02 AmericanoPrimera Peso 

IV
Elqui$Feria Mayorista Las Palmeras
2005-12-19 AmericanoPrimera Kilo Pesou@ y@
y@
IV
Elqui$Feria Mayorista Las Palmeras
2005-10-19 AmericanoSegunda Kilo Peso@@ @
@
IV
Elqui$Feria Mayorista Las Palmeras
2001-12-14CristalPrimera Peso 

IV
Elqui$Feria Mayorista Las Palmeras
2002-08-21CristalPrimera
Caja 30 Kilos Peso @  @
 @
IV
Elqui$Feria Mayorista Las Palmeras
2002-12-04CristalPrimera Kilo Pesor@ y@
u@
IV
Elqui$Feria Mayorista Las Palmeras
2002-11-07CristalSegunda Peso 

IV
Elqui$ Feria Mayorista Las Palmeras 
2002-08-21 Cristal Segunda 
Caja 30 Kilos Peso @ @
@

IV
Elqui$
Feria Mayorista Las Palmeras

2002-11-21
Cristal
Segunda
Kilo
Peso
y@
y@

y@
IV
Elqui$ Feria Mayorista Las Palmeras 
2002-09-02 Cristal Tercera Kilo Peso @@ @@
@@
IV
Elqui$ Feria Mayorista Las Palmeras 
2002-12-30 Cristal America Primera Kilo Peso `@ @
@

IV

Elqui$
Feria Mayorista Las Palmeras

2002-12-23
Cristal America
Segunda
Kilo
Peso
@@
@@
@@
IV
Elqui$Feria Mayorista Las Palmeras
2002-11-25InfiernoPrimera Kilo Pesoy@ y@
y@
IV
Elqui$Feria Mayorista Las Palmeras
2005-04-20Sin EspecificaPrimera Kilo Pesoy@ @@
 |@
IV
Elqui$Feria Mayorista Las Palmeras
2006-04-21Sin EspecificarPrimera Kilo Pesor@ r@
r@
IVLimar)!Feria Mayorista El Agro de Limar
2001-06-22 Kilo Peso@ @
@
IVLimar)!Feria Mayorista El Agro de Limar
2001-05-25Primera Kilo Pesoi@ i@
i@
IVLimar)!Feria Mayorista El Agro de Limar
2001-11-02 AmericanoPrimera Peso 

IVLimar)!Feria Mayorista El Agro de Limar
2006-05-10 AmericanoPrimera
Caja 30 Kilos PesoX@ X@
X@
IVLimar)!Feria Mayorista El Agro de Limari
2005-08-12 AmericanoPrimera Kilo Peso333333? 333333?
333333?
IVLimar)!Feria Mayorista El Agro de Limar
2001-06-20 AmericanoPrimera Kilo Peso@@ @
@@
IVLimar)!Feria Mayorista El Agro de Limar
2001-12-14CRISTALPrimera Peso 

IVLimar)!Feria Mayorista El Agro de Limari
2002-12-04CristalPrimera
Caja 30 Kilos Pesop@ p@
p@
IVLimar)!Feria Mayorista El Agro de Limar
2001-03-21CRISTALPrimera
Caja 30 Kilos Peso@ @
@
IVLimar)!Feria Mayorista El Agro de Limar
2002-11-07CRISTALPrimera Kilo Pesou@ u@
u@
IVLimar)!Feria Mayorista El Agro de Limari
2002-04-22CristalPrimera Kilo PesoQ@ Q@
Q@
IVLimar)!Feria Mayorista El Agro de Limar
2001-12-28CRISTALPrimera Kilo Pesoy@ y@
y@
IVLimar)!Feria Mayorista El Agro de Limari
2001-01-28CristalPrimera Kilo Pesof@ f@
f@
IVLimar)!Feria Mayorista El Agro de Limar
2001-01-26CRISTALPrimera Kilo Pesob@ b@
b@
IVLimar)!Feria Mayorista El Agro de Limar
2002-11-07CRISTALSegunda Peso 

IV Limar) !Feria Mayorista El Agro de Limari 
2002-11-21 Cristal Segunda 


  2. Re: download file with post   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2006-05-17 02:15:32 - In reply to message 1 from Fernando
I am not sure what is the difficulty. If you want to download a file and store it in a local file, just keep reading the response body with the function ReadReplyBody and write the data to your local file.

  3. Re: download file with post   Reply   Report abuse  
Picture of Fernando Fernando - 2006-05-17 04:57:28 - In reply to message 2 from Manuel Lemos
Ok thanks for help me, i made this code:

$error=$http->ReadReplyBody($body,1000);
if($error!=""|| strlen($body)==0)
break;
$gestor = fopen("J:\precios Aji.xls", "w");
fwrite($gestor, $body);
fclose($gestor);

But xls format is wrong, the file created shows a book with rows like this:

2002-04-15ICristalIPrimera IKilo IPesoI@o@I

As you can see, rows are not divided by columns
2002-04-15,Cristal,Primera,etc..

I really appreciate your help

  4. Re: download file with post   Reply   Report abuse  
Picture of Manuel Lemos Manuel Lemos - 2006-05-17 05:20:49 - In reply to message 3 from Fernando
You need to include that code in a loop so you can copy the whole file, not just the first 1000 bytes.

Also you need to use the mode "wb" when writing to binary files, or else you file may become corrupted.

  5. Re: download file with post   Reply   Report abuse  
Picture of Fernando Fernando - 2006-05-17 07:41:49 - In reply to message 4 from Manuel Lemos
GOTCHA!!!
ok, i fixed my code, thanks to MANUEL LEMOS (gracias loco, te pasaste de wena onda!!)
so to get a xml file through a post form i made some changes to test_http_post.php, file
included with "http protocol client class",

vars changed:

$url // giving site url
$arguments["PostValues"] // pairs variable/value
$arguments["Referer"] // request page

and code changes in the response section, replacing this:

echo "<H2><LI>Response body:</LI</H2>\n<PRE>\n";
for(;;)
{
$error=$http->ReadReplyBody($body,1000);
if($error!=""
|| strlen($body)==0)
break;
echo HtmlSpecialChars($body);
}
echo "</PRE>\n";
flush();

to this:

echo "<H2><LI>Response body:</LI</H2>\n<PRE>\n";
$bodyx=""; // initializing body
for(;;)
{
$error=$http->ReadReplyBody($body,100000);
if($error!=""
|| strlen($body)==0)
break;
$bodyx.=$body; // adding new parts from loop
}
$gestor = fopen("J:\precios Aji.xls", "wb"); // opening or creating file with write and binary options
fwrite($gestor, $bodyx); // writing complete body
fclose($gestor); // closing file
echo "</PRE>\n";
flush();

that's all
chao pescao

  6. update, fe de erratas. Re: download...   Reply   Report abuse  
Picture of Fernando Fernando - 2006-05-17 07:48:48 - In reply to message 5 from Fernando
update, fe de erratas.

change "...to get a xml file through..." to "...to get a xls file through..." on last reply....oops...jeje ;-), sorry
bye, chao