卷曲不使用子域

时间:2012-10-22 00:50:24

标签: curl

以下是我现在获得的代码(更改了域名和子域名):

<?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($抢));回应我的字符串(0)“”。我无法弄清楚问题是什么以及为什么我无法访问该页面。我正在谷歌搜索一个星期,尝试了大量的方法并没有任何效果,也许你们可以帮助我...

提前致谢。

1 个答案:

答案 0 :(得分: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;