在 Goutte 中发送具有相同参数名称的 post 请求

Sim*_*son 4 php httprequest symfony goutte

我正在抓取的一个站点对两个参数使用相同的名称,所以我想做这样的事情:

$params = array('dates' => '20140414', 'o' => '192382', 'o' => '213003' etc...);
$crawler = $client->request('POST', $url, $params);
Run Code Online (Sandbox Code Playgroud)

但是,由于数组中不可能有两个相同的键,因此我遇到了问题。是否有可能在 Goutte(Symfony 的 BrowserKit)中提出这样的请求?这是我想从 Chrome 的网络选项卡发出的确切请求的打印屏幕。

在此处输入图片说明

emb*_*emb 5

为了用 Goutte(或 Guzzle,它有同样的问题)做到这一点,您必须构建自己的表单 POST 请求,而不是使用$formParameters. 这需要手动设置 Content-Type 并将参数作为请求正文发送。

假设您要发送以下参数:

['foo' => 1, 'bar' => 2, 'bar'=> 3, 'baz' => 4]
Run Code Online (Sandbox Code Playgroud)

这就是你的代码的样子

$queryParams = [
    'foo=1',
    'bar=2',
    'bar=3',
    'baz=4',
];

$content = implode('&', $queryParams);

//This produces foo=1&bar=2&bar=3&baz=4

/** @var Goutte\Client $client */
$crawler = $client->request('POST', 'http://example.com/post.php', [], [], ['HTTP_CONTENT_TYPE' => 'application/x-www-form-urlencoded'], $content);
Run Code Online (Sandbox Code Playgroud)

请注意,参数和值必须经过 urlencoded。