PHP Curl - 登录后重定向

时间:2014-02-25 13:48:22

标签: php curl

我想从卷曲的网站上获取数据。 首先,我这样登录:

$userAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4"; 

$ch = curl_init("https://192.168.67.10/Appliance/index.jsp"); 

$username="***"; 
$password="***"; 
$submit = "userid=$username&pass=$password&submitted=true&rememberMe=0";

if(!$ch) { 
    die(sprintf("Fehler[%d]: %s", 
        curl_errno($ch), curl_error($ch))); 
} 
curl_setopt($ch, CURLOPT_VERBOSE, 1); 
curl_setopt($ch, CURLOPT_HEADER, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0); 
curl_setopt($ch, CURLOPT_COOKIEJAR, "/tmp/cookie.txt");  
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $submit);

$data = curl_exec($ch); 

if(!$data) { 
    die(sprintf("Fehler [%d]: %s", 
        curl_errno($ch), curl_error($ch))); 

} 

curl_close($ch); 

echo $data;

如何立即访问其他页面?

登录页面:https://192.168.67.10/Appliance/HomePage/index.jsp 我想访问的页面:https://192.168.67.10/Appliance/SystemsCenter/Encryption/CertificateStore/index.jsp

提前谢谢。

2 个答案:

答案 0 :(得分:0)

if(!$data){
    //your existing code

}else{
    //it was successful
    curl_close($ch);
    //repeat process of instantiating curl here
}

答案 1 :(得分:0)

我没有看到你使用CURLOPT_POST。所以请添加:

curl_setopt($ch, CURLOPT_POST, 1);

另外,使用启用跟随重定向,以便不遗余力!

curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);