服务器未收到CURL URL参数

问题描述 投票:0回答:1

我想从一个简单登录的网站上用curl / php抓取完整的HTML源代码。我可以执行登录过程并获取源代码,但另一端的服务器似乎忽略了所有的html参数。

当我运行下面提供的代码时,我得到了这个url的源代码:https://www.example.com/page而不是https://www.example.com/page?user=1&date=2018-12-12。如果我在任何网络浏览器中打开带有参数的网址,它会显示正确的网站。

我试图抓住另一个网站,其中包含来自不同服务器和网站的参数: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服务器和站点)一起使用。任何的想法?

php curl
1个回答
0
投票

我找到了一个有效的解决方法:

使用以下简单代码行创建一个新的.php文件:

header('Location: https://www.example.com/page?user=1&date=2018-12-12');

在主.php文件中,只需用新的.php文件url替换上面的url:

curl_setopt($curl, CURLOPT_URL, "newfile.php");
© www.soinside.com 2019 - 2024. All rights reserved.