O
O
oleamm2010-10-21 03:12:07
PHP
oleamm, 2010-10-21 03:12:07

PHP: send file downloaded from server1 with post request to server2?

So, for example, there is a picture on server1 server1.ru/file1.jpg
It is necessary to send this file to server2 (server2.ru) with a POST request.
How to do it as quickly and easily as possible?
My suggestion:
To download file1.jpg use:

$fp = @fopen('http://server1.ru/file1.jpg', &quot;rb&quot;);<br/>
 $fd = @fopen('tmp/tmp_filename', &quot;w&quot;);<br/>
 if ($fp && $fd) {<br/>
 while (!feof($fp)) {<br/>
 $st = fread($fp, 4096);<br/>
 fwrite($fd, $st);<br/>
 }<br/>
 }<br/>
 @fclose($fp);

We then use CURL to send the tmp/tmp_filename file to server2.
Perhaps there is a faster way?
ps: it would be great to do all of the above on the client side using some jQuery and not load the server with this nonsense =)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mithgol, 2010-10-21
@Mithgol

// читаем файл
$fileString = file_get_contents('http://server1.ru/file1.jpg');

// подготавливаем POST
$params = array('http' => array(
   'method' => 'POST',
   'content' => $fileString
));
$context = stream_context_create($params);

// посылаем POST, получаем отклик
if($remote = @fopen('http://server2.ru/filesend.php', 'rb', false, $context)){
   $response = @stream_get_contents($remote);
}

Original method source: " HTTP POST from PHP, without cURL " (By Wez Furlong, code posted Nov 15, 2006, updated May 23, 2010).

S
Sergey Beresnev, 2010-10-22
@sectus

Interestingly, two servers and the interaction between them are mentioned first. And in the end, I remember about some kind of “client”.
Maybe you don't need to bother with these requests at all. Give the second recipient server a link to the image - let it download it. This way, the scripts of the donor server will not be loaded (if you give a direct link to the image), and you can organize a queue for downloading images from the first server on the second server if there are a lot of such images downloaded at the same time.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question