I have an annoying form that I can use with my web browser to POST to, that I cannot replicate using guzzle at the moment.
The form requires the content-type to be multipart/form-data.
Using firebug I can see that the form request/response headers and body look like this:
Accept:
text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:
gzip,deflate,sdch
Accept-Language:
en-US,en;q=0.8
Cache-Control:
max-age=0
Connection:
keep-alive
Content-Length:
849
Content-Type:
multipart/form-data; boundary=----WebKitFormBoundaryy0aDN3TK2YELUZ09
Cookie:
auth=5e6b3363d9c787f54abe9da01563e6bb
DNT:
1
Host:
Origin:
Referer:
User-Agent:
Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.48 Safari/537.36
- Request Payload
- ------WebKitFormBoundaryy0aDN3TK2YELUZ09
Content-Disposition: form-data; name="state"
5
------WebKitFormBoundaryy0aDN3TK2YELUZ09
Content-Disposition: form-data; name="blank"
0
------WebKitFormBoundaryy0aDN3TK2YELUZ09
Content-Disposition: form-data; name="aircraftType"
A320
------WebKitFormBoundaryy0aDN3TK2YELUZ09
Content-Disposition: form-data; name="airport"
5
------WebKitFormBoundaryy0aDN3TK2YELUZ09
Content-Disposition: form-data; name="print"
sf-1204
------WebKitFormBoundaryy0aDN3TK2YELUZ09
Content-Disposition: form-data; name="print"
sf-1206
------WebKitFormBoundaryy0aDN3TK2YELUZ09
Content-Disposition: form-data; name="print"
sf-1015
------WebKitFormBoundaryy0aDN3TK2YELUZ09
Content-Disposition: form-data; name="action_print"
Open Selected Information
------WebKitFormBoundaryy0aDN3TK2YELUZ09--
- Response Headersview source
Cache-Control:
must-revalidate, post-check=0, pre-check=0
Connection:
Keep-Alive
Content-disposition:
attachment;filename=output.pdf
Content-Type:
application/pdf
Date:
Thu, 14 Nov 2013 13:01:45 GMT
Keep-Alive:
timeout=5, max=100
Pragma:
public
Server:
Apache
Transfer-Encoding:
chunked
In my class I have attempted to recreate this using the following code:
$request = $this->client->post(
array('Content-Type'=>'multipart/form-data'),
array(
'state' => 5,
'blank' => 1,
'aircraftType' => 'A320,
'airport' => $airport,
'print' =>'sf-1204',
'print' =>'sf-1206',
'print' =>'sf-1015',
'action_print' => 'Open Selected Information'
)
);
$response = $request->send()->getBody(true);
This actually works - in that I do get a response back (pdf file) but the major problem is that due to the fact there are 3 "print" variables to be sent (sf-204, sf-1206, sf-1015) - you obviously can't have 3 "print" keys in an array, so only one is sent to the server.
Just how can I create the multipart/form-data message body on my request in the same way that the browser can do. I'm a little lost and need a push in the right direction!
Thanks for any help!