如何使用 Zend_Http_Client 模拟 POSTing 多个复选框表单字段 myField[]?

发布于 2024-12-04 04:59:10 字数 1251 浏览 4 评论 0原文

我正在使用 Zend_Http_Client 将一组数据 POST 到运行 PHP 的服务器。但是,服务器需要 myField[] 形式的数据,即我有一组复选框,用户可以选中多个复选框。我当前的代码是:

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

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


更新

我实际上已经找到了一种方法来做到这一点,通过破解 Zend_Http_Client 代码本身。然而这并不理想。我是这样做的:

首先,我简单地将值添加到 POST 字段,如下所示:

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

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

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

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

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

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

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

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

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

我宁愿只使用 Zend_Http_Client 而不对库代码进行更改,但这目前有效。我将非常感谢任何关于如何在不破坏库的情况下做到这一点的建议。

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

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

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:

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

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

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

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

禾厶谷欠 2024-12-11 04:59:10

最简单的方法可能就是自己设置原始帖子正文:

$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

如果您还有其他变量要在 postdata 中发送,您可能需要子类化 Zend_Http_Client 并重写 _prepareBody() 如下。

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

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

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

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.

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

Usage

$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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文