登录站点并在登录后重定向到php curl中的页面

时间:2016-09-22 05:58:19

标签: php curl

要登录我使用过的网站。但成功登录后,我如何在网站中重定向到我想要的页面。

通过php curl登录我使用了这个。

 $loginUrl = 'https://secure.propertyshark.com/mason/Accounts/logon.html';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $loginUrl);
    curl_setopt($ch, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "email=myemail@hotmail.com&password=mypassword");
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_COOKIESESSION, true);
$cookie = getcwd().DIRECTORY_SEPARATOR.'cookie.txt';
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);
    $answer = curl_exec($ch);
    if (curl_error($ch)) {
        echo curl_error($ch);
    }
    curl_setopt( $ch, 'http://www.propertyshark.com/mason/Property/619443/133-32-244-St-Queens-NY-11422/');
    echo $answer;

但我不确定它是否有效,所以我想如何确认登录并重定向到“http://www.propertyshark.com/mason/Property/619443/133-32-244-St-Queens-NY-11422/”页面。

现在我正在使用它,但没有工作。

<?php
$loginUrl = 'https://secure.propertyshark.com/mason/Accounts/logon.html';
$ch1 = curl_init();
$cookie = getcwd().DIRECTORY_SEPARATOR.'cookie.txt';
curl_setopt($ch1, CURLOPT_URL, $loginUrl);
curl_setopt($ch1, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch1, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);

curl_setopt($ch1, CURLOPT_USERAGENT,'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Ubuntu Chromium/32.0.1700.107 Chrome/32.0.1700.107 Safari/537.36');
curl_setopt($ch1, CURLOPT_POST, true);
curl_setopt($ch1, CURLOPT_POSTFIELDS, "email=myemail@hotmail.com&password=password");
curl_setopt($ch1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch1, CURLOPT_COOKIESESSION, true);


$next_page = 'http://www.propertyshark.com/mason/Property/619443/133-32-244-St-Queens-NY-11422/';
$ch2 = curl_init();
$cookie = getcwd().DIRECTORY_SEPARATOR.'cookie.txt';
curl_setopt($ch2, CURLOPT_URL, $next_page);
curl_setopt($ch2, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);

$answer = curl_exec($ch2);
if (curl_error($ch2)) {
    echo curl_error($ch2);
}

echo $answer;

1 个答案:

答案 0 :(得分:1)

$cookie = getcwd().DIRECTORY_SEPARATOR.'cookie.txt';
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie);

您需要添加cookie文件选项,并在登录后向下一页发送另一个卷曲请求

LATER EDIT:

        $loginUrl = 'https://secure.propertyshark.com/mason/Accounts/logon.html';
        $ch1 = curl_init();
        $cookie = getcwd().DIRECTORY_SEPARATOR.'cookie.txt';
        curl_setopt($ch1, CURLOPT_URL, $loginUrl);
        curl_setopt($ch1, CURLOPT_COOKIEJAR, $cookie);
        curl_setopt($ch1, CURLOPT_COOKIEFILE, $cookie);
        curl_setopt($ch1, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch1, CURLOPT_FOLLOWLOCATION, TRUE);

        ............


        $next_page = 'http://www.propertyshark.com/mason/Property/619443/133-32-244-St-Queens-NY-11422/';
        $ch2 = curl_init();
        $cookie = getcwd().DIRECTORY_SEPARATOR.'cookie.txt';
        curl_setopt($ch2, CURLOPT_URL, $next_page);
        curl_setopt($ch2, CURLOPT_COOKIEJAR, $cookie);
        curl_setopt($ch2, CURLOPT_COOKIEFILE, $cookie);
        curl_setopt($ch2, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch2, CURLOPT_FOLLOWLOCATION, TRUE);
        ............