Skip to content
Advertisement

Sending array having space in keys being removed when sending by cURL

I have a strange issue.

$arr = array(
  'my key 1' => 'Some data 1',
  'my_key_2' => 'Some data 2',
  'my key 3' => 'Some data 3',
);

I am sending this data using cURL …

$curl = curl_init($url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($curl);
curl_close($curl);

On receiveing end, I am just printing it back.

echo print_r($_POST);

Which prints it out as

array(
  [0] => 0,
  [my_key_2] => Some data 2,
)

It’s removing array keys with spacing.

Any ideas, what could be causing it?

Advertisement

Answer

Your spaces in params are replaced with + when you use http_build_query(), try providing an array without this function when you set the CURLOPT_POSTFIELDS option.

You can read about it in docs… https://www.php.net/manual/ru/function.http-build-query.php#95784

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement