How to redirect / redirect an HTTP PUT request using PHP?

I am receiving HTTP PUT requests on a server and I would like to redirect / forward these requests to another server.

I am handling a PUT request on a server with PHP.

The PUT request uses basic HTTP authentication.

Here's 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 to my first server and resubmitting the PUT request with CURL?

Thanks!

+2


a source to share


2 answers


HTTP / 1.1 defines a 307 status code for such a redirect. However, PUT is commonly used by client software and you can pretty much assume that no one deserves 307 points.

The most efficient way to do this is to configure a proxy server in Apache to redirect the request to a 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);

      

+5


a source


Impossible. A redirect is implicitly a request GET

. You will need to play as a proxy using curl

.



Saving to disk is not technically necessary either, you can simply send the response body directly into the Curl request body. But since I've never done this in PHP (it's a piece of cake in Java), I can't give a more detailed answer on this.

+2


a source







All Articles