post thru fsockopen
am 17.04.2007 16:46:58 von Joakim Ling
Hi there
Im tring to send a file with a php script, Im using php 5.2 on a Win
2003 server. At the other end I just save the file and process the data
and print the result, then fetch the output and write a report. But this
doesn't work, any one got an idea? The file never get to the other end?
When I do a var_export($_POST) is empty.
This is the code I use:
function postFile($file,$host,$path)
{
$remote_page =3D "http://".$host.$path;
$boundary =3D
"---------------------------".substr(md5(rand(0,32000)),0,10 );
// read the file
$fp =3D @fopen($file,"r");
if (!$fp) die ("Cannot read $file !!");
$content_file =3D fread($fp,filesize($file));
fclose($fp);
// define HTTP POST DATA
$data =3D "--$boundary\n".
"Content-Disposition: form-data; name=3D\"file\"; =
filename=3D\"$file\"\n".
"Content-Type: application/octet-stream\n\n$content_file".
"--$boundary--\r\n\r\n";
$msg =3D "POST $remote_page HTTP/1.0\n".
"Content-Type: multipart/form-data; boundary=3D$boundary\n".
"Content-Length: ".strlen($data)."\r\n\r\n";
// Open socket connection ...
$f =3D fsockopen($host,80, $errno, $errstr, 30);
if ($f)
{
// Send the data
fputs($f,$msg.$data);
// retrieve the response
$result=3D"";
while (!feof($f)) {
$result.=3Dfread($f,1024);
}
fclose($f);
// write the response (if needed)
return $result;
} else {
die ("Cannot connect !!!");
}
}
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: post thru fsockopen
am 17.04.2007 18:58:47 von James Crow
On Tuesday 17 April 2007 10:46:58 Joakim Ling wrote:
> Hi there
>
> Im tring to send a file with a php script, Im using php 5.2 on a Win
> 2003 server. At the other end I just save the file and process the data
> and print the result, then fetch the output and write a report. But this
> doesn't work, any one got an idea? The file never get to the other end?
> When I do a var_export($_POST) is empty.
>
>
> This is the code I use:
> function postFile($file,$host,$path)
> {
> $remote_page =3D "http://".$host.$path;
> $boundary =3D
> "---------------------------".substr(md5(rand(0,32000)),0,10 );
>
> // read the file
> $fp =3D @fopen($file,"r");
> if (!$fp) die ("Cannot read $file !!");
> $content_file =3D fread($fp,filesize($file));
> fclose($fp);
>
> // define HTTP POST DATA
> $data =3D "--$boundary\n".
> "Content-Disposition: form-data; name=3D\"file\"; filename=3D\"$file\"\n".
> "Content-Type: application/octet-stream\n\n$content_file".
> "--$boundary--\r\n\r\n";
>
> $msg =3D "POST $remote_page HTTP/1.0\n".
> "Content-Type: multipart/form-data; boundary=3D$boundary\n".
> "Content-Length: ".strlen($data)."\r\n\r\n";
>
> // Open socket connection ...
> $f =3D fsockopen($host,80, $errno, $errstr, 30);
> if ($f)
> {
>
> // Send the data
> fputs($f,$msg.$data);
>
> // retrieve the response
> $result=3D"";
>
> while (!feof($f)) {
> $result.=3Dfread($f,1024);
> }
>
> fclose($f);
> // write the response (if needed)
> return $result;
> } else {
> die ("Cannot connect !!!");
> }
> }
If you can use file_get_contents() with the allow_url_fopen =3D yes directi=
ve in=20
php.ini it would be easier.
=46rom http://us2.php.net/manual/en/wrappers.http.php :
$postdata =3D http_build_query(
array(
'var1' =3D> 'some content',
'var2' =3D> 'doh'
)
);
$opts =3D array('http' =3D>
array(
'method' =3D> 'POST',
'header' =3D> 'Content-type: application/x-www-form-urlencoded',
'content' =3D> $postdata
)
);
$context =3D stream_context_create($opts);
$result =3D file_get_contents('http://example.com/submit.php', false, $cont=
ext);
?>
I have done something similar except I used the $_REQUEST variable in the=20
remote script rather than doing a POST. I had just a small amount of data t=
o=20
pass so $_REQUEST worked for me.
On Windows with PHP4 I had to do some tricks to get this type of thing to=20
work. On Linux with PHP5 it worked flawlessly.
Thanks,
James
=2D-=20
James Crow
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: post thru fsockopen
am 18.04.2007 05:58:46 von Manuel Lemos
Hello,
on 04/17/2007 11:46 AM Joakim Ling said the following:
> Hi there
>
> Im tring to send a file with a php script, Im using php 5.2 on a Win
> 2003 server. At the other end I just save the file and process the data
> and print the result, then fetch the output and write a report. But this
> doesn't work, any one got an idea? The file never get to the other end?
> When I do a var_export($_POST) is empty.
Couldn't that be because you should be checking $_FILES instead of $_POST?
Anyway, I use this HTTP client class and it supports file uploading easily.
http://www.phpclasses.org/httpclient
--
Regards,
Manuel Lemos
Metastorage - Data object relational mapping layer generator
http://www.metastorage.net/
PHP Classes - Free ready to use OOP components written in PHP
http://www.phpclasses.org/
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
RE: post thru fsockopen
am 18.04.2007 09:32:32 von Joakim Ling
-----Original Message-----
From: James Crow [mailto:james@ultratans.com]=20
Sent: den 17 april 2007 18:59
To: php-windows@lists.php.net
Subject: Re: [PHP-WIN] post thru fsockopen
On Tuesday 17 April 2007 10:46:58 Joakim Ling wrote:
> Hi there
>
> Im tring to send a file with a php script, Im using php 5.2 on a Win
> 2003 server. At the other end I just save the file and process the
data
> and print the result, then fetch the output and write a report. But
this
> doesn't work, any one got an idea? The file never get to the other
end?
> When I do a var_export($_POST) is empty.
>
>
> This is the code I use:
> function postFile($file,$host,$path)
> {
> $remote_page =3D "http://".$host.$path;
> $boundary =3D
> "---------------------------".substr(md5(rand(0,32000)),0,10 );
>
> // read the file
> $fp =3D @fopen($file,"r");
> if (!$fp) die ("Cannot read $file !!");
> $content_file =3D fread($fp,filesize($file));
> fclose($fp);
>
> // define HTTP POST DATA
> $data =3D "--$boundary\n".
> "Content-Disposition: form-data; name=3D\"file\"; =
filename=3D\"$file\"\n".
> "Content-Type: application/octet-stream\n\n$content_file".
> "--$boundary--\r\n\r\n";
>
> $msg =3D "POST $remote_page HTTP/1.0\n".
> "Content-Type: multipart/form-data; boundary=3D$boundary\n".
> "Content-Length: ".strlen($data)."\r\n\r\n";
>
> // Open socket connection ...
> $f =3D fsockopen($host,80, $errno, $errstr, 30);
> if ($f)
> {
>
> // Send the data
> fputs($f,$msg.$data);
>
> // retrieve the response
> $result=3D"";
>
> while (!feof($f)) {
> $result.=3Dfread($f,1024);
> }
>
> fclose($f);
> // write the response (if needed)
> return $result;
> } else {
> die ("Cannot connect !!!");
> }
> }
If you can use file_get_contents() with the allow_url_fopen =3D yes
directive in=20
php.ini it would be easier.
From http://us2.php.net/manual/en/wrappers.http.php :
$postdata =3D http_build_query(
array(
'var1' =3D> 'some content',
'var2' =3D> 'doh'
)
);
$opts =3D array('http' =3D>
array(
'method' =3D> 'POST',
'header' =3D> 'Content-type: =
application/x-www-form-urlencoded',
'content' =3D> $postdata
)
);
$context =3D stream_context_create($opts);
$result =3D file_get_contents('http://example.com/submit.php', false,
$context);
?>
I have done something similar except I used the $_REQUEST variable in
the=20
remote script rather than doing a POST. I had just a small amount of
data to=20
pass so $_REQUEST worked for me.
On Windows with PHP4 I had to do some tricks to get this type of thing
to=20
work. On Linux with PHP5 it worked flawlessly.
Thanks,
James
--=20
James Crow
--=20
Thanks for the example. But I found the error in my script,=20
"Content-Disposition: form-data; name=3D\"file\"; =
filename=3D\"$file\"\n".
I had to do a basename($file) first of course :)
-- jocke
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php