duanji6997 2012-02-21 10:04
浏览 34

使用Zend_Http_Client发送发布请求

I want to send a post request with a url redirect to a request page. Jest think request page currently use for handle submit form.

I tried the following code to my request page.

<?php

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

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

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

I tried Zend_Http_Client to do that. This is my simple action.

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); 
}

Now request send to the test.php. But not redirect to the http://localhost/test/test.php page. I want send request + redirect. I asked this question previously. But it closed as pure question. Please help me. I haven't good knowledge about http request. What is the strictredirects?

  • 写回答

1条回答 默认 最新

  • duanjia4097 2012-02-22 06:00
    关注

    I think this might cover the basics of what you want:

    <?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');
            }
        }
    }
    

    it seems as though this the form of your question, but what you really want to know is how send post data in a redirect instead of get data, if I'm on the right track please comment on how else we can change this to help you solve your problem.

    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); 
    }
    

    I get the idea that what you want with this piece of code is to submit a post request to this url and to redirect your browser there at the same time.
    This should be possible and according to what I can find it is supposed to be as simple as $response->location but I can't make it work. ->location is supposed to be a header element but I can't find it. Maybe one of our other gurus can help.

    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?