Php 使用cURL将表单数据发布到服务器
我有一张简单的表格。。用户将在其中输入数据,我希望在单击“保存”时将数据发送到服务器 这是我的表格:Php 使用cURL将表单数据发布到服务器,php,json,curl,Php,Json,Curl,我有一张简单的表格。。用户将在其中输入数据,我希望在单击“保存”时将数据发送到服务器 这是我的表格: <form method="POST" name="admin_form" action=" "> <div class="form-row"> <div class="form-group col-md-12"> &
<form method="POST" name="admin_form" action=" ">
<div class="form-row">
<div class="form-group col-md-12">
<label for="inputEmail4">Product Name</label>
<input type="name" class="form-control" name="name" id="inputproductname"
placeholder="Product name">
</div>
<div class="form-group col-md-12">
<label for="inputPassword4">Description</label>
<textarea class="form-control" name="description" id="exampleFormControlTextarea1"
rows="3"></textarea>
</div>
</div>
<!-- start select shop -->
<div class="form-group">
<label for="exampleFormControlSelect2">Select shop</label>
<?php
$q = "SELECT * FROM heroku_50fc799819d.shops ORDER BY name ASC";
$r = mysqli_query($conn,$q);
?>
<select class="form-control" name="shops" id="shops" required>
<!-- <option value="0">Select category.</option> -->
<?php while ($data = mysqli_fetch_array($r)){
$id = $data['id'];
$name = $data['name'];
?>
<option value="<?php echo $id;?>"> <?php echo $name; ?> </option>
<?php } ?>
</select>
</div>
<!-- end shop select -->
<!-- image select -->
<form>
<div class="form-group">
<label for="exampleFormControlFile1">Upload image product</label>
<input type="file" class="form-control-file" id="exampleFormControlFile1">
</div>
</form>
<!-- end image select -->
<!-- start category select -->
<div class="form-group">
<label for="exampleFormControlSelect1">Select one or multiple categories</label>
<?php
$q = "SELECT * FROM heroku_50fc799819d.categories ORDER BY name ASC";
$r = mysqli_query($conn,$q);
?>
<select multiple class="form-control" name="category" id="product_category" required>
<!-- <option value="0">Select category.</option> -->
<?php while ($data = mysqli_fetch_array($r)){
$id = $data['id'];
$name = $data['name'];
?>
<option value="<?php echo $id;?>"> <?php echo $name; ?> </option>
<?php } ?>
</select>
</div>
<!-- end category select -->
<div class="form-group">
<label for="inputPrice">Price</label>
<input type="text" class="form-control" name="price" id="inputPrice" placeholder="12000">
</div>
<div class="form-group">
<label for="inputPrice">Discount</label>
<input type="text" class="form-control" name="discount" id="inputPrice" placeholder="10">
</div>
<div class="form-group">
<label for="inputPrice">Qty</label>
<input type="text" class="form-control" name="qty" id="inputPrice" placeholder="10">
</div>
<button type="submit" name="submit" class="btn btn-primary">
Save
</button>
</form>
我当前正在从服务器返回此错误消息。那么,我如何通过单击save来传递表单中的数据呢??
{“错误”:“未传递任何产品名称”}这是否回答了您的问题?是的,我找到了如何处理错误的答案…所以我的问题解决了一半。。。。问题是当用户单击save(保存)按钮时,如何从表单中获取变量值在您尝试使用json上传图像时-图像是二进制数据,json不是二进制安全的,如果您处理二进制数据,请不要使用json,使用
多部分/表单数据
或应用程序/x-www-form-urlencoded
,它们都完全能够处理二进制数据(如图像)(多部分/表单数据的带宽开销低于x-www-form-urlencoded tho,后者在最坏的情况下每字节二进制数据的开销约为66%,多部分/表单数据每字节的开销约为0%,它只有一些头开销,但没有每字节的开销)@hanshenrik非常好的观察结果…实际上我计划将图像发送到firebase存储并从firebase收集图像URL,这就是我将发送的内容-暂时忽略图像位..关于如何发送表单数据的任何建议…我对此非常了解
$post_data = new \stdClass();
$final_post_data = new \stdClass();
$post_data ->name = $_POST['name']?? '';
$post_data ->description = $_POST['description']?? '';
$post_data ->shopId = $_POST['shops']?? '';
$post_data ->categories = [2, 16, 278];
$post_data ->price = $_POST['price']?? '';
$post_data ->quantity = $_POST['qty']?? '';
$post_dataJSON = json_encode($post_data);
$final_post_data ->body = $post_dataJSON;
$final_post_dataJSON = json_encode($final_post_data);
//initialize
$ch = curl_init('https://admin2go.herokuapp.com/api/admin/products/create');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
//adding the post variable to the request
curl_setopt($ch, CURLOPT_POSTFIELDS, $final_post_dataJSON);
//Return output instead of outputting it
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($final_post_dataJSON)
)
);
$result = curl_exec($ch);
if ($result === FALSE) {
echo "cURL Error: " . curl_error($ch);
}
//close and free up the curl handle
curl_close($ch);
//Display row output
print_r($result);
?>