开发者

How to forward/redirect an HTTP PUT Request with PHP?

开发者 https://www.devze.com 2022-12-29 08:08 出处:网络
I receive HTTP PUT requests on a server and I would like to redirect / forward these requests to an other server.

I receive HTTP PUT requests on a server and I would like to redirect / forward these requests to an other server.

I handle the PUT request on both server with PHP.

The PUT request is using basic HTTP authentication.

开发者_StackOverflow中文版

Here is an example :

www.myserver.com/service/put/myfile.xml

redirect to

www.myotherserver.com/service/put/myfile.xml

How can I do this without saving the file on my first server and resending a PUT request using CURL?

Thanks!


HTTP/1.1 defines status code 307 for such redirect. However, PUT is normally used by client software and you can pretty much assume no one honors 307.

The most efficient way to do this is to setup a proxy on Apache to redirect the request to the new URL.

This is how you can proxy it in PHP,

$data = file_get_contents('php://input');
$mem  = fopen('php://memory'); 
fwrite($mem, $data); 
rewind($mem);   
$ch = curl_init($new_url);                             
curl_setopt($ch, CURLOPT_PUT, true);  
curl_setopt($ch, CURLOPT_INFILE, $mem); 
curl_setopt($ch, CURLOPT_INFILESIZE, strlen($data)); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
curl_exec($ch);       
curl_close($ch);  
fclose($meme);


Not possible. A redirect is implicitly a GET request. You'll need to have to play for a proxy using curl.

Saving on disk is technically also not necessary, you could just pipe the reponse body directly to the request body of Curl. But since I've never done this in PHP (in Java it's a piece of cake), I can't give a more detailed answer about that.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号