user10108953
user10108953

Reputation:

How to set up _GET in arrays

$data2 = [
    'a' => '(!isset($_GET["a"])) ? "default" : htmlspecialchars($_GET["a"]);',
    'b' => '-(!isset($_GET["b"])) ? "default" : htmlspecialchars($_GET["b"]);',
    'c' => '(!isset($_GET["c"])) ? "default" : htmlspecialchars($_GET["c"]);',
    'd' => '(!isset($_GET["d"])) ? "default" : htmlspecialchars($_GET["d"]);',
];

curl_setopt($ch, CURLOPT_URL, "post request url");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data2));
curl_setopt($ch, CURLOPT_POST, 1);
$x = curl_exec($ch);
echo $x;

i use this in a php that calls a POST url with the following request how would i go about setting it up so i can edit 1 php for all like put the file.php?a=28.29.29&b=1232.231.121&c=2121&d=8282832 and it will fill out the post request spots for me any advice would be great

here is how it normally looks manually

$data2 = [
    'a' => '228.29.29',
    'b' => '-1232.231.121',
    'c' => '2121',
    'd' => '8282832',

];

Upvotes: 1

Views: 46

Answers (1)

Binit Ghetiya
Binit Ghetiya

Reputation: 1989

As per your question seems like there is syntax error, please replace $data with below code and it should be working fine.

$data2 = [
'a' => (!isset($_GET["a"])) ? "default" : htmlspecialchars($_GET["a"]),
'b' => (!isset($_GET["b"])) ? "default" : "-" .htmlspecialchars($_GET["b"]),
'c' => (!isset($_GET["c"])) ? "default" : htmlspecialchars($_GET["c"]),
'd' => (!isset($_GET["d"])) ? "default" : htmlspecialchars($_GET["d"]),
 ];

curl_setopt($ch, CURLOPT_URL, "post request url");
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data2));
curl_setopt($ch, CURLOPT_POST, 1);
$x = curl_exec($ch);
echo $x;

Upvotes: 2

Related Questions