使用PHP中的Goutte / Guzzle发送带有方括号('[]')的发布请求

时间:2018-04-18 12:07:02

标签: php guzzle goutte square-bracket

我正在抓一个网站,我正在使用Goutte / Guzzle发帖子请求。我目前正在启用它,这是我的代码:

$cookies = $client->getRequest()->getCookies();
$response = $client->getClient()->post(
            self::URL_REQUEST_EXPORT_CREATION,
            array(
                    GuzzleHttp\RequestOptions::HEADERS      => array('Cookie' => 'X-Mapping-gkldneee=' . str_replace(' ', '+', $cookies['X-Mapping-gkldneee']) . '; admin_digitick=' . str_replace(' ', '+', $cookies['admin_digitick'])),
                    GuzzleHttp\RequestOptions::FORM_PARAMS  => array( 'req' => array(
                            'itemSelected'      => array(200, 201, 203, 207, 208, 220, 221, 222, 223, 240, 241, 242, 243, 244, 247, 258, 280, 281, 282, 283, 284, 285),
                            'critere'           => true
                    ))));
    $jsonResponse = json_decode($response->getBody(), true);

Here is the form data with the parameters expected by the request

我不知道guzzle如何处理空方括号,我已经尝试了很多方法在post请求中编写FORM_PARAMS。

有没有办法解决这种情况?提前谢谢。

1 个答案:

答案 0 :(得分:0)

我使用此代码找出解决方案:

$cookies = $client->getRequest()->getCookies();
    $chaineCookies = 'X-Mapping-gkldneee=' . str_replace(' ', '+', $cookies['X-Mapping-gkldneee']) . '; admin_digitick=' . str_replace(' ', '+', $cookies['admin_digitick']);
    $response = $client->getClient()->post(
            self::URL_AJAX_EXPORT_CATALOGUE,
            array(
                    GuzzleHttp\RequestOptions::HEADERS  => array(
                            'Cookie'        => $chaineCookies,
                            'Content-Type'  => 'application/x-www-form-urlencoded'
                    ),
                    GuzzleHttp\RequestOptions::BODY     => implode('&', array(
                            'req[itemSelected][]=200',
                            'req[itemSelected][]=201',
                            'req[itemSelected][]=203',
                            'req[itemSelected][]=207',
                            'req[itemSelected][]=208',
                            'req[itemSelected][]=220',
                            'req[itemSelected][]=221',
                            'req[itemSelected][]=222',
                            'req[itemSelected][]=223',
                            'req[itemSelected][]=240',
                            'req[itemSelected][]=241',
                            'req[itemSelected][]=242',
                            'req[itemSelected][]=243',
                            'req[itemSelected][]=244',
                            'req[itemSelected][]=247',
                            'req[itemSelected][]=248',
                            'req[itemSelected][]=250',
                            'req[itemSelected][]=251',
                            'req[itemSelected][]=253',
                            'req[itemSelected][]=254',
                            'req[itemSelected][]=255',
                            'req[itemSelected][]=256',
                            'req[itemSelected][]=257',
                            'req[itemSelected][]=258',
                            'req[itemSelected][]=280',
                            'req[itemSelected][]=281',
                            'req[itemSelected][]=282',
                            'req[itemSelected][]=283',
                            'req[itemSelected][]=284',
                            'req[itemSelected][]=285',
                            'req[critere]=true'
                    ))
            ));