2012-02-21 116 views
1

我想發送一個URL重定向到請求頁面的發佈請求。 Jest認爲請求頁面當前用於處理提交表單。使用Zend_Http_Client發送郵件請求

我試過以下代碼到我的請求頁面。

<?php 

$myFile = "test.txt"; 
$fh = fopen($myFile, 'w') or die("can't open file"); 
$stringData = "Request come to the page\n"; 
fwrite($fh, $stringData); 

if (isset($_POST)) { 
     $stringData = $_POST['user']; 
     fwrite($fh, $stringData); 
} 

fclose($fh); 
echo 'page found'; 

我試過Zend_Http_Client這樣做。這是我的簡單行動。

public function indexAction() 
{ 
    $client = new Zend_Http_Client('http://localhost/test/test.php'); 

    $client->setParameterPost(array('user' => 'dinuka')); 
    $client->setConfig(array('strictredirects' => true)); 

    $response = $client->request(Zend_Http_Client::POST); 
} 

現在請求發送到test.php。但不能重定向到http://localhost/test/test.php頁面。我想發送請求+重定向。我之前曾問過這個問題。但它是純粹的問題。請幫幫我。我沒有很好的關於http請求的知識。什麼是strictredirects

+0

你能否澄清你的問題? 「通過網址重定向發送請求頁面的發佈請求」是什麼意思? – Liyali 2012-02-21 13:33:10

+0

@Liyali - 當您提交表單時,發送參數以形成動作URL並重定向到該URL。我也想這樣做,但是沒有形式。 – 2012-02-22 04:20:09

+0

如果您始終想將您的操作重定向到/test/test.php,那麼爲什麼不使用重定向器來做呢? – Liyali 2012-02-22 04:35:15

回答

1

我認爲這可能涉及你想要什麼的基本知識:

<?php 

class IndexController extends Zend_Controller_Action { 

    public function init() { 
    } 

    public function indexAction() { 
     //set form action to indexController nextAction 
     $form = My_Form(); 
     $form->setAction('/index/next'); 
     //asign form to view 
     $this->view->form = $form; 
    } 

    public function nextAction() { 
     //if form is posted and valid 
     if ($this->getRequest()->isPost()) { 
      if ($form->isValid($this->getRequest()->getPost())) { 
       $data = $form->getValues(); 

       //Do some stuff 
      } 
     } else { 
      //if form not posted go to form in indexAction 
      $this->_forward('index'); 
     } 
    } 
} 

它好像這是你的問題的形式,但你真的想知道的是如何在一個重定向發送POST數據,而不是獲取數據,如果我在正確的軌道上請 評論我們如何改變這種情況,以幫助您解決您的問題。

public function indexAction() 
{ 
    $client = new Zend_Http_Client('http://localhost/test/test.php'); 

    $client->setParameterPost(array('user' => 'dinuka')); 
    $client->setConfig(array('strictredirects' => true)); 

    $response = $client->request(Zend_Http_Client::POST); 
} 

我的想法,你想用這段代碼是什麼提交POST請求到這個網址和瀏覽器在同一時間有重定向。
這應該是可能的,根據我可以找到它應該是像$response->location一樣簡單,但我不能讓它工作。 ->location應該是一個標題元素,但我找不到它。也許我們其他的大師可以幫忙。

+0

我可以在發送post請求後重定向,但它並不是真的需要,當調用$ client-> request(Zend_Http_Client :: POST)時,它也應該重定向,我不知道我是怎麼解釋這個的 – 2012-02-22 09:08:33

+0

認爲@Liyali可能有這個原因,這可能不起作用,很可能大多數服務器都默認不允許這種類型的活動(瀏覽器可能是相同的),我甚至無法在自己的服務器上運行它。嘗試隱藏的表單方法。 – RockyFord 2012-02-23 06:48:18