PHP redirection with code 307 changes method from POST to GET

后端 未结 2 687
没有蜡笔的小新
没有蜡笔的小新 2021-01-22 01:02

In my PHP software I have an self-update feature which sends a HTTP request with the POST method to a certain URL (to a PHP script). Now this URL has changed (I moved the script

2条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-22 01:57

    It looks like file_get_contents does not repost the data, possibly for the reason highlighted by @daiscog.

    Curl however will repost to the redirected url:

    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, 'http://localhost/old/long/path/update.php');   
    curl_setopt($ch, CURLOPT_POST, true);    
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);  
    curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);  
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    
    curl_setopt($ch, CURLOPT_POSTFIELDS, $requestData);
    
    $serverResponse = curl_exec($ch);
    

    However it would make more sense to either handle this at the server level (eg an Apache url rewrite) or to simply include the new file in the old one:

    //old.php
    include('path/to/new.php');
    

提交回复
热议问题