1

I'm currently trying to send a post request with PHP with duplicated parameter, here's what I have done so far.

$searchData = array(

    'ListShow' => 'ListShow',
    'dspSsuPd' => '200', //rows per page
    // 'szkbuChkbx' => json_encode($arrsearhchbx),
    'szkbuChkbx[]' => array('011',
    '012',
    '111',
    '112'),
    'lstDspPg' => $n //pages
);
$searchData = http_build_query($searchData);

//search with cookie and data to get result
if ($cookie == '') {
    echo 'cookie is empty';
} else {
    // echo $cookie . "\n";
    $searchResult = postRequest($searchActionURL, $cookie, $searchData);
    // echo ($searchResult);

The postRequest function:

function postRequest($url, $cookie, $params)
{
    //echo $cookie;
    $curl = curl_init();
    curl_setopt_array($curl, array(
        CURLOPT_URL => $url,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_ENCODING => "",
        CURLOPT_MAXREDIRS => 10,
        CURLOPT_TIMEOUT => 30,
        CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
        CURLOPT_CUSTOMREQUEST => "POST",
        CURLOPT_HTTPHEADER => array(
            "cache-control: no-cache",
            "content-type: application/x-www-form-urlencoded",
            "postman-token: 2c3803ce-d9e9-d7b5-4ba6-cb0313cd1e22",
            "user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36",
            "Cookie: " . $cookie,
        ),
    ));
    curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
    $response = curl_exec($curl);
    $err = curl_error($curl);
    curl_close($curl);

    if ($err) {
        echo "cURL Error #:" . $err;
        return false;
    } else {
        return $response;
    }
} 

I have referred to the manual and they did say that CURLOPT_POSTFIELDS don't mix well with curl_setopt_array() so I used curl_setopt() for that specific option to no avail. There is also a post that talked about this: How do I use arrays in cURL POST requests I tried following but it still doesn't work and only send the last element of szkbuChkbx. Anyone have any idea how can I achieve this?

  • you can't have duplicate keys in an array which is essentially what you are trying to do – Professor Abronsius May 18 '23 at 06:34
  • @TranMinhQuan Try to remove `[]` in the line `'szkbuChkbx[]'` and tell what you are facing. –  May 18 '23 at 06:40
  • @ProfessorAbronsius The parameter I'm wanting to send is the szkbuChkbx, I'm essentially crawling a website and when I look at the body of the get request of that website they have szkbuChkbx: 011 szkbuChkbx: 012 szkbuChkbx: 008 szkbuChkbx: 111 szkbuChkbx: 112 szkbuChkbx: 113 szkbuChkbx: ETF szkbuChkbx: ETN szkbuChkbx: RET szkbuChkbx: IFD szkbuChkbx: 999 So I put them in an array like above but it only accepts the last one in that array. – Tran Minh Quan May 18 '23 at 06:41
  • @PracticalOpportunist I have tried that and it gives me nothing. With [] at least they accept the last element. – Tran Minh Quan May 18 '23 at 06:42
  • Different problem, same issue: I built a string instead of an array then split it on the receiving end. Is that an option for you? i.e. `szkbuChkbx:123:234:345` – OldPadawan May 18 '23 at 07:49

0 Answers0