I would like to use htmlspecialchars
to sanitize data before doing a POST
request but keep getting the error:
url=*** - Uncaught TypeError: http_build_query(): Argument #1 ($data) must be of type array, string given
This is the function related to this error and how it is getting triggered:
function makePostRequest($baseURL) {
$ch = curl_init();
$clean_post = htmlspecialchars($POST);
$data = http_build_query($clean_post);
curl_setopt($ch, CURLOPT_URL, $baseURL);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
if($e = curl_error($ch)) {
echo $e;
} else {
$json = json_decode($response, true);
return print_r($json);
}
}
...
$response = "";
switch (getRequestMethod()) {
case 'GET':
$response = makeGetRequest($baseURL);
break;
case 'POST':
$response = makePostRequest($baseURL);
break;
default:
echo "There has been an error";
return;
}
This is a sample of the data I am sending as part of the POST
request:
data = {
name:'***',
password: '***',
userID: emailAddress,
userSecret: password
}
console.log('data', data)
jQuery.ajax({
type: "POST",
url: "proxy.php?url=***",
dataType: "json",
contentType: 'application/x-www-form-urlencoded',
data: data,
success: function (data){
console.log('success', data)
}
});
});