I am attempting to perform a series of actions using php cURL requests to a RESTful API which returns JSON data. I have the first step working, which is to (GET) request a list of available resources and populate a dropdown with the results:
The request:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://cloud.skytap.com/templates",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"accept: application/json",
"authorization: Basic xxxxxxxxx",
"cache-control: no-cache",
"content-type: application/json",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$json = json_decode($response,true);
?>
The Dropdown:
<form action="select-action2.php" method="post">
<select name="users">
<option selected="selected">Select</option>
<?php
$users = $json;
foreach($users as $key => $value){
?>
<option value="<?php echo ($value["id"]); ?>"><?php echo $value["name"]; ?></option>
<?php
}
?>
</select>
<input type="submit" name="submit" value="Submit">
For the second step, I would like to pass the selected value from the dropdown to a new POST request on a second “action” page to perform an action using the selected id. To achieve this I try using the submit from the first dropdown to pass the selected value to the $tempID variable on the action page, then append this value to the url in the request on that action page:
<?php
$tempId = $_POST['users'];
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://cloud.skytap.com/configurations?template_id=".$tempId,
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(
"accept: application/json",
"authorization: Basic xxxxxxxx",
"content-type: application/json",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}
While the variable is being passed, and I can print the dropdown value to the screen on the action page, it does not seem to execute the request as the $response is not printed to the screen and no action appears to be taken on the account side.
I can use the code on the action page to successfully perform this action via Postman (and the request itself is generated via Postman), So I have to assume the code itself is okay. I am very new to this area, so a lot of it is educated guess work.
I have tried finding some answers online and checking similar questions, but am not sure exactly what I am looking for and have come up short. Any help is greatly appreciated.