在 PHP 中使用 DOMCrawler 从下拉框中选择一个选项


Select an option from dropdown box using DOMCrawler in PHP

我在PHP中使用DOMCrawler。我在下面有 HTML。我需要能够选择选项"文本1",然后提交表格。我有以下代码,但我似乎无法使其工作...我做错了什么?

use Goutte'Client;
$client = new Client();
$crawler = $client->request('GET', 'http://myURL');
$form = $crawler->selectButton('Text1')->form();   
$crawler2 = $client->submit($form);

这是 HTML:

<form action="something.php" name="frmOpcion" id="frmOpcion" method="post" enctype="multipart/form-data">
<select name="cmbOpcion" id="cmbOpcion" class="textoCmb">
<option value="a">Text1</option>
<option selected="selected" value="b">Text2</option>
</select>
<input type="image" name="imgOpcion" id="imgOpcion" alt="Send" title="Send" src="goTo.gif">
</form>

文档给出了这个例子:

// Select an option or a radio
$form['country']->select('France');

若要使示例适应你的情况,请先选择窗体。请注意,selectButton()用于按钮和输入,而不是选择控件:

$form = $crawler->selectButton('imgOpcion');

接下来,设置选择的值:

$form->select('Text1');

最后,提交表格:

$client->submit($form)