2

这是我现在得到的代码(更改了域名和子域名):

<?php
$agent = "Mozilla/5.0 (X11; U; Linux i686; en-US) AppleWebKit/534.16 (KHTML, like Gecko) Chrome/10.0.648.204 Safari/534.16";
$headers = "Expect:";
$postdata = "username=test&password=test&ref=".base64_encode(md5(time().".com"))."&session=s".md5(time());
$login="http://subdomain.mydomain.com/login.php";
$grab="http://subdomain.mydomain.com/index.php";
function login($url,$data){
        $fp = fopen("cookie.txt", "w");
        fclose($fp);
        $login = curl_init();
        curl_setopt($login, CURLOPT_COOKIEJAR, "cookie.txt");
        curl_setopt($login, CURLOPT_COOKIEFILE, "cookie.txt");
        curl_setopt($login, CURLOPT_TIMEOUT, 40000);
        curl_setopt($login, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($login, CURLOPT_URL, $url);
        curl_setopt($login, CURLOPT_USERAGENT, $agent);
        curl_setopt($login, CURLOPT_COOKIESESSION, true);
        curl_setopt($login, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($login, CURLOPT_POST, TRUE);
        curl_setopt($login, CURLOPT_POSTFIELDS, $data);
        ob_start();
        return curl_exec ($login);
        ob_end_clean();
        curl_close ($login);
unset($login);
}                                
function grab_page($site){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT, $agent);
        curl_setopt($ch, CURLOPT_TIMEOUT, 40000);
        curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
        curl_setopt($login, CURLOPT_COOKIESESSION, true);
        curl_setopt($ch, CURLOPT_URL, $site);
        ob_start();
        return curl_exec ($ch);
        ob_end_clean();
        curl_close ($ch);
}
function grab_subpage($site){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT, $agent);
        curl_setopt($ch, CURLOPT_TIMEOUT, 40000);
        curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
        curl_setopt($login, CURLOPT_COOKIESESSION, true);
        curl_setopt($ch, CURLOPT_URL, $site);
        ob_start();
        return curl_exec ($ch);
        ob_end_clean();
        curl_close ($ch);
}
function post_data($site,$data){
        $datapost = curl_init();
        curl_setopt($datapost, CURLOPT_URL, $site);
        curl_setopt($datapost, CURLOPT_TIMEOUT, 40000);
        curl_setopt($datapost, CURLOPT_HEADER, TRUE);
        curl_setopt($datapost, CURLOPT_HTTPHEADER, $headers);
        curl_setopt($datapost, CURLOPT_USERAGENT, $agent);
        curl_setopt($datapost, CURLOPT_POST, TRUE);
        curl_setopt($datapost, CURLOPT_POSTFIELDS, $data);
        curl_setopt($datapost, CURLOPT_COOKIEFILE, "cookie.txt");
        ob_start();
        return curl_exec ($datapost);
        ob_end_clean();
        curl_close ($datapost);
        unset($datapost);
}
login($login, $postdata);
echo grab_page($grab);
?>

好的,所以...如果我从http://subdomain.mydomain.com/导航任何页面,此代码工作正常,但如果我尝试从http://anothersubdomain.mydomain.com/获取页面,它会下降并显示给我一个白页。var_dump(grab_page($grab)); 回应我 string(0) "" 。我无法弄清楚问题是什么以及为什么我无法访问该页面。我现在正在谷歌搜索一个星期,尝试了很多方法,但没有任何效果,也许你们可以帮助我......

提前致谢。

4

1 回答 1

0

检查 cookie 文件,如果它有任何保存的 cookie,因为CURLOPT_COOKIEJAR应该在您关闭句柄时保存 cookie。

在您的登录功能中更改此代码:

    ob_start();
    return curl_exec ($ch);
    ob_end_clean();
    curl_close ($ch);

至:

$response_data = curl_exec($ch);
curl_close($ch);
return $response_data;
于 2012-10-22T08:51:18.680 回答