我想从一个简单的登录网站获取curl / php的完整HTML源代码。我可以执行登录过程并获取源代码,但是另一端的服务器似乎忽略了所有html参数。
运行下面提供的代码时,我得到此URL的源代码:https://www.example.com/page,而不是https://www.example.com/page?user=1&date=2018-12-12的URL。 如果我在任何网络浏览器中打开带有参数的网址,就会显示正确的网站。
我试图从另一个服务器和网站中获取参数,这些网站运行得很好:https://www.example2.com/otherpage?user=1&date=2018-12-12
<?php define('USERNAME', 'user');
define('PASSWORD', '1234');
define('USER_AGENT', 'Mozilla/5.0 (Windows NT 10.0; Win64; x64)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113
Safari/537.36');
define('COOKIE_FILE', 'cookie.txt');
define('LOGIN_FORM_URL', 'https://www.example.com/admin');
define('LOGIN_ACTION_URL', 'https://www.example.com/admin');
$postValues = array(
'user' => USERNAME,
'pass' => PASSWORD
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, LOGIN_ACTION_URL);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($postValues));
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_REFERER, LOGIN_FORM_URL);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_exec($curl);
if(curl_errno($curl)){
throw new Exception(curl_error($curl));
}
curl_setopt($curl, CURLOPT_URL, "https://www.example.com/page?user=1&date=2018-12-12");
curl_setopt($curl, CURLOPT_COOKIEJAR, COOKIE_FILE);
curl_setopt($curl, CURLOPT_USERAGENT, USER_AGENT);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
echo curl_exec($curl);
?>
来源:http://thisinterestsme.com/php-login-to-website-with-curl/
我很困惑,因为它与example2.com一起使用,但不能与example.com(它们当然是两个不同的Web服务器和站点)一起使用。有想法吗?
答案 0 :(得分:0)
我找到了一种可行的解决方法:
使用以下简单代码行创建一个新的.php文件:
header('Location: https://www.example.com/page?user=1&date=2018-12-12');
在主.php文件中,只需将上面的url替换为新的.php文件url:
curl_setopt($curl, CURLOPT_URL, "newfile.php");