2012-02-21 147 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