duanliusong6395 2017-03-09 12:18
浏览 57

在页面加载时执行PHP cURL POST请求

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 程序不包含适用于入口点的静态Main方法
    • ¥15 素材场景中光线烘焙后灯光失效
    • ¥15 请教一下各位,为什么我这个没有实现模拟点击
    • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
    • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
    • ¥20 有关区间dp的问题求解
    • ¥15 多电路系统共用电源的串扰问题
    • ¥15 slam rangenet++配置
    • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
    • ¥15 ubuntu子系统密码忘记