尝试测试执行图片上传的表单,并有一些隐藏字段,如下所示:
<form class="add" action="https://example.com/stSPyOCwDVg" method="post" enctype="multipart/form-data">
<input type="hidden" name="tokenCode" value="U2FsdGVkX18zMTY1MjMxNuUCJfrsNa-cT0yap3xGbgrDN6RkCLpTbOm8JLusrW1vGGQxqAdYYdE">
<input type="file" name="file" multiple>
<button class="addbtn" type="submit" name="go">add image</button>
</form>
期待提交类似于非多部分表单,但事实并非如此。而是按如下方式尝试POST:
// load the form page
$client = new Client();
$crawler = $client->request('GET', 'https://example.com/form');
// uploading params and files as per docs
$form = $crawler->selectButton('add image')->form(); // locate the upload form by button name
$post_url = $form->getUri(); // 'action' attrib on the form
$params = $form->getValues(); // grab hidden fields
$files = ['/tmp/UL_img1', '/tmp/UL_img2', '/tmp/UL_img3'];
$crawler = $client->request('POST', $post_url, $params, $files);
echo $crawler->html();
这只返回原始表单,没有上传文件。有谁知道这是否被正确调用?
答案 0 :(得分:0)
这可能不是理想的解决办法,但这确实有效:
$form = $crawler->selectButton('add image')->form();
$files = ['/tmp/UL_img1', '/tmp/UL_img2', '/tmp/UL_img3']
foreach($files as $file) {
$form['file'] = $tmpfname; // this was key
$browser = $client->submit($form);
}
基本上可以将文件名分配给“文件”输入。这样我们就不必担心正确的POST url / hidden attribs / cookeis与请求一起发送。
同一请求中的多个文件尚不可用。将更新。