如何使用Zend_Http_Client模拟posting多个复选框表单字段myField []? [英] How to emulate POSTing multiple checkbox form fields myField[] with Zend_Http_Client?

查看:183
本文介绍了如何使用Zend_Http_Client模拟posting多个复选框表单字段myField []?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Zend_Http_Client将一组数据发送到运行PHP的服务器。但是,服务器期望以 myField [] 形式的数据,即我有一组复选框,用户可以检查多个。我目前的代码是:

I am using Zend_Http_Client to POST a set of data to my server running PHP. However, the server is expecting data in the form myField[], i.e. I have a set of check boxes and the user can check more than one. My current code is:

   foreach ($myValues as $value) {
    $this->client->setParameterPost('myField[]', $value);
   }

但是,似乎Zend_Http_Client只是覆盖 myField [] 每次循环时都带有新值。如何使用Zend_Http_Client添加同名的多个POST字段?

However, it seems that Zend_Http_Client is simply overwriting myField[] with the new value each time it goes through the loop. How can I add multiple POST fields of the same name using Zend_Http_Client?

UPDATE

我实际上通过黑客攻击Zend_Http_Client代码找到了一种方法。然而,这并不理想。我是这样做的:

I have actually figured out a way to do this, by hacking the Zend_Http_Client code itself. However this is not ideal. Here's how I did it:

首先,我只是将值添加到POST字段中,如下所示:

First, I simply added the values to the POST fields like this:

$myValues = array(0,1,2);
$this->client->setParameterPost('myField', $myValues);

在函数 _prepareBody()中,Zend_Http_Client使用以下代码构建POST数据:

In the function _prepareBody(), Zend_Http_Client builds the POST data with the following code:

  $body = http_build_query($this->paramsPost, '', '&');

如果查看它构建的POST数据,它看起来像这样:

If you look at the POST data that it builds, it looks like this:

myField[0]=0&myField[1]=1&myField[2]=2

当然,它是网址编码的,所以它看起来像这样:

Of course, it is url-encoded, so it looks like this:

myField%5B0%5D=0&myField%5B1%5D=1&myField%5B2%D=2

所以,我刚刚添加了一个 preg_replace 来制作[0] - > [],[1] - > []等:

So, I just added a preg_replace to make [0] -> [], [1] -> [], etc:

$body = preg_replace('/%5B(?:[0-9]|[1-9][0-9]+)%5D=/', '%5B%5D=', $body);

我宁愿只使用Zend_Http_Client而不更改库代码,但现在这样做。我非常感谢有关如何在不破解库的情况下进行操作的任何建议。

I'd rather just use Zend_Http_Client without making changes to the library code, but this works for now. I'd be very grateful for any suggestions on how to do it without hacking the libraries.

推荐答案

最简单的方法可能就是自己设置原始邮政体:

The simplest approach may be just to set the raw post body yourself:

$values = array(
    0,
    1,
    2,
);

$key = 'myField';
$rawData = '';
foreach ($values as $value) {
    if ($rawData !== '') {
        $rawData .= '&';
    }
    $rawData .= $key . '%5B%5D=' . $value;
}

$client = new Zend_Http_Client();
$client->setRawData($rawData);
$client->setUri('http://www.davidcaunt.co.uk/');
$client->request(Zend_Http_Client::POST);

$request = $client->getLastRequest();

//Zend_Debug::dump($request);
Zend_Debug::dump(urldecode($request));

Postdata


myField [] = 0& myField [] = 1& myField [] = 2

myField[]=0&myField[]=1&myField[]=2

如果您要发送其他变量postdata,你可能想要继承Zend_Http_Client并覆盖 _prepareBody()的实现,如下所示。

If you have other variables to send in the postdata, you'll probably want to subclass Zend_Http_Client and override the implementation of _prepareBody() as follows.

此修改旨在保持与未来更新兼容,因此,除非设置了POST参数,并且表单不是多部分(文件上载),因此调用父方法:

This modification aims to remain compatible with future updates, and as such, calls the parent method unless POST params are set, and the form is not multipart (a file upload):

class My_Http_Client extends Zend_Http_Client
{

    function _prepareBody() 
    {
        if (count($this->paramsPost) > 0 && $this->enctype == self::ENC_URLENCODED) {
            $this->setHeaders(self::CONTENT_TYPE, self::ENC_URLENCODED);

            $body = '';
            foreach ($this->paramsPost as $key => $value) {

                if (is_array($value)) {
                    foreach ($value as $v) {
                        $body .= $key . '%5B%5D=' . $v . '&';
                    }
                } else {
                    $body .= $key . '=' . $value . '&';
                }               
            }

            return rtrim($body, '&');
        }

        return parent::_prepareBody();
    }
}

用法

$client = new My_Http_Client();
$client->setParameterPost('name', 'John');
$client->setParameterPost('myField', array(0,1,2));
$client->setUri('http://www.davidcaunt.co.uk/');
$client->request(Zend_Http_Client::POST);

$request = $client->getLastRequest();

Zend_Debug::dump(urldecode($request));

这篇关于如何使用Zend_Http_Client模拟posting多个复选框表单字段myField []?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆