【问题标题】:PHP: Send Values To HTML Form With Curl In PHPPHP:在 PHP 中使用 Curl 将值发送到 HTML 表单
【发布时间】:2015-07-15 07:26:18
【问题描述】:

请原谅我的问题... 这已经讨论过很多次了,但我没有注意到... :( 我们有这个 html 表单:

<!DOCTYPE html>
<html>
<head>
<title>Comment</title>
</head>
<body>
<fieldset>
<legend align="center">Comments</legend>
<form action="server.php" method="post">
Name: <input type="text" name="name" /><br><br>
Email: <input type="email" name="email" /><br><br>
Web: <input type="url" name="web" /><br><br>
Message:<br>
<textarea name="comment"></textarea><br><br>
<input type="submit" name="send" value="send" />
</form>
</fieldset>
</body>
</html>

还有这个 php 文件:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$web = $_POST['web'];
$comment = nl2br($_POST['comment']);
//database variables;
$host = "localhost";
$user = "root";
$password = "";
$db = "ashiyane";
//connect to mysql & insert data into table;
$connection = mysql_connect($host,$user,$password);
if ($connection){
    echo "Connected Successfully...";
    $select_db = mysql_select_db($db);
    if ($select_db && $name !== "" && $web !=="" && $email !=="" && $comment !==""){
        echo "<br>Database Selected...";
        $run_query = mysql_query("INSERT INTO comments (name,email,web,comment) VALUES              ('$name','$email','$web','$comment')");
        if ($run_query){
            echo "<br>Data Have Been Insert ...<br>";
        }else{
            echo "<br>Inserting Data Failed!";
        }
    }else{
        echo "<br>Database No Selected!";
    }
}else{
    echo "<br>Connecting Failed!";
}
//show results;
$show_query = mysql_query("SELECT * FROM comments");
while ($results = mysql_fetch_array($show_query)){
    echo $results['name']." : ".$results['email']." : ".$results['web']." : ".$results['comment']."<br>====================<br>";
}
?>

现在我们希望我们的值发送带有 curl 函数的 HTML 表单... 请帮我 ... 我再次为提出这个问题而道歉...... :) 感谢所有朋友! ;)

【问题讨论】:

    标签: php html post curl methods


    【解决方案1】:

    假设 curl 已在服务器上安装并启用,您应该能够执行以下操作:

    <?php
    $post_data = array(
        'name' => 'Your Name',
        'email' => 'user@example.com',
        'web' => 'http://www.example.com',
        'comment' => 'your comment',
    )
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL,"http://www.example.com/path/to/comment/post/script");
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
    
    curl_exec ($ch);
    curl_close ($ch); 
    ?>
    

    另一种选择,可能是更灵活的选择是使用Guzzle 它将为您处理 http 请求和响应。

    作为旁注,您发布的脚本当前是危险编写的,您直接将用户提供的数据插入数据库而不转义它(例如通过使用 mysql_real_escape_string),理想情况下您应该通过PDO 使用准备好的语句或类似的

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-01-28
      • 2020-03-12
      • 1970-01-01
      相关资源
      最近更新 更多