How to emulate POSTing multiple form fields of the myField [] checkbox with Zend_Http_Client?

I am using Zend_Http_Client to send a dataset to my server with PHP. However, the server expects data in the form of myField[] , that is, I have a set of flags, and the user can check several. My current code is:

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

However, it seems that Zend_Http_Client simply overwrites myField[] with a new value each time it goes through the loop. How to add multiple POST fields with the same name using Zend_Http_Client?


UPDATE

I really figured out a way to do this by cracking the Zend_Http_Client code itself. However, this is not ideal. Here is how I did it:

At first, I simply added the values ​​to the POST fields as follows:

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

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

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

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

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

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

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

So, I added a preg_replace to do [0] β†’ [], [1] β†’ [], etc:

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

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

+2
source share
1 answer

The easiest way is to simply set the body of the original message 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 p>

MyField [] = 0 & MyField [] = 1 & MyField [] = 2

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

This modification is intended to remain compatible with future updates and, as such, calls the parent method if POST parameters are not specified and the form is not multiple (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(); } } 

Using

 $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)); 
+3
source

Source: https://habr.com/ru/post/986714/


All Articles