ป้ายกำกับ: เมธอด

curl: ส่ง ฟอร์มแบบ postcurl: ส่ง ฟอร์มแบบ post

หลังจากตัวอย่าง curl: ส่ง ฟอร์มแบบ get เรามาลองส่งข้อมูลแบบโพสต์กันต่อ เว็บไซต์ส่วนใหญ่จะนิยมส่งค่าในแบบฟอร์มแบบ post มากกว่าแบบ get เพราะว่า url จะดูสวยงาม ไม่เละเทะ ดูแล้วสบายตา และปลอดภัยกว่าการส่ข้อมูลให้เห็นง่าย ๆ แบบใช้ url

<!doctype html>
<html>

<head>
 <meta charset="utf-8">
 <title>CURL: send post variables</title>
 <link href="../vendor/twbs/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet" type="text/css">
</head>

<body>
 <div class="container">
 <?php
if (count ($_POST)) {
 $url = 'http://localhost/snippets/PHP/variables.php';

 $ch = curl_init () ;

 curl_setopt_array ($ch, [
 CURLOPT_ENCODING => 'UTF-8',
 CURLOPT_POST => 1,
 CURLOPT_POSTFIELDS => http_build_query ($_POST) ,
 CURLOPT_RETURNTRANSFER => true,
 CURLOPT_URL => $url,
 ]) ;

 $result = curl_exec ($ch) ;
 curl_close ($ch) ;

 echo $result;
}
?>
 <form action="curl_post.php" enctype="multipart/form-data" method="post">
 <div class="form-group">
 <label for="name">Name:</label>
 <input class="form-control" id="name" name="name" type="text">
 </div>
 <div class="form-group">
 <label for="avatar">Avatar:</label>
 <input accept="image/gif, image/jpeg, image/x-png" class="form-control" id="avatar" name="avatar" type="file">
 </div>
 <div class="form-group">
 <label for="address1">text address:</label>
 <input class="form-control" id="address1" name="address[]" type="text">
 </div>
 <div class="form-group">
 <label for="address2">text address 2:</label>
 <input class="form-control" id="address2" name="address[]" type="text">
 </div>
 <button type="submit" class="btn btn-default">Submit</button>
 </form>
 </div>
</body>

</html>

จากการเปลี่ยน option ของ curl เล็กน้อยการส่งข้อมูลก็เปลี่ยนไปเป็นแบบ method post แล้ว แต่ก็ยังส่งไฟล์ไปไม่ได้อยู่ดี ๆ ใจเย็น ๆ ครับบทความหน้าส่งไฟล์ออกไปแน่ ๆ ครับ