下面有2个文件,我正在尝试使用cURL自动登录并获取“登录成功”消息,与“登录失败”消息相反。我该怎么办?
我知道这可能看起来像我要求其他人为我编写代码,并且在提出这个问题之前我没有花费任何精力研究cURL和制作我自己的cURL代码。但是,我实际上做了很多研究,但是我找不到任何我所说的例子,或者做了我想做的事情,所以这就是我在这里问的原因。如果可以,请帮忙。
test1.php
<form method = "POST" action = "test2.php">
<input type = "username" name = "username">
<input type = "password" name = "password">
<input type = "submit" value = "submit">
</form>
test2.php
$username = $_POST['username'];
$password = $_POST['password'];
if ($username == "username" && $password == "password") {
echo "login success";
}
else {
echo "login fail";
}
需要结果:
login success
答案 0 :(得分:1)
这是基本代码:
<?php
$ch = curl_init('http://yourdomain.com/test2.php');
$post_params = array('username' => 'username', 'password' => 'password');
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_params); // PHP will format the array into POST data
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
if ($response == 'login success') {
echo 'Login was successful!';
} else {
echo 'Something went wrong :(';
}
请注意,登录真实网站比发送表单字段更复杂。您还需要取回会话cookie,并在将来的请求中发送它。参见
答案 1 :(得分:0)
您正在寻找the cURL command-line syntax to do a POST request:
curl -X POST --data&#34; param1 = value1&amp; param2 = value2&#34; https://example.com/test2.php
答案 2 :(得分:0)
我认为你可以这样做:
$data = array('username' => 'username', 'password' => password);
$url = 'http://example.com';
$handle = curl_init($url);
curl_setopt($handle, CURLOPT_POST, true);
curl_setopt($handle, CURLOPT_POSTFIELDS, $data);
$response = curl_exec($handle);
var_dump($response);
上的已接受答案中查看更多详细信息