PHP:在PHP中使用Curl将值发送到HTML表单

时间:2022-10-16 13:27:14

Excuse me for my question ... This has been discussed many times but i didn't notice... :( we have this html form :

对不起我的问题...这已经讨论了很多次,但我没有注意到...... :(我们有这个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>

and this php file:

这个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>";
}
?>

Now we want our values to send HTML form with curl functions ... please help me ... Once again I apologize for asking this question... :) Thanks All Friend !! ;)

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

1 个解决方案

#1


Assuming curl is installed and enabled on the server, you should be able to do something like:

假设在服务器上安装并启用了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); 
?>

Another alternative, and possibly a more flexible one would be to use Guzzle which will handle the http request and response for you.

另一个替代方案,也可能是更灵活的方法是使用Guzzle来处理http请求和响应。

As a side note, the script you posted is currently dangerously written, you are directly inserting user provided data into the database without escaping it (e.g. by using mysql_real_escape_string), ideally you should be using a prepared statement via PDO or similar

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

#1


Assuming curl is installed and enabled on the server, you should be able to do something like:

假设在服务器上安装并启用了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); 
?>

Another alternative, and possibly a more flexible one would be to use Guzzle which will handle the http request and response for you.

另一个替代方案,也可能是更灵活的方法是使用Guzzle来处理http请求和响应。

As a side note, the script you posted is currently dangerously written, you are directly inserting user provided data into the database without escaping it (e.g. by using mysql_real_escape_string), ideally you should be using a prepared statement via PDO or similar

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