2010-06-15 277 views
40

我有一個在線網關,它需要HTML表單提交隱藏字段。我需要通過一個PHP腳本來做到這一點沒有任何HTML表單(我有一個DB的隱藏字段中的數據)PHP - 通過POST重定向併發送數據

要通過GET爲此發送數據:

header('Location: http://www.provider.com/process.jsp?id=12345&name=John'); 

而要做到這一點發送數據通過POST?

回答

39

你不能做到這一點使用PHP。

正如其他人所說,你可以使用cURL - 但是然後PHP代碼成爲客戶端而不是瀏覽器。

如果您必須使用POST,那麼唯一的方法就是使用PHP生成填充表單並使用window.onload掛鉤來調用javascript來提交表單。

C.

+3

那麼奇怪的是,捲曲不是解決方案,因爲我自己就是這麼做的。但我想你的解決方案也適用。 – 2010-06-15 12:52:23

+0

您可以使用jQuery $ .post方法。在這種情況下,您可以選擇停留在本網站上的選項,或者在提交時重定向。 – Ervin 2012-11-27 10:24:16

+0

是的,它有可能使用PHP和curl代理請求並將其轉換爲POST,但這意味着接收端容易受到CSRF和/或未對請求應用基於會話的控制(您可以將一些這個,但不是所有的參數都通過捲曲) – symcbean 2015-10-26 11:06:32

1

對此使用捲曲。谷歌的「捲曲PHP的帖子」,你會發現這個:http://www.askapache.com/htaccess/sending-post-form-data-with-php-curl.html

請注意,您也可以使用CURLOPT_POSTFIELDS選項的數組。從php.net文檔:

要在HTTP「POST」操作中發佈的完整數據。要發佈文件,請使用@預先指定文件名並使用完整路徑。這可以作爲urlencoded字符串傳遞,如'para1 = val1 & para2 = val2 & ...'或作爲字段名稱作爲鍵和字段數據作爲值的數組。如果value是一個數組,則Content-Type頭將被設置爲multipart/form-data。

4

這將涉及cURL PHP擴展。

$ch = curl_init('http://www.provider.com/process.jsp'); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, "id=12345&name=John"); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER , 1); // RETURN THE CONTENTS OF THE CALL 
$resp = curl_exec($ch); 
+1

嘿馬特,這是CURL不可能的。通過捲曲,我們不會被重定向。 – rahul 2017-07-26 19:13:53

1

你要打開一個套接字到現場與fsockopen和模擬HTTP-POST請求。谷歌會告訴你很多片段如何模擬請求。

26

這裏是解決方法示例。

function redirect_post($url, array $data) 
{ 
    ?> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
     <script type="text/javascript"> 
      function closethisasap() { 
       document.forms["redirectpost"].submit(); 
      } 
     </script> 
    </head> 
    <body onload="closethisasap();"> 
    <form name="redirectpost" method="post" action="<? echo $url; ?>"> 
     <?php 
     if (!is_null($data)) { 
      foreach ($data as $k => $v) { 
       echo '<input type="hidden" name="' . $k . '" value="' . $v . '"> '; 
      } 
     } 
     ?> 
    </form> 
    </body> 
    </html> 
    <?php 
    exit; 
} 
+1

它是一個「髒」的解決方案,但絕對有效!我做了一個類似的功能,但用一個明確的'echo'而不是關閉並重新打開''標籤 – DiegoDD 2014-06-25 15:28:20

+0

我喜歡這種骯髒的東西。 php標籤可能需要設置爲<?php – Kemal 2015-06-17 13:22:07

+0

@DiegoDD:你有任何代碼/要點,以便我可以複製? – Hiep 2016-09-19 12:26:17

0

我用下面的代碼,以捕獲從form.php的提交POST數據,然後將其串聯到一個URL來回到形式發送審定改正。像魅力一樣工作,實際上將POST數據轉換爲GET數據。

foreach($_POST as $key => $value) { 
    $urlArray[] = $key."=".$value; 
} 
$urlString = implode("&", $urlArray); 

echo "Please <a href='form.php?".$urlString."'>go back</a>"; 
+0

不幸的是,這不是什麼問。他想通過重定向進行POST,而不是將POST轉換爲GET。 – TimWolla 2013-06-21 00:45:09

+1

此外,如果你想達到這個目的,你也可以使用PHP的本地函數'http_build_query()'。 – Ben 2013-12-23 19:48:57

2
/** 
* Redirect with POST data. 
* 
* @param string $url URL. 
* @param array $post_data POST data. Example: array('foo' => 'var', 'id' => 123) 
* @param array $headers Optional. Extra headers to send. 
*/ 
public function redirect_post($url, array $data, array $headers = null) { 
    $params = array(
     'http' => array(
      'method' => 'POST', 
      'content' => http_build_query($data) 
     ) 
    ); 
    if (!is_null($headers)) { 
     $params['http']['header'] = ''; 
     foreach ($headers as $k => $v) { 
      $params['http']['header'] .= "$k: $v\n"; 
     } 
    } 
    $ctx = stream_context_create($params); 
    $fp = @fopen($url, 'rb', false, $ctx); 
    if ($fp) { 
     echo @stream_get_contents($fp); 
     die(); 
    } else { 
     // Error 
     throw new Exception("Error loading '$url', $php_errormsg"); 
    } 
} 
+0

我是PHP新手。有沒有辦法改變它的URL?此功能不會更改網址,但會重定向。我將'$ headers'作爲'null'離開(我應該是什麼?)。 – 2015-07-19 02:14:03

8

,如果你想避免捲曲呼叫和重定向瀏覽器像正常和模仿POST調用另一種解決方案:

保存後,做一個臨時重定向:

function post_redirect($url) { 
    $_SESSION['post_data'] = $_POST; 
    header('Location: ' . $url); 
} 

然後經常檢查會話變量post_data

if (isset($_SESSION['post_data'])) { 
    $_POST = $_SESSION['post_data']; 
    $_SERVER['REQUEST_METHOD'] = 'POST'; 
    unset($_SESSION['post_data']); 
} 

會有一些缺少的組件,如apache_request_headers()不會顯示POST Content頭部等。

+0

感謝您提供最乾淨的解決方案。 – Gregzenegair 2017-08-25 11:12:10

1

備選地,重定向之前設置一個會話變量和在目的地URL進行測試,可以解決這個問題對我來說。

0

是的,你可以在PHP中做到這一點,例如在

的Silex或Symfony3

使用子請求

$postParams = array(
    'email' => $request->get('email'), 
    'agree_terms' => $request->get('agree_terms'), 
); 

$subRequest = Request::create('/register', 'POST', $postParams); 
return $app->handle($subRequest, HttpKernelInterface::SUB_REQUEST, false); 
0

舊的文章,但這裏是我如何處理它。使用newms87的方法:

if($action == "redemption") 
{ 
    if($redemptionId != "") 
    { 
     $results = json_decode($rewards->redeemPoints($redemptionId)); 

     if($results->success == true) 
     { 
      $redirectLocation = $GLOBALS['BASE_URL'] . 'rewards.phtml?a=redemptionComplete'; 
      // put results in session and redirect back to same page passing an action paraameter 
      $_SESSION['post_data'] = json_encode($results); 
      header("Location:" . $redirectLocation); 
      exit(); 
     } 
    } 
} 
elseif($action == "redemptionComplete") 
{ 
    // if data is in session pull it and unset it. 
    if(isset($_SESSION['post_data'])) 
    { 
     $results = json_decode($_SESSION['post_data']); 
     unset($_SESSION['post_data']); 
    } 
    // if you got here, you completed the redemption and reloaded the confirmation page. So redirect back to rewards.phtml page. 
    else 
    { 
     $redirectLocation = $GLOBALS['BASE_URL'] . 'rewards.phtml'; 
     header("Location:" . $redirectLocation); 
    } 
} 
0

一種解決方法至極完美的作品:

在源頁面,,開始打開會話並分配儘可能多的值你可能想。 然後做動遷用 「頭」:

<!DOCTYPE html> 
<html> 
    <head> 
     <?php 
      session_start(); 
      $_SESSION['val1'] = val1; 
      ... 
      $_SESSION['valn'] = valn; 
      header('Location: http//Page-to-redirect-to'); 
     ?> 
    </head> 
</html> 

,然後在圓盾頁:

<!DOCTYPE html> 
<?php 
    session_start(); 
?> 
<html> 
    ... 
    <body> 
     <?php 
      if (isset($_SESSION['val1']) && ... && isset($_SESSION['valn'])) { 
       YOUR CODE HERE based on $_SESSION['val1']...$_SESSION['valn'] values 
      } 
     ?> 
    </body> 
</html> 

無需使用Javascript,也沒有的jQuery .. 祝你好運!