【问题标题】:PHP curl to POST form data to REST APIPHP curl 将表单数据发布到 REST API
【发布时间】:2020-01-28 15:19:40
【问题描述】:

我是 PHP 新手,我正在为我在大学的一门学科开发一个简单的客户端。该客户端的主要目标是将 CRUD 转换为 JAVA API。经过一番研究,我发现对于像这样的简单客户端,人们使用 CURL。我从未使用过 curl,我不知道我是否做错了什么。当我提交表单时,我收到此错误。

“注意:未定义的索引:名称”

“注意:未定义的索引:描述”

我能做些什么来解决这个问题?如果有人可以帮助我,我将不胜感激!

HTML 表单

<form class="form" action="createActivity.php">
    <label for="name" class="labelActivityName"><b>Name</b></label>
    <input type="text" id="name" placeholder="Name" name="name">

    <label for="comment" class="labelActivityDescription"><b>Description</b></label>
    <textarea id="description" placeholder="Description..." name="description"></textarea>

    <button type="submit"><b>Submit</b></button>
</form>

PHP

$url = "http://localhost:8080/tourism/api/activities";

$username = 'user';
$password = 'user123';

$fields = array(
    'name' => $_POST['name'],
    'description' => $_POST['description']
);

$client = curl_init();
curl_setopt($client, CURLOPT_URL, $url);
curl_setopt($client, CURLOPT_RETURNTRANSFER,1);
curl_setopt($client, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_setopt($client, CURLOPT_USERPWD, "$username:$password");
curl_setopt($client, CURLOPT_POST, count($fields));
curl_setopt($client, CURLOPT_POSTFIELDS, $fields);

$response = curl_exec($client);
curl_close($client);

【问题讨论】:

    标签: php html curl


    【解决方案1】:

    您应该使用表单方法 POST,因为默认情况下 from 方法是 GET 并且您已通过 $_POST 收集数据,请尝试以下代码:

    <form class="form" method="post" action="createActivity.php">
        <label for="name" class="labelActivityName"><b>Name</b></label>
        <input type="text" id="name" placeholder="Name" name="name">
    
        <label for="comment" class="labelActivityDescription"><b>Description</b></label>
        <textarea id="description" placeholder="Description..." name="description"></textarea>
    
        <button type="submit"><b>Submit</b></button>
    </form>
    

    【讨论】:

      【解决方案2】:

      您应该在表单标签中添加 method='post' 并在按钮标签中添加 name 属性

      <form class="form" method="post" action="createActivity.php">
                  <label for="name" class="labelActivityName"><b>Name</b></label>
                  <input type="text" id="name" placeholder="Name" name="name">
      
                  <label for="comment" class="labelActivityDescription"><b>Description</b></label>
                  <textarea id="description" placeholder="Description..." name="description"></textarea>
      
                  <button type="submit" name="submit"><b>Submit</b></button>
              </form>
      

      你的php代码应该是这样的-

             if(isset($_POST['submit']))
              {
              $url = "http://localhost:8080/tourism/api/activities";
      
              $username = 'user';
              $password = 'user123';
      
              $fields = array(
                  'name'=>($_POST['name']),
                  'description'=>($_POST['description'])
              );
      
              $client = curl_init();
              curl_setopt($client, CURLOPT_URL, $url);
              curl_setopt($client, CURLOPT_RETURNTRANSFER,1);
              curl_setopt($client, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
              curl_setopt($client, CURLOPT_USERPWD, "$username:$password");
              curl_setopt($client, CURLOPT_POST, count($fields));
              curl_setopt($client, CURLOPT_POSTFIELDS, $fields);
      
              $response = curl_exec($client);
              curl_close($client);
            }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-10
        • 1970-01-01
        • 1970-01-01
        • 2019-06-03
        • 2013-09-09
        相关资源
        最近更新 更多