【发布时间】:2020-01-28 13:45:44
【问题描述】:
我是 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="description" class="labelActivityDescription"><b>Description</b></label>
<textarea id="description" placeholder="Description..." name="description"></textarea>
<button type="submit"><b>Submit</b></button>
</form>
PHP CURL:
$url = "http://localhost:8080/myapi/actvities";
$username = 'user';
$password = 'user123';
$name = (isset($_POST['name']));
$description = (isset($_POST['description']));
$fields = array(
'name' => $name,
'description' => $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, 1);
curl_setopt($client, CURLOPT_POSTFIELDS, $fields);
$response = curl_exec($client);
curl_close($client);
【问题讨论】:
-
$name = (isset($_POST['name']));-isset返回一个布尔值,因此您没有在此处捕获 POST 参数的实际 值。 -
嗯好吧,我开始明白了,但是如果我删除了 isset,php 会抛出这个错误“notice: undefined index: name”,我该怎么做才能解决这个问题?
-
在
$response和curl_close($client);之间添加此代码echo curl_errno($client); echo curl_error($client);以查看错误。 -
注意:未定义索引 -> stackoverflow.com/a/4261200/10955263
-
$url = "localhost:8080/myapi/actvities";这是错字吗?